0

the code so far installs a hook to detect the mouse activity but what i want is either to filter the activity for certain UI or to detect where the clicks has occurred (on which hwnd ) exactly 'Desktop' is there a way ?

this is the code i've used it's from Microsoft website here : How to set a Windows hook in Visual C# .NET

EDIT : i found that he code provided by is not global so for global hook check link in the answer the answer ,,

  • You pretty much have to know the HWND first then do your own filtering during your global callback. Use `GetDesktopWindow ()` to get HWND of Desktop ahead of time –  Jul 13 '15 at 02:29
  • thank you @Micky for your reply i already thought about using 'GetDesktioWindow()' to get the HWND but **where or how can i filter it during global callback ?** "first i tried to use 'WndProc()' but i found out it only receives messages passed to HWND i am using and nothing about desktop " – Ayad Kara Kâhya Jul 13 '15 at 09:16
  • No worries, as per the example install a WH_MOUSE and the HWND will be in the `MOUSEHOOKSTRUCT.hwnd` via the LPARAM of your `MouseHook()` callback –  Jul 13 '15 at 10:08
  • i found that this hook is not global and only works within the form am using it was so depressing for me should i edit the question ? i have to start from zero calling WH_MOUSE_LL instead Of WH_MOUSE – Ayad Kara Kâhya Jul 14 '15 at 01:54
  • Perhaps it's a limitation of .NET? Up to you. I always did global hooks from c/c++. Wishing you well –  Jul 14 '15 at 02:56
  • Dear @MickyDuncan i found that WH_MOUSE_LL struct have no HWND in it and the WH_MOUSE stuct (which is not global) have HWND that "according to Microsoft website :" hwnd Type: HWND A handle to the window that will receive the mouse message corresponding to the mouse event. what do you think – Ayad Kara Kâhya Jul 14 '15 at 17:45

1 Answers1

0

First about the hook i found that it's not global and i found open-source global hook Here so whenever the mouse click occurs an event will fire and there i run simple callback code that checks if the desktop is the active control

[DllImport("user32.dll")]
static extern int GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);

public void GetActiveWindow() {
const int maxChars = 256;
int handle = 0;
StringBuilder className = new StringBuilder(maxChars);

handle = GetForegroundWindow();

if (GetClassName(handle, className, maxChars) > 0) {
    string cName = className.ToString();
    if (cName == "Progman" || cName == "WorkerW") {
        // desktop is active
    } else {
        // desktop is not active
    }
}

}

DONE!

special thanks to Micky Duncaand and AJKenny84

Community
  • 1
  • 1