I created a small application that adds an item to the system menu of all windows, and handles clicking on it with a hook (WH_CALLWNDPROC
).
The code works when the window receives WM_SYSCOMMAND
messages with the existing items (minimize, maximize, etc.) but does not capture clicks on my custom item.
The problem is, apparently, that I'm not getting the correct ID of the item I added, but I don't know why.
How will I do this?
Here is the code to add the item (works):
const
SC_MYITEM = $020;
MYITEM_STRING = 'My Item';
...
function AddMyMenuItem(hWnd: HWND): Boolean;
var
WndMenu: HMENU;
begin
Result := False;
if IsWindowVisible(hWnd) then
begin
WndMenu := GetSystemMenu(hWnd, False);
if WndMenu <> 0 then
Result := AppendMenu(WndMenu, MF_STRING or MF_ENABLED, SC_MYITEM, PChar(MYITEM_STRING));
end;
end;
And this is the code in the dll file that the hook runs:
const
SC_MYITEM = $020;
function CallWndProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
Window: HWND;
begin
Result := CallNextHookEx(0, nCode, wParam, lParam);
if nCode < HC_ACTION then Exit;
if (PCWPStruct(lParam)^.message = WM_SYSCOMMAND) and (PCWPStruct(lParam)^.wParam = SC_MYITEM) then
begin
Window := PCWPStruct(lParam)^.hwnd;
//The procedure that processes the window...
end;
end;
As mentioned, if I check this line PCWPStruct(lParam)^.wParam = SC_MAXIMIZE
I will get the correct result when maximize is pressed, and the code will run.