right clicking titlebar (Hooks/subclassing??)

I am trying to capture every time any titlebar is right-clicked. I can hook into the mouse, and so can capture any right click, but am having difficulty actually identifying what they have clicked on. I believe its WM_NCHITTEST that is needed, but I'm stuck, and really need a code example, so any offers? please??

Ah, sorry, I feel that I did not explain correctly what it was that I wanted to do, so I shall try again.

I wish to capture right click on a titlebar on any window in the entire system, not just my app.

The two solutions i can see, are to hook into the mouse and then when a right click is detected, subclass the active window and process the message just received, but I think that this may not work too well/at all.

The second is to hook to receive WM_NCHITEST, and then process from there, but I do not know what to do about mouse buttons here.

My attempts at subclassing have failed, but I would prefer to use hooks, since I imagine it would be more reliable.

Ah, yes, now I understand. Well, Brad usually writes really good software but the hook sample he wrote is, ...., uhm, ...., suboptimal ;-) Despite the fact that many stuff applies only to Win3.1 (e.g. task identifier in SetWindowHookEx) he also ignored one of the most important fact to consider when writing hooks. All variables (including the global hook handle declared in the units) are specific to the process the hook has been activated in. So when the mouse hook callback is triggered for another process than the one which installed the hook ALL these variables are uninitialized and invalid!

Here's correct code to do the hook:

library Hook;

uses
Windows, SysUtils;

type
// shared memory area used by the DLL in different process contexts (loacted in memory mapped file)
PHookInfo = ^THookInfo;
THookInfo = record
MouseHook, // general mouse messages
SynchEvent: THandle; // used to synchronize main program and hook DLL
Windows9x: Boolean;
end;

begin
Result := True;
// create shared memory area to use some global variables for all process contexts the DLL can be called in
InternalMapping := CreateFileMapping(DWORD(-1), nil, PAGE_READWRITE, 0, SizeOf(THookInfo), InternalFileMappingName);
if DictInternalMapping <> 0 then
begin
// the shared memory will be mapped for the entire lifetime of the DLL (it needs really not much memory)
HookInfo := MapViewOfFile(InternalMapping, FILE_MAP_READ or FILE_MAP_WRITE, 0, 0, SizeOf(THookInfo));
if Assigned(HookInfo) then
begin
with HookInfo^ do
begin
// Retrieve handle to synchronization event. This handle needs not to be freed from here and must be
// created by the main application. The event must be created initially set as this is the state which
// indicates free operation for the hook.
SynchEvent := OpenEvent(EVENT_ALL_ACCESS, False, HookEvent);
if SynchEvent = 0 then Result := False; // show that something went wrong
Windows9x := (Win32Platform and VER_PLATFORM_WIN32_NT) = 0;
end;
end
else Result := False; // show that something went wrong
end
else Result := False; // show that something went wrong
end;

begin
if GetSharedData then
begin
// check if we have still a pending message to swallow
if (HookInfo.SwallowMessage = wParam) then
begin
HookInfo.SwallowMessage := -1;
if Code < 0 then Result := DoDefault
else Result := 1;
end
else
begin
// A timeout value of 0 means not to wait for the event but to check it.
// An unsignaled event means there were no results before or the main application has already
// processed the last result. The event must manually be reset by the application!
IsEvent := (WaitForSingleObject(HookInfo.SynchEvent, 0) = WAIT_TIMEOUT) and
((wParam = WM_MBUTTONDOWN) or (wParam = WM_NCMBUTTONDOWN));

if IsEvent and (GetAsyncKeyState(VK_CONTROL) < 0) then
begin
with PMouseHookStruct(lParam)^ do
if HookInfo.Windows9x then DoWin9xStuff(hwnd, Pt)
else DoWinNTStuff(hwnd, Pt);
if Code < 0 then Result := DoDefault
else Result := 1;
end
else Result := DoDefault;
end;
end
else Result := 1;
ReleaseSharedData;
end;

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Thanks for the code Mike. Please could you explain a little what DictInternalMapping and SwallowMessage do/are, since these mean it won't compile, I guess the second is whether or not to pass on the message, but the first has stumped me completely.

Oops, sorry. I copied the code out from one of my projects and have obviously not changed all references. Please, just change DictInternalMapping to InternalMapping (there is a variable declaration). The SwallowMesage is quite simple. Sometimes it is important not to handle a particular message when a previous one has been picked up by the hook. E.g. mouse down triggers in my case a popup window, so this message doesn't go through to the target application. But because mouse down doesn't come through I don't want mouse up to come through, either.

Firstly, my computer with delphi on is inaccessable for a few days, so I'm trying just to follow the code by eye (and memory), so I hope I don't say anything to obviously stupid..

So SwallowMessage should be defined in THookInfo as Integer, or wParam or something similar?

Also for swallowmessage it is set to -1 at one point, but never cleared or anything else. -1 is mouseup? why is it never cleared?

I want detect WM_NCRBUTTONDOWN which is what is sent when the user right clicks the titlebar, and then swallow the WM_RBUTTONUP which follows (and [possibly] triggers the menu). I do not understand why Windows sends WM_RBUTTONUP and not WM_NCRBUTTONUP, its very annoying (plus it means hittest is htClient and not htCaption, also a pain!). But anyway, if you can explain these things, and show me what to do, the points are all yours (I've upped it to the amount I have - I really am grateful for your help)

SwallowMessage is indeed defined as Integer so setting it to -1 is possible (which denotes no pending message) and makes sure it does not represent a valid message as this value is compared with the incoming message in MouseHookProc, in this line:

if (HookInfo.SwallowMessage = wParam) then ...

and reset if it applies. Furthermore, no redirection of the message is done so it never reaches the target window.

Featured Post

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Introduction
Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview. I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work. Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message. In the To field, type your recipient's fax number @efaxsend.com.
You can even send a secure international fax — just include t…

In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…