功能:程序间传递消息,获得屏幕图形。代码如下,同时运行这两个程序,在form1中按下按纽,则在form2中会出现form1的图形。
Form1代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
UserMessage : UINT;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
UserMessage := RegisterWindowMessage('WM_SAMPLE');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sendmessage(HWND_BROADCAST,UserMessage ,handle,0);
end;
end.
Form2代码:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm2 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
UserMessage : UINT;
procedure GetImage(Form1Window : HWND);
public
{ Public declarations }
protected
procedure WndProc(var Message: TMessage); override;
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.FormCreate(Sender: TObject);
begin
UserMessage := RegisterWindowMessage('WM_SAMPLE');
end;
procedure TForm2.WndProc(var Message: TMessage);
begin
if Message.Msg = UserMessage then
GetImage(Message.WParam)
else inherited;
end;
procedure TForm2.GetImage(Form1Window : HWND);
var
Form1DC: HDc;
Form1Rect: TRect;
begin
image1.Picture := nil;
Form1DC := GetWindowDC(Form1Window);
GetWindowRect(Form1Window,Form1Rect);
image1.Width := Form1Rect.Right-Form1Rect.Left;
image1.Height := Form1Rect.Bottom - Form1Rect.Top;
BitBlt(Image1.Canvas.Handle,0,0,image1.Width,
image1.Height,
Form1DC, 0,
0, SRCCOPY);
Image1.Refresh;
ReleaseDC(Form1Window, Form1DC);
SetForegroundWindow(handle);
beep;
end;
end.