Мне нужно показать ключ base64 в TMemo. К сожалению, невозможно правильно отобразить эту строку base64: она обрезается на каждом «/» возвратом каретки или на любом «+», где систематически запускается новая строка! Я пробовал все, что было в моих силах, чтобы сделать эту строку одной длинной фразой (без возврата каретки), но не понятна. Как можно получить плоскую строку в base64 (без возврата каретки), если возможно, изменить размер автоматически при изменении размера формы и TMemo? Большое спасибо.Fmx TMemo не может показать строку base64 соответствующим образом
0
A
ответ
0
Для тех, кто заинтересован, код ниже: TForm с TMemo (памятка). Это решение работает для меня для плоской строки Base64. Наконец, больше не отключается строка при каждом/или +. Возможно, нижеследующее решение нужно настроить, но оно работает для меня достаточно. Конечно, прежде чем обрабатывать строку b64 в приложении, ее нужно фильтровать для устранения CR-LF, но это нормально. Я использую события: OnKeyDown, OnResize, OnPainting TMemo. Я написал специальный формат функцииMemo (..), который выполняет задание выравнивания строк соответствующим образом. Код принимает только истинные символы B64 и фильтрует неисправные символы, если они есть.
#define IS_B64(c) (isalnum(c) || (c == '/') || (c == '+') || (c == '='))
//Adjustments work for Courier New, standard size:
const float FW=7.2;//Font width
const diff=25;//Room for vert. scroll bar
//Gives the number of characters in one line of the TMemo:
// width : width in pixels where to put the line of chars
// font_sz : the average width of a character
// returns the number of characters by line of the TMemo
inline int nchars(int width, float font_sz)
{
return int(float(width-diff)/font_sz);
}//nchars
//---------------------------------------------------------------------------
//Formats the memo to a certain length of characters:
// *p : the memo to format
// nc : the number of characters for each line.
void formatMemo(TMemo *p, int nc)
{
if(p==0) return;
AnsiString src, dest;//UnicodeString is less fast...
//Filter everything as B64 only:
for(int i=1; i<=p->Text.Length(); ++i) {//Indexing is "1-based" like on Delphi (except on mobiles)
if(IS_B64(p->Text[i])) dest += p->Text[i];
}
p->Lines->Clear();//Erases everyting
int length=dest.Length(), units=length/nc, remain=length%nc;
for(int k=0 ; k<units ; ++k) {
p->Lines->Append(dest.SubString(1+k*nc, nc));
}
if(remain) {
p->Lines->Append(dest.SubString(1+units*nc, remain));
}
}//formatMemo
//---------------------------------------------------------------------------
void __fastcall TForm1::memoKeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift)
{
//This event is triggered before the character is sent in Text.
//Saves caret position:
TCaretPosition p={memo->CaretPosition.Line, memo->CaretPosition.Pos};
memo->Tag=0;//Don't do a format.
if(Key==0 && !IS_B64(KeyChar))//Printable KeyChar
{
//Changes the entry into '0':
KeyChar='0';
KeyDown(Key,KeyChar,Shift);
//Put a backspace to erase:
Key=vkBack; KeyChar=0;
KeyDown(Key,KeyChar,Shift);
}
else memo->Tag=1;//Programs a format in the OnPainting
memo->SetFocus();
memo->CaretPosition=p;//Repositions the caret
}
//---------------------------------------------------------------------------
//In case of resize, reformat the TMemo
void __fastcall TForm1::memoResize(TObject *Sender)
{
formatMemo(memo, nchars(memo->Width,FW));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::memoPainting(TObject *Sender, TCanvas *Canvas, const TRectF &ARect)
{
//We will use the Tag of the memo as a parameter, to plan a reformat.
if(memo->Tag){//A format is asked by OnKeyDown.
TCaretPosition p={memo->CaretPosition.Line, memo->CaretPosition.Pos};
formatMemo(memo, nchars(memo->Width,FW));
memo->SetFocus();
memo->CaretPosition=p;
memo->Tag=0;//Done
}
}
//---------------------------------------------------------------------------