|
nbsp; CreateFontIndirect(lf)); // Change foreground and background colors. 改变前景色和背景色 clFore := SetTextColor(ACanvas.Handle, clSilver); clBack := SetBkColor(ACanvas.Handle, clBlack); // Get the menu height. 获取菜单高度 MenuHeight := (ARect.Bottom-ARect.Top) * ((Sender as TMenuItem).Parent as TMenuItem).Count; Rectang := Rect(-1, 0, dwCheck-1, MenuHeight); // Draw the text. 画文本 ExtTextOut(ACanvas.Handle, -1, MenuHeight, Eto_Clipped, @Rectang, ''''Made in Borland'''', 15, nil); // Returns to the original state. 返回到最初状态 DeleteObject(SelectObject(ACanvas.Handle, OldFont)); SetTextColor(ACanvas.Handle, clFore); SetBkColor(ACanvas.Handle, clBack); end; // Draw the real menu text. 画真实的菜单项文本 ARect.Left := ARect.Left + LoWord(dwCheck) + 2; DrawText(ACanvas.Handle, PChar((Sender as TMenuItem).Caption), Length((Sender as TMenuItem).Caption), ARect, 0); end;
Figure 6: Using OnDrawItem to draw vertical text on a menu. Figure 7: Menu with vertical text.
One tricky detail is knowing where to begin drawing the text. It should begin at the bottom of the last item on the menu. To get its position, we get the height of the menu item, using: 从哪儿开始画文本是应该知道的。它应该在菜单的最后一项的底部开始。为了得到这个位置,我们如下这样要获取菜单项的高度: ARect.Top - ARect.Bottom and multiply it by the number of items in the menu: 并且乘上菜单项的数目: (((Sender as TMenuItem).Parent as TMenuItem).Count)
Rotated Text 旋转的文本 The Windows API allows you to draw text at any angle. To do this in Delphi, you must use the API function CreateFont or CreateFontIndirect. CreateFont is declared as shown in Figure 8. Windows API可以让你用任何角度来画文本。为了在Delphi中做到这点,你必须用到CreateFont或者CreateFontIndirect这两个API函数。Figure 8显示了如何声明CreateFont。 function CreateFont( nHeight, // Logical height of font. 字体的逻辑高度 nWidth, // Logical average character width. 字符的逻辑平均宽度 nEscapement, // Angle of escapement. 旋转的角度 nOrientation, // Base-line orientation angle. 底线的定位角度 fnWeight: Integer; // Font weight. 字体的weight子属性 fdwItalic, // Italic attribute flag. 是否斜体 fdwUnderline, // Underline attribute flag. 是否下划线 fdwStrikeOut, // Strikeout attribute flag. 是否Strikeout属性 fdwCharSet // Character set identifier. 字符集 fdwOutputPrecision, // Output precision. fdwClipPrecision, // Clipping precision. fdwQuality, // Output quality. fdwPitchAndFamily: DWORD; // Pitch and family. lpszFace: PChar // Pointer to typeface name string. ): HFONT; stdcall;
Figure 8: The Object Pascal declaration for the CreateFont Windows API function.
While this function has many parameters, you will usually want only to change one or two attributes of the text. In such cases, you should use the CreateFontIndirect function instead. It takes only one argument - a record of type TLogFont, as shown in Figure 9. 虽然这函数有很多参数,但你通常只须改变文本的一个或两个属性。在这种情形下,你将使用CreateFontIndirect函数来代替。它只须一个参数----一个TlogFont的记录类型的参数,在Figure 9可以看到。
tagLOGFONTA = packed record lfHeight: Longint; lfWidth: Longint; lfEscapement: Longint; lfOrientation: Longint; lfWeight: Longint; lfItalic: Byte; lfUnderline: Byte; lfStrikeOut: Byte; lfCharSet: Byte; lfOutPrecision: Byte; lfClipPrecision: Byte; lfQuality: Byte; lfPitchAndFamily: Byte; lfFaceName: array[0..LF_FACESIZE - 1] of AnsiChar; end; TLogFontA = tagLOGFONTA; TLogFont = TLogFontA; Figure 9: The TLogFont record.
Looking at this record, you''''ll notice its members match the parameters for the CreateFont function. The advantage of using this function/record combination is that you can fill the record''''s members with a known font using the GetObject API function, change the members you want, and create the new font. 仔细看下这个记录类型,你会发现它的成员与CreateFont函数的参数十分相似。使用这个函数/记录 的联合体的好处是,你可以用GetObject这个API函数来将一个已知的字体来填满这个记录的成员值,然后改变你想改变的成员值来产生一个新字体。
To draw rotated text, the only member you must change is lfEscapement, which sets the text angle in tenths of degrees. So, if you want text drawn at 45 degrees, you must set lfEscapement to 450. 为了画出旋转的文字,你仅仅只须改变的成员值是lfEscapement,它可以用十分之一度的单位来设置字体的角度。所以,如果你想字符旋转45度,你必须设置 lfEscapement为450。 Notice that there are flags to draw italic, underline, and strikeout text, but there is no flag to draw bold text. This is done with the lfWeight member, a number between 0 and 1000. 400 is normal text, values above this draw bold text, and values below it draw light text. 注意到这里有不少标记来选取斜体,下划线,凸出文字,但是却没有标记来画粗体。这是因为用lfWeight成员来代替了,这个成员的数值介于0与1000之间。400是正常值,高于这个值的是粗体,低于这个值的是细体。
The code in Figure 10 draws text at angles ranging from 0 degrees to 360 degrees, at 20-degree intervals. It''''s the form''''s OnPaint event handler, so the text is redrawn each time the form is painted. Figure 11 shows the result. Figure 10中的代码从0度到360度每隔20度就画一次字符。这是在窗体的OnPaint事件中触发的,所以文字在窗体每次描绘时重画。在Figure 11可以看到效果。
procedure TForm1.FormPaint(Sender: TObject); var OldFont, NewFont : hFont; LogFont : TLogFont; i : Integer; begin // Get handle of canvas font. 获取窗体字体对象的句柄 OldFont := Canvas.Font.Handle; i := 0; // Transparent drawing. 设置透明属性 SetBkMode(Canvas.Handle, Transparent); // Fill LogFont structure with information 用信息填写LogFont结构 // from current font. 从当前字体 GetObject(OldFont, Sizeof(LogFont), @LogFont); // Angles range from 0 to 360. 从0到360度 while i < 3600 do begin // Set escapement to new angle. 设置文字方向到新的角度 LogFont.lfEscapement := i; // Create new font. 创建新字体 NewFont := CreateFontIndirect(LogFont); // Select the font to draw. 选取字体来输出 SelectObject(Canvas.Handle, NewFont); // Draw text at the middle of the form. 在窗体中间输出文字 TextOut(Canvas.Handle, ClientWidth div 2, ClientHeight div 2, ''''Rotated Text'''', 21); // Clean up. 清空 DeleteObject(SelectObject(Canvas.Handle, OldFont)); // Increment angle by 20 degrees. 每隔20度递增 Inc(i, 200); end; end;
Figure 10: Code to draw text rotated in 20-degree intervals. Figure 11: Text rotated 360 degrees.
The form''''s font is set to Arial, a TrueType font. This code works only with TrueType fonts; other kinds of fonts don''''t support text rotation. To get current font settings and fill the TLogFont structure, you must use the GetObject API function. The code in Figure 12 shows how to fill and display the TLogFont settings for the form''''s font. 这个窗体的字体设置成Arial,一种TrueType字体。这段代码仅仅在TrueType字体下才能运行;其它字体不支持文字旋转。为了获取当前字体设置和填写TlogFont结构体,你必须用到GetObject这个API函数。在Figure 12中的代码中可以看到如何填写和显示窗体中TlogFont的设置。
procedure TForm1.Info1Click(Sender: TObject); var LogFont : TLogFont; begin // Fill LogFont structure with information 填写LogFont结构体的成员值 // from current font. 从当前字体 GetObject(Canvas.Font.Handle, Sizeof(LogFont), @LogFont); // Display font information. 显示字体信息 with LogFont do ShowMessage( ''''lfHeight: '''' + IntToStr(lfHeight) + #13 + ''''lfWidth: '''' + IntToStr(lfWidth) + #13 + ''''lfEscapement: ''''+IntToStr(lfEscapement) + #13 + ''''lfOrientation: '''' + IntToStr(lfOrientation) + #13 + ''''lfWeight: '''' + IntToStr(lfWeight) + #13 + ''''lfItalic: '''' + IntToStr(lfItalic) + #13 + ''''lfUnderline: '''' + IntToStr(lfUnderline) + #13 + ''''lfStrikeOut: '''' + IntToStr(lfStrikeOut) + #13 + ''''lfCharSet: '''' + IntToStr(lfCharSet) + #13 + ''''lfOutPrecision: '''' + IntToStr(lfOutPrecision) + #13 + ''''lfClipPrecision: '''' + IntToStr(lfClipPrecision) + #13 + ''''lfQuality: '''' + IntToStr(lfQuality) + #13 + ''''lfPitchAndFamily: ''''+IntToStr(lfPitchAndFamily) + #13 + ''''lfFaceName: '''' + string(lfFaceName)); end;
Figure 12: Getting and displaying font attributes.
Once you have the settings in a TLogFont structure, the only change left is to set lfEscapement to the desired angle and create a new font with CreateFontIndirect. Before using this new font, it must be selected with SelectObject. Another way is to assign the handle of this new font to the handle of the canvas''''s font, before drawing the t
上一页 [1] [2] [3] 下一页 没有相关教程
|