BeginPaint和GetDC的区别
BeginPaint和GetDC的区别
代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
protected
procedure WndProc(var Message:TMessage); override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.WndProc(var Message: TMessage);
var
DC: HDC;
Str: String;
PS: tagPaintStruct;
begin
if Message.Msg = WM_PAINT then
begin
Str := 'Hello Ming.';
DC := BeginPaint(self.Handle,PS);
TextOut(DC,0,0,PChar(Str),Length(Str));
EndPaint(Self.Handle,PS);
end;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
DC: HDC;
Str: String;
PS: tagPaintStruct;
begin
Str := 'Hello Ming.';
DC := GetWindowDC(Self.Handle);//GetDC(0); 0:Desktop
TextOut(DC,0,0,PChar(Str),Length(Str));
ReleaseDC(Self.Handle,DC);//ReleaseDC(0,DC); 0:Desktop
{ TODO :
下面的代码并不能实现输出,BeginPaint,EndPaint只能在窗体过程中的WM_PAINT中使用.
BeginPaint()和EndPaint()可以删除消息队列中的WM_PAINT消息,并使无效区域有效.
GetDC()和ReleaseDC()并不删除也不能使无效区域有效,因此当程序跳出WM_PAINT 时,
无效区域仍然存在.系统就回不断发送WM_PAINT消息,于是程序不断处理WM_PAINT消息.}
{
DC := BeginPaint(self.Handle,PS);
TextOut(DC,0,0,PChar(Str),Length(Str));
EndPaint(Self.Handle,PS);
}
end;
end.