如何获取Memo/RichEdit控件中字体的宽度?(100分)

  • 主题发起人 主题发起人 FlyBeckham
  • 开始时间 开始时间
F

FlyBeckham

Unregistered / Unconfirmed
GUEST, unregistred user!
应该如何调用那个Win Api?
 
好像有得到某个字符的宽度的函数,没有某种字体的宽度,<br>因为一种字体的字符宽度可能不同。
 
这个不行?<br>memo1.font.size
 
将form字体设成和memo/richedit一样,然后通过form.canvas.TextWidth<br>可以得到.
 
设置form字体有问题的. 很多控件的parentfont默认是true. 一设form字体, <br>那些控件字体就都变掉了. 也许会影响界面效果.<br>取得memo的字体宽度不难, 因为它的字体是固定的, richedit 只能取得当前<br>默认字体的宽度.<br>var<br>&nbsp; dc: HDC;<br>&nbsp; sz: TSize;<br>begin<br>dc := getdc(memo.handle);<br>gettextextentpoint32(dc, pchar('W'), 1, sz);<br>sz.cx 就是字体的宽度.<br>sz.cy 是高度.
 
<br>&nbsp;tagTEXTMETRICA = record<br>&nbsp; &nbsp; tmHeight: Longint;<br>&nbsp; &nbsp; tmAscent: Longint;<br>&nbsp; &nbsp; tmDescent: Longint;<br>&nbsp; &nbsp; tmInternalLeading: Longint;<br>&nbsp; &nbsp; tmExternalLeading: Longint;<br>&nbsp; &nbsp; tmAveCharWidth: Longint;<br>&nbsp; &nbsp; tmMaxCharWidth: Longint;<br>&nbsp; &nbsp; tmWeight: Longint;<br>&nbsp; &nbsp; tmOverhang: Longint;<br>&nbsp; &nbsp; tmDigitizedAspectX: Longint;<br>&nbsp; &nbsp; tmDigitizedAspectY: Longint;<br>&nbsp; &nbsp; tmFirstChar: AnsiChar;<br>&nbsp; &nbsp; tmLastChar: AnsiChar;<br>&nbsp; &nbsp; tmDefaultChar: AnsiChar;<br>&nbsp; &nbsp; tmBreakChar: AnsiChar;<br>&nbsp; &nbsp; tmItalic: Byte;<br>&nbsp; &nbsp; tmUnderlined: Byte;<br>&nbsp; &nbsp; tmStruckOut: Byte;<br>&nbsp; &nbsp; tmPitchAndFamily: Byte;<br>&nbsp; &nbsp; tmCharSet: Byte;<br>&nbsp; end;<br><br>tagTEXTMETRICA在Windows中声明<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; dc:hdc;<br>&nbsp; tm:tagTextMetrica;<br>begin<br>&nbsp; dc:=getdc(memo1.Handle);<br>&nbsp; gettextmetrics(dc,tm);<br>&nbsp; showmessage(inttostr(tm.tmAveCharWidth));<br>&nbsp; releasedc(memo1.handle,dc);<br>end;<br>
 
多人接受答案了。
 
后退
顶部