为什么TextOut只能在响应WM_PAINT消息后调用才能显示? ( 积分: 100 )

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

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.FormCreate(Sender: TObject);<br>var<br> &nbsp;_hdc:HDC;<br> &nbsp;_hwnd:hwnd;<br> &nbsp;lpPaint:tagPaintStruct;<br>begin<br> &nbsp;_hdc:=BeginPaint(_hwnd,lpPaint);<br> &nbsp;TextOut(_hdc,0,0,'sdf',4);<br> &nbsp;EndPaint(_hwnd,lpPaint);<br>end;<br>这个TextOut怎么不能使?
 
procedure TForm1.FormCreate(Sender: TObject);<br>var<br> &nbsp;_hdc:HDC;<br> &nbsp;_hwnd:hwnd;<br> &nbsp;lpPaint:tagPaintStruct;<br>begin<br> &nbsp;_hdc:=BeginPaint(_hwnd,lpPaint);<br> &nbsp;TextOut(_hdc,0,0,'sdf',4);<br> &nbsp;EndPaint(_hwnd,lpPaint);<br>end;<br>这个TextOut怎么不能使?
 
洗分呀你
 
procedure TForm1.FormCreate(Sender: TObject);<br>var<br> &nbsp;_hdc:HDC;<br> &nbsp;_hwnd:hwnd;<br> &nbsp;lpPaint:tagPaintStruct;<br> &nbsp;str:PChar;<br>begin<br> &nbsp;str:='abc';<br> &nbsp;_hdc:=BeginPaint(_hwnd,lpPaint);<br> &nbsp;TextOut(_hdc,0,0,str,4);<br> &nbsp;EndPaint(_hwnd,lpPaint);<br>end;
 
晕,<br>1. 'abc'是3个字符<br>TextOut(_hdc,0,0,'abc',3);<br>2. Delphi是不区别大小写,而c是区别的,<br>所以hdc:HDC在c语言正确,但在delphi行不通,所以改为_hdc:HDC;才可以<br>3. BeginPaint(), EndPaint() 是只能用在响应WM_PAINT消息,而不是在FormCreate<br><br>4. 注意:Textout(..)显示的文字,窗体上如果有控件,可能会遮挡显示的文字<br><br>下面代码调试通过<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;protected<br> &nbsp; &nbsp;procedure WMPaint(var Message: TWMPaint); message WM_PAINT;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.WMPaint(var Message: TWMPaint);<br>var<br> &nbsp;_hdc:HDC;<br> &nbsp;_hwnd:hwnd;<br> &nbsp;lpPaint: tagPaintStruct;<br>begin<br> &nbsp;_hdc:=BeginPaint(Self.Handle,lpPaint);<br> &nbsp;TextOut(_hdc, 0, 0,'abc', 3);<br> &nbsp;EndPaint(Self.Handle, lpPaint);<br>end;<br><br>end.<br><br><br>上面代码用WindowsApi麻烦, 用Delphi的Canvas方便:<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;procedure FormPaint(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;protected<br> &nbsp;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br><br><br>procedure TForm1.FormPaint(Sender: TObject);<br>begin<br> &nbsp; &nbsp;Canvas.TextOut(0, 0, 'abc');<br>end;<br><br>end.
 
为什么TextOut只能在响应WM_PAINT消息后调用才能显示<br>在其它事件中如FormCreate,FormActivated中却不行?
 
FormCreate,FormActivated 窗口还没有建立,和没有激活
 
多人接受答案了。
 
后退
顶部