您可以计算当前字幕的最大长度。
表单的当前 ClientWidth 在运行时可用,并且使用表单设计器可以估计图标占用的空间。 AnsiString 的像素宽度由 Canvas->TextWidth 函数返回。
AnsiString Words = First + Middle + Last;
// store width of text in pixels
WordsWidthInPixels = Canvas->TextWidth(Words);
空格的数量可以借助一两个空格的TextWidth找到。
更新:
这里是一些使用系统指标而不是设计者估计的代码。我已经将几乎所有代码都放在了一个名为 GetNumSpacesMetric 的函数中。
函数头添加到Form类的头文件中:-
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormResize(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
int __fastcall GetNumSpacesMetric(TObject *Sender, TComponent* AForm);
};
在表单上创建事件OnResize,并添加在调整表单大小时更新Caption的代码。如果表单的名称不是 Form1 则需要在此处的代码中进行更改:-
void __fastcall TForm1::FormResize(TObject *Sender)
{
// strings
const AnsiString First = AnsiString("First");
const AnsiString Middle = AnsiString("Middle");
const AnsiString Last = AnsiString("Last");
// get number of spaces
int NumSpacesMetric = GetNumSpacesMetric(Sender, Form1);
// print the caption
if( NumSpacesMetric > 0 ) {
AnsiString Spaces = AnsiString::StringOfChar(' ', NumSpacesMetric);
AnsiString caption = First + Spaces + Middle + Spaces + Last;
Form1->Caption = caption;
}
}
接下来添加 GetNumSpacesMetric 函数定义。在函数头和创建 Image 的位置。
// calculate the number of spaces needed between three words in Form Caption
int __fastcall TForm1::GetNumSpacesMetric(TObject *Sender, TComponent* AForm)
{
const int NumberOfMenuIcons = 3;
const AnsiString Words = "FirstMiddleLast";
const AnsiString TwinSpace = AnsiString::StringOfChar(' ', 2);
const int Squeeze = 7 * 8; // tweak 1 - squeeze string length
//const int FineTune = 840; // tweak 2 - lengthen string when width smaller
//const int LimitLength = 980; // tweak 3
static int WordsPixelWidth;
static int TwinSpacePixelWidth = 1;
// get metric data
static NONCLIENTMETRICS ncm;
static bool done = false;
// do once
if(!done)
{
ncm.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo( SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &ncm, NULL);
TImage *tmpImage = new TImage(AForm);
// Font data
tmpImage->Canvas->Font->Handle = CreateFontIndirect(&ncm.lfCaptionFont);
// get pixel widths of Words and double space
WordsPixelWidth = tmpImage->Canvas->TextWidth(Words);
TwinSpacePixelWidth = tmpImage->Canvas->TextWidth(TwinSpace);
DeleteObject(tmpImage->Canvas->Font->Handle);
done = true;
}
int clientwidth = ClientWidth;
/*
// limit length of text if required
if( clientwidth > LimitLength)
clientwidth = LimitLength;
*/
// client width minus icon widths and words width
int NumOfPixelsLeft = clientwidth
- ncm.iCaptionWidth
- (ncm.iMenuWidth * NumberOfMenuIcons)
- WordsPixelWidth
- Squeeze
// + ((8 * (FineTune - clientwidth))/100)
;
// return number of pixels available divided by size of two spaces
return NumOfPixelsLeft / TwinSpacePixelWidth;
}
//---------------------------------------------------------------------------
有一些调整可用于更改程序并在代码中给出简短描述。
更新 2:
为 GetNumSpacesMetric 添加了一个参数,用于传递 Form 对象。
有一组更新的指令可以获取一些指标:-
TITLEBARINFO 结构,TITLEBARINFOEX 结构,GetTitleBarInfo 函数和 GetTitleBarInfoEx 函数。 p>