问题一波接一波,消息机制下怎样才能让窗体只创建一次,销毁一次。
DLL里面利用消息 创建窗体的部分代码
Function GetMsgProc(icode: Integer;wp: WPARAM;lp: LPARAM):LRESULT;stdcall;
begin
if (icode=HC_ACTION) then
begin
if (PMSG(lp)^.message=WM_USER+600) and ((1 shl 31)and lp=0) then //MessageBox(0,'显示','显示',0);
begin
if form1=nil then------------这里会多次创建窗体
Form1:=Tform1.Create(nil); //应该是在这里修改 但是具体不知道怎么修改
form1.Visible:=not form1.Visible; //希望有高人指点下这个是什么意思
end;
end;
Result:=CallNextHookEx(keyhhk,icode,wp,lp);
end;
Function SetHook(hwid:Integer):boolean;stdcall;//这个函数是导出表函数,接受传递的参数
var
GameTid:THandle;
h:HWND;
begin
Result:=false;
h:=hwid;
if (windows.IsWindow(h)=false) then
begin
Messagebox(0,'无效窗口句柄','error',0);
exit;
end;
GameTid:=GetWindowThreadProcessId(h);
if GameTid=0 then
begin
Messagebox(0,'无法获得线程ID','error',0);
exit;
end;
if GameTid<>0 then
begin
keyhhk:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,GetModuleHandle('dllGame.dll'),GameTid);
end;
if keyhhk>0 then
Result:=true;
end;
procedure DllEnterProc(reason:integer);
begin
case reason of
windows.DLL_PROCESS_ATTACH: begin end;
windows.DLL_PROCESS_DETACH: begin Form1.Free;form1:=nil; end;
end;
end;
if SrvIP.Selected <> nil then
begin
nowpid:=StrToCard(SrvIP.Selected.Caption);
end;
nowwinid:=GetHWndByPID(nowpid);
SwitchToThisWindow(nowwinid,true);
sleep(50);
if nowwinid<>0 then
begin
PostMessage(nowwinid,WM_USER+600,0,0);//呼出或者隐藏窗体
end;