const First_My_Msg= WM_USER+10;
type TMyMsg=(
my_msg3, // = WM_USER+10,
my_msg4,
my_msg5);
type
TForm1 = class(TForm)
lst1: TListBox;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
procedure showMsg(msg: string);
{ Public declarations }
protected
procedure WndProc(var msg:TMessage);override;
end;
const my_msg = WM_USER + 1;
const my_msg2= WM_USER + 2;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure myCallback(hWnd: THandle; MsgId:Cardinal; dwData: Integer;lResult:DWORD);stdcall;
var
astr: array[0..MAX_PATH - 1] of AnsiChar;
begin
GetWindowText(hWnd, @astr, MAX_PATH);
Form1.showMsg(Format('handle: %d, msgid: %s, dwData: %d, result: %d, window text: %s',
[hWnd, GetEnumName(TypeInfo(TMyMsg), MsgId-First_My_Msg), dwData, lResult, astr])); //
end;
{ TForm1 }
procedure TForm1.WndProc(var msg: TMessage);
begin
inherited;
case msg.Msg of
my_msg:
showMsg('my message');
my_msg2:
showMsg('my message2');
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
SendMessage(Self.Handle, my_msg, 0, 0);
end;
procedure TForm1.showMsg(msg: string);
begin
lst1.Items.Add(Format('[%s] %s', [FormatDateTime('hh:mm:ss', now), msg]));
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
// PostMessage(Self.Handle, my_msg2, 0, 0); 中文
SendMessageCallback(Self.Handle, Cardinal(my_msg4)+First_My_Msg, 0, 0, @myCallback, 123);
end;
end.
自定义消息发送与处理及获取枚举名称
最新推荐文章于 2025-01-12 16:22:26 发布