码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

-------------------------D7-

仅仅是学习,不要太较真,哈哈

资料来源忘记了

 

--------------------------------

 

-----------unit开始

-------------------------------MyCustomForm_u开始--------------

unit MyCustomForm_u;

interface
uses Windows,Classes,Messages;

const MY_MESSAGE = WM_USER + 1;

type
TMyCustomForm=class(TComponent)
private
FHandle: HWND;
procedure MainWndProc(var Message: TMessage);// message MY_MESSAGE;{自定义一个窗口过程}
procedure DoMY_MESSAGE(var Msg: TMessage); message MY_MESSAGE;
public
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
published
property Handle: HWND read FHandle; {发布一个Handle属性,这样别人可 以给它发送消息}
end;

implementation

{ TMyCustomForm }

constructor TMyCustomForm.Create(AOwner: TComponent);
begin
inherited;
FHandle := Classes.AllocateHWnd(MainWndProc);

{AllocateHWnd是Classes单元的全局过程,需要一个TWndMethod类型的参数(即一个窗
口过程);其作用是创建一个不可见窗口并将参数传入的窗口过程关联于它,返回所创建窗
口的句柄}
end;

destructor TMyCustomForm.Destroy;
begin
if FHandle <> 0 then
Classes.DeallocateHWnd(FHandle); {销毁已创建的窗口}
inherited;
end;

procedure TMyCustomForm.DoMY_MESSAGE(var Msg: TMessage);
begin
Messagebox(0,'接到自定义消息0','自定义',MB_OK);
end;

procedure TMyCustomForm.MainWndProc(var Message: TMessage);
begin
{if Message.Msg = WM_CLOSE then
MessageBox(0, '收到消息WM_CLOSE', '收到消息', MB_OK)
else
DefaultHandler(Message);
if Message.Msg=MY_MESSAGE then
Messagebox(0,'接到自定义消息1','自定义',MB_OK); }
Dispatch(Message);
end;

end.

 

--------------------------------MyCustomForm_u结束------------

 

---------------Unit1开始--------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

const MY_MESSAGE_01 = WM_USER + 11;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure DoMY_MESSAGE(var Msg: TMessage); message MY_MESSAGE_01;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses MyCustomForm_u;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
DemoMsgCom: TMyCustomForm;
begin
DemoMsgCom := TMyCustomForm.Create(nil);
SendMessage(DemoMsgCom.Handle, MY_MESSAGE, 0, 0);
SendMessage(DemoMsgCom.Handle, WM_CLOSE, 0, 0);
FreeAndNil(DemoMsgCom);
end;

procedure TForm1.DoMY_MESSAGE(var Msg: TMessage);
begin
Messagebox(0,'接到自定义消息','自定义',MB_OK);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SendMessage(self.Handle, MY_MESSAGE_01, 0, 0);
end;

end.

 

-----------------Unit1结束----------------------

 

 

 

 

---------------------------Unit结束

------------Form

object Form1: TForm1
Left = 918
Top = 676
Width = 259
Height = 256
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 80
Top = 40
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 80
Top = 112
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
end

--------------Form

 

posted on 2021-04-07 15:26  码农的笔记  阅读(210)  评论(0编辑  收藏  举报