我寫了一個 DLL 用來實現全區的消息hook,我想在接收到某個消息時在托盤上顯示一個tray icon,而且在鼠標在icon 上右鍵時可以顯示popup menu。我先將icon放在res文件中,然後在DLL初始化時讀入來,在顯示時用 Shell_NotifyIcon 來顯示。似乎一齊都很順利,但是當鼠標移到tray 上的icon時,icon就消失了。我的DLL代碼大致如下(這是Delphi的,我想改成VC的,但不太熟VC,還不知道怎麼改)
////////////////////////////////////////////////////////////////
// HookDll Project library HookDll;uses
SysUtils,
Classes,
Messages,
Graphics, ShellAPI,
windows;const
WM_MYICONTRAY = WM_USER + 1121 ;var
CurrentHook: HHook;
FIcon :array[1..2] of HICON;
TrayIconData: TNotifyIconData;{$R HookDll.RES}procedure MyDLLHandler(Reason: integer);
begin
case Reason of
DLL_Process_Attach:
begin
try
// 讀入 ICON
FIcon[1] := LoadIcon(hInstance, 'MAINICON');
FIcon[2] := LoadIcon(hInstance, 'GRAYICON');
With TrayIconData do
begin
cbSize := SizeOf(TrayIconData);
Wnd := hInstance;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_MYICONTRAY; // 這裡不知怎樣處理 call back
hIcon := FIcon[1];
end; except
end;
end;
DLL_Process_Detach:
begin
end;
end;
end;function GlobalMsgHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
begin
if code<0 then begin
GlobalMsgHook := CallNextHookEx(CurrentHook,code,wParam,lparam); //then return the value from it.
Exit;
end; if (...) then // 如果得得某個消息,則顯示tray
begin
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;
Result := CallNextHookEx(CurrentHook, code, wParam, lparam); //call the next hook proc if there is one
//Exit;
end;
exports GlobalMsgHook index 1,
SetHookHandle index 2;
begin
DLLProc := @MyDLLHandler;
MyDLLHandler(DLL_Process_Attach);
end.
////////////////////////////////////////////////////////////////
// HookDll Project library HookDll;uses
SysUtils,
Classes,
Messages,
Graphics, ShellAPI,
windows;const
WM_MYICONTRAY = WM_USER + 1121 ;var
CurrentHook: HHook;
FIcon :array[1..2] of HICON;
TrayIconData: TNotifyIconData;{$R HookDll.RES}procedure MyDLLHandler(Reason: integer);
begin
case Reason of
DLL_Process_Attach:
begin
try
// 讀入 ICON
FIcon[1] := LoadIcon(hInstance, 'MAINICON');
FIcon[2] := LoadIcon(hInstance, 'GRAYICON');
With TrayIconData do
begin
cbSize := SizeOf(TrayIconData);
Wnd := hInstance;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_MYICONTRAY; // 這裡不知怎樣處理 call back
hIcon := FIcon[1];
end; except
end;
end;
DLL_Process_Detach:
begin
end;
end;
end;function GlobalMsgHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
begin
if code<0 then begin
GlobalMsgHook := CallNextHookEx(CurrentHook,code,wParam,lparam); //then return the value from it.
Exit;
end; if (...) then // 如果得得某個消息,則顯示tray
begin
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;
Result := CallNextHookEx(CurrentHook, code, wParam, lparam); //call the next hook proc if there is one
//Exit;
end;
exports GlobalMsgHook index 1,
SetHookHandle index 2;
begin
DLLProc := @MyDLLHandler;
MyDLLHandler(DLL_Process_Attach);
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货