我在被调用的dll中向调用者(EXE程序)SendMessage了一个消息(自定义的)可是我的EXE的Form好像没有收到
解决方案 »
- 繁體簡體的轉換
- 语法基本问题(菜鸟级),希望回答!
- 一个DBGRID的问题!!!!!!!
- 大家帮忙抉择~!
- 如何用DBGridEh进行自动排序?50分 急!
- 枚举类型范围
- 关于ftp的问题(急)
- 在ADOConnection的连接字符串中,如何方便的得到数据库中服务器的名字
- 请问TStringGrid的两个问题
- 小鸟问题:我用了ACCESS 数据库,在做安装盘时怎样打包DAO环境....
- 我的第一个程序欢迎大家给点意见(来者有分)
- 救救我吧!!用[NMFTP]下载文件,在ListItem事件里使用NMFtp.DownLoad(Listing,Listing)只能下载第一个文件,然后出错!!欲哭无泪啊!!
procedure WndProc(var Message: TMessage); override;
procedure TFormMain.WndProc(var Message: TMessage);
begin
case message.msg of
YOUR_DEFINE_MSG_CONST:
begin
ShowMessage('收到');
// 处理
end;
end;
inherited WndProc(message);
end;
2,你发消息的HANDLE怎么来的?一定正确吗?
3,你怎么发的消息啊?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls; const
SX_MY_CLOSE=WM_USER+100;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations } procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
end;var
Form1: TForm1;implementation
function myshow(Ahandle:Thandle;AForm:Thandle;ACaption:String):Longint; external 'mydll.dll';
procedure myclose(AFormRef:Longint);external 'mydll.dll';
{$R *.DFM}procedure TForm1.WndProc(var Message: TMessage);
begin
case message.msg of
SX_MY_CLOSE:
begin
ShowMessage('收到');
end;
end;
inherited WndProc(message);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tmp:Longint;
begin
tmp:=myshow(application.Handle,Form1.Handle,'test');
end;end.以下是dll部分代码unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations } public
{ Public declarations }
mhandle: Thandle;
melogint:Longint;
end;
const SX_MY_CLOSE = WM_USER+100;
function myshow(Ahandle:Thandle;Aform:Thandle;ACaption:String):Longint;
procedure myclose(AFormRef:Longint);implementation{$R *.DFM}function myshow(Ahandle:Thandle;Aform:Thandle;ACaption:String):Longint;
var
Form1: TForm1;
begin
Application.handle:=Ahandle;
Form1.mhandle:=Aform;
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Result:=Longint(Form1);
Form1.melogint:=Longint(Form1);
Form1.Show;
end;procedure myclose(AFormRef:Longint);
begin
if AFormRef>0 then
TForm1(AFormRef).Release;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(mhandle,SX_MY_CLOSE,0,0);
end;end.请指出错误,thanks
PostMessage(FindWindow(pchar('TForm1'), pchar('Form1')), SX_MY_CLOSE, 0, 0);应该没有问题了
function myshow(Ahandle:Thandle;Aform:Thandle;ACaption:String):Longint;
var
Form1: TForm1;
begin
Application.handle:=Ahandle;
Form1.mhandle:=Aform;
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Result:=Longint(Form1);
Form1.melogint:=Longint(Form1);
Form1.Show;
end;
应改为:function myshow(Ahandle:TApplication;Aform:Thandle;ACaption:String):Longint;
var
Form1: TForm1;
begin
Application:= Ahandle;
Form1.mhandle:=Aform;
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Result:=Longint(Form1);
Form1.melogint:=Longint(Form1);
Form1.Show;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(Application.MainForm.Handle,SX_MY_CLOSE,0,0);
end;
为什么我两个窗体都是TForm类的而且实例变量都是Form1他就能找到我的主窗体而不是dll的那个窗体呢
exe部分unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls; const
SX_MY_CLOSE=WM_USER+100;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
tmp:Longint;
procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
end;var
Form1: TForm1;implementation
function myshow(Ahandle:Thandle;AForm:Thandle;ACaption:String):Longint; external 'mydll.dll';
procedure myclose(AFormRef:Longint);external 'mydll.dll';
{$R *.DFM}procedure TForm1.WndProc(var Message: TMessage);
begin
case message.msg of
SX_MY_CLOSE:
begin
myclose(tmp);
tmp:=0;
end;
end;
inherited WndProc(message);
end;
procedure TForm1.Button1Click(Sender: TObject);begin
if tmp=0 then
tmp:=myshow(application.Handle,Form1.Handle,'test');
end;end.dll部分unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations } public
{ Public declarations }
mhandle: Thandle;
melogint:Longint;
end;
const SX_MY_CLOSE = WM_USER+100;
function myshow(Ahandle:Thandle;Aform:Thandle;ACaption:String):Longint;
procedure myclose(AFormRef:Longint);implementation{$R *.DFM}function myshow(Ahandle:Thandle;Aform:Thandle;ACaption:String):Longint;
var
Form1: TForm1;
begin
Application.handle:=Ahandle;
Form1.mhandle:=Aform;
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Result:=Longint(Form1);
Form1.melogint:=Longint(Form1);
Form1.Show;
end;procedure myclose(AFormRef:Longint);
begin
if AFormRef>0 then
TForm1(AFormRef).Release;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// SendMessage(Application.MainForm.Handle,SX_MY_CLOSE,0,0);
PostMessage(FindWindow(pchar('TForm1'), pchar('Form1')), SX_MY_CLOSE, 0, 0);
end;end.
不过有一点可以确定,就是当你在一个函数中对一个控件的属性赋值时好像可以在别的函数中看到,原因:我也不太清楚。你在消息处理时调用MyClose干什么?要关闭Dll的窗体吗?直接在Button1Click事件里加上Close就行了
Close;当你的主程序收到消息后将计数器减 1效果不一样吗?
两个进程间通信,用内存映像文件就可以了
CreateFileMapping();
MapViewOfFile();看一下Delphi的帮助。
你自定义的消息要注册成为系统消息,这样就可以了!呵呵:)注册消息API:
RegisterWindowMessage
发送消息API:
BroadcastSystemMessage这种消息可以跨进程传递。