如何得到TMemo的行高(100分)

  • 主题发起人 主题发起人 devil_li
  • 开始时间 开始时间
D

devil_li

Unregistered / Unconfirmed
GUEST, unregistred user!
如何得到TMemo的行高?<br>我想让TMemo有这样的效果:像RichEdit一样,列滚动条是动态的,当内容超过Memo高度时,自动出现滚动条,这样就需要判断TMemo的行高*行数 来判断是否应该出现滚动条了.<br>注意:Memo.Font.Height这样是不行的,比如Arail 9,memo行高15,但是font.height=12,<br>TControlCanvas.TextHeight, Windows.TextOut这样都是不行的<br>
 
这个问题我解决了,用的这个方法:<br>给memo发一个EM_SCROLL消息,如果返回值为true,表示可以滚动,那么添加滚动条<br>反之,取消滚动条<br>不过判断行高还是没搞定,如果那位高手能搞定的话,我还是会给分的<br>附我的代码:<br>procedure TfrmBaseChat.MsgOutChange(Sender: TObject);<br>var<br>&nbsp; idx: Integer;<br>&nbsp; CanScroll: Boolean;<br>begin<br>&nbsp; inherited;<br>&nbsp; idx := MsgOut.SelStart;<br>&nbsp; CanScroll := False;<br>&nbsp; if HiWord(SendMessage(MsgOut.Handle, EM_SCROLL, SB_LINEDOWN, 0)) = 1 then<br>&nbsp; begin<br>&nbsp; &nbsp; CanScroll := True;<br>&nbsp; &nbsp; SendMessage(MsgOut.Handle, EM_SCROLL, SB_LINEUP, 0);<br>&nbsp; end<br>&nbsp; else if HiWord(SendMessage(MsgOut.Handle, EM_SCROLL, SB_LINEUP, 0)) = 1 then<br>&nbsp; begin<br>&nbsp; &nbsp; CanScroll := True;<br>&nbsp; &nbsp; SendMessage(MsgOut.Handle, EM_SCROLL, SB_LINEDOWN, 0);<br>&nbsp; end;<br>&nbsp; if CanScroll and (MsgOut.ScrollBars = ssNone) then<br>&nbsp; begin<br>&nbsp; &nbsp; MsgOut.ScrollBars := ssVertical;<br>&nbsp; &nbsp; MsgOut.SelStart := idx;<br>&nbsp; &nbsp; MsgOut.SelLength := 0;<br>&nbsp; end<br>&nbsp; else if not CanScroll and (MsgOut.ScrollBars = ssVertical) then<br>&nbsp; begin<br>&nbsp; &nbsp; MsgOut.ScrollBars := ssNone;<br>&nbsp; &nbsp; MsgOut.SelStart := idx;<br>&nbsp; &nbsp; MsgOut.SelLength := 0;<br>&nbsp; end;<br>end;<br>
 
用GetGUIThreadInfo函数可以得到你所说的“行高”。这个函数使用了一个结构GUIThreadInfo,其中rcCaret成员是一个RECT类,其bottom减去top便是你所需要<br>的“行高”(单位是像素)。
 
ok,给分了,多谢ozmaster<br>附上代码:<br>procedure TForm1.SpeedButton1Click(Sender: TObject);<br>var<br>&nbsp; info: TGUIThreadinfo;<br>begin<br>&nbsp; FillChar(info, Sizeof(info), 0);<br>&nbsp; info.cbSize := Sizeof(info);<br>&nbsp; if not GetGUIThreadInfo(GetCurrentThreadId, info) then<br>&nbsp; &nbsp; raise Exception.Create('error');<br>&nbsp; ShowMessage(IntToStr(info.rcCaret.Bottom - info.rcCaret.Top));<br>end;<br>
 
后退
顶部