Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Intercept all mouse event for a specified hwnd

Intercept all mouse event for a specified hwnd

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
24 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kcynic
    wrote on last edited by
    #1

    I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

    LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(0 > nCode)
    return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

    //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
    MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
    DWORD process\_id;
    GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
    if(process\_id==target\_process\_id){//is that process to be hooked
    	.... my process
    	return 1;//or return 0
    }
       return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
    

    }

    that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

    G C M M 5 Replies Last reply
    0
    • K kcynic

      I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

      LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if(0 > nCode)
      return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

      //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
      MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
      DWORD process\_id;
      GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
      if(process\_id==target\_process\_id){//is that process to be hooked
      	.... my process
      	return 1;//or return 0
      }
         return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
      

      }

      that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      what happens if you dont call CallNextHookEx in the return statement ? (ie remove the CallNextHookEx and return [something else]) ... it occurs to me that is what is propagating the message to the next handler. 'g'

      K 1 Reply Last reply
      0
      • K kcynic

        I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

        LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
        {
        if(0 > nCode)
        return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

        //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
        MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
        DWORD process\_id;
        GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
        if(process\_id==target\_process\_id){//is that process to be hooked
        	.... my process
        	return 1;//or return 0
        }
           return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
        

        }

        that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Are you sure your hook is being called?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        K 1 Reply Last reply
        0
        • C Code o mat

          Are you sure your hook is being called?

          > The problem with computers is that they do what you tell them to do and not what you want them to do. <

          K Offline
          K Offline
          kcynic
          wrote on last edited by
          #4

          of courese im sure. i place a messagebox at the if block, the messagebox will display there.

          C 1 Reply Last reply
          0
          • K kcynic

            I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

            LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
            {
            if(0 > nCode)
            return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

            //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
            MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
            DWORD process\_id;
            GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
            if(process\_id==target\_process\_id){//is that process to be hooked
            	.... my process
            	return 1;//or return 0
            }
               return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
            

            }

            that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

            M Offline
            M Offline
            Malli_S
            wrote on last edited by
            #5

            You are calling CallNextHookEx()[^] in both the case i.e. nCode > 0 or nCode < 0; You may return a nonzero value to prevent the system from passing the message to the target window procedure from your handler. Even have a look at this[^].

            [Delegates]      [Virtual Desktop]      [Tray Me !]
            -Malli...! :rose:****

            C K 2 Replies Last reply
            0
            • G Garth J Lancaster

              what happens if you dont call CallNextHookEx in the return statement ? (ie remove the CallNextHookEx and return [something else]) ... it occurs to me that is what is propagating the message to the next handler. 'g'

              K Offline
              K Offline
              kcynic
              wrote on last edited by
              #6

              you means the last? if the message is in the window handle i want to handle, the last CallNextHookEx will not be called.

              1 Reply Last reply
              0
              • M Malli_S

                You are calling CallNextHookEx()[^] in both the case i.e. nCode > 0 or nCode < 0; You may return a nonzero value to prevent the system from passing the message to the target window procedure from your handler. Even have a look at this[^].

                [Delegates]      [Virtual Desktop]      [Tray Me !]
                -Malli...! :rose:****

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #7

                But here he does exactly that, not call CallNextHookEx and return non-zero:

                if(process\_id==target\_process\_id){//is that process to be hooked
                	.... my process
                	return 1;//or return 0
                }
                

                Am i missing something?

                > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                M 1 Reply Last reply
                0
                • K kcynic

                  of courese im sure. i place a messagebox at the if block, the messagebox will display there.

                  C Offline
                  C Offline
                  Code o mat
                  wrote on last edited by
                  #8

                  And does your process-id comparison become TRUE at the right times?

                  > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                  K 1 Reply Last reply
                  0
                  • C Code o mat

                    But here he does exactly that, not call CallNextHookEx and return non-zero:

                    if(process\_id==target\_process\_id){//is that process to be hooked
                    	.... my process
                    	return 1;//or return 0
                    }
                    

                    Am i missing something?

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                    M Offline
                    M Offline
                    Malli_S
                    wrote on last edited by
                    #9

                    Sorry ! I missed it ! It's my coffee time ! :sigh:

                    [Delegates]      [Virtual Desktop]      [Tray Me !]
                    -Malli...! :rose:****

                    1 Reply Last reply
                    0
                    • K kcynic

                      I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

                      LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
                      {
                      if(0 > nCode)
                      return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

                      //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
                      MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
                      DWORD process\_id;
                      GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
                      if(process\_id==target\_process\_id){//is that process to be hooked
                      	.... my process
                      	return 1;//or return 0
                      }
                         return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
                      

                      }

                      that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

                      M Offline
                      M Offline
                      Malli_S
                      wrote on last edited by
                      #10

                      Does GetWindowThreadProcessId return proper process id ? The only possibility is your if(process_id==target_process_id) is not satisfying. Check whether you've defined the shared section properly, and even look for values of target_process_id and process_id by debug printing them.

                      [Delegates]      [Virtual Desktop]      [Tray Me !]
                      -Malli...! :rose:****

                      K 1 Reply Last reply
                      0
                      • C Code o mat

                        And does your process-id comparison become TRUE at the right times?

                        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                        K Offline
                        K Offline
                        kcynic
                        wrote on last edited by
                        #11

                        yes, im sure. because i instore the target process' process id in a shared segment. And i also has placed a messagebox at that if block for testing.

                        C 1 Reply Last reply
                        0
                        • M Malli_S

                          You are calling CallNextHookEx()[^] in both the case i.e. nCode > 0 or nCode < 0; You may return a nonzero value to prevent the system from passing the message to the target window procedure from your handler. Even have a look at this[^].

                          [Delegates]      [Virtual Desktop]      [Tray Me !]
                          -Malli...! :rose:****

                          K Offline
                          K Offline
                          kcynic
                          wrote on last edited by
                          #12

                          you can see, if nCode>0, to the target process(other processes are not what i want to deal with) the flow will enter the if block, so the second CallNextHookEx will never be called.

                          1 Reply Last reply
                          0
                          • K kcynic

                            yes, im sure. because i instore the target process' process id in a shared segment. And i also has placed a messagebox at that if block for testing.

                            C Offline
                            C Offline
                            Code o mat
                            wrote on last edited by
                            #13

                            Well, the documentation says that returning a non-zero value MIGHT prevent the system from letting the message go on his marry way. So i guess there's no assurance. How about altering the MOUSEHOOKSTRUCT, for example, setting hwnd to NULL before you return?

                            > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                            K 1 Reply Last reply
                            0
                            • M Malli_S

                              Does GetWindowThreadProcessId return proper process id ? The only possibility is your if(process_id==target_process_id) is not satisfying. Check whether you've defined the shared section properly, and even look for values of target_process_id and process_id by debug printing them.

                              [Delegates]      [Virtual Desktop]      [Tray Me !]
                              -Malli...! :rose:****

                              K Offline
                              K Offline
                              kcynic
                              wrote on last edited by
                              #14

                              yes, target_process_id do be defined in a shared segment:

                              #pragma data_seg (".SHARED")
                              HHOOK g_hPreviousMouseHook = 0;
                              HHOOK g_hPreviousWinProcHook = 0;
                              HINSTANCE g_hInstance = 0;
                              HWND g_hMinimizedWindowList[ARRAY_SIZE] = {0};
                              int g_iMinimizedWindowCount = 0;
                              DWORD target_process_id = 0;
                              #pragma data_seg()

                              #pragma comment(linker, "/SECTION:.SHARED,RWS")

                              Malli_S, this code slice is copied from your tutor article http://www.codeproject.com/KB/system/TrayMe.aspx. Im trying your demo project today.

                              M 1 Reply Last reply
                              0
                              • C Code o mat

                                Well, the documentation says that returning a non-zero value MIGHT prevent the system from letting the message go on his marry way. So i guess there's no assurance. How about altering the MOUSEHOOKSTRUCT, for example, setting hwnd to NULL before you return?

                                > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                                K Offline
                                K Offline
                                kcynic
                                wrote on last edited by
                                #15

                                No. My target test program is a simple mfc dialog with a tray icon, when you right-click the tray there will be a popup menu display. I want to hook the mouse messages so that the menu will not appear on the screen. No matter i return which value or change the hwnd member of MOUSEHOOKSTRUCT to NULL, the menu will always display, the difference is that, this menu will not receive messages any more.

                                C 1 Reply Last reply
                                0
                                • K kcynic

                                  No. My target test program is a simple mfc dialog with a tray icon, when you right-click the tray there will be a popup menu display. I want to hook the mouse messages so that the menu will not appear on the screen. No matter i return which value or change the hwnd member of MOUSEHOOKSTRUCT to NULL, the menu will always display, the difference is that, this menu will not receive messages any more.

                                  C Offline
                                  C Offline
                                  Code o mat
                                  wrote on last edited by
                                  #16

                                  Ah, you won't get mouseclick events from an icon clicked on the tray, that works differently. See here[^]. You can specify what message your application wants to receive if the icon gets clicked on the tray. You should probably look for that message instead of mouseclicks. You should have said that earlier. :)

                                  > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                                  K 1 Reply Last reply
                                  0
                                  • C Code o mat

                                    Ah, you won't get mouseclick events from an icon clicked on the tray, that works differently. See here[^]. You can specify what message your application wants to receive if the icon gets clicked on the tray. You should probably look for that message instead of mouseclicks. You should have said that earlier. :)

                                    > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                                    K Offline
                                    K Offline
                                    kcynic
                                    wrote on last edited by
                                    #17

                                    but, if so, i want to hook such message, what should i do?

                                    C 1 Reply Last reply
                                    0
                                    • K kcynic

                                      but, if so, i want to hook such message, what should i do?

                                      C Offline
                                      C Offline
                                      Code o mat
                                      wrote on last edited by
                                      #18

                                      Well, first you need to determine what that message is, as said, the application can TELL the shell what it wants to receive so bascily it can be anything. Try using spy++, maybe it helps, maybe it does not.

                                      > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                                      K 1 Reply Last reply
                                      0
                                      • K kcynic

                                        I want to hook a mouse events of a specified window. So, i install a WH_MOUSE hook callback procedure for that thread. If i didn't hook that message, the thread would show a pop menu when it received a right button message. After i hooked that thread, windows would call my callback procedure when such event occurred, but the original window's message handler would be called, too. But i want to hold such message to make the original window will not receive such message. I use global hooks but check the process id like:

                                        LRESULT CALLBACK MouseHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
                                        {
                                        if(0 > nCode)
                                        return CallNextHookEx(g_hPreviousMouseHook, nCode, wParam, lParam);

                                        //OutputDebugString(\_T("Mouse Hooked Event...\\n"));
                                        MOUSEHOOKSTRUCT \*pMouseHooksStruct = (MOUSEHOOKSTRUCT \*) lParam;
                                        DWORD process\_id;
                                        GetWindowThreadProcessId(pMouseHooksStruct->hwnd,&process\_id);
                                        if(process\_id==target\_process\_id){//is that process to be hooked
                                        	.... my process
                                        	return 1;//or return 0
                                        }
                                           return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam, lParam);
                                        

                                        }

                                        that, target_process_id is stored in the shared segment block, im sure its ok. How to fix such a problem? Regards.

                                        M Offline
                                        M Offline
                                        Mark Salsbery
                                        wrote on last edited by
                                        #19

                                        A mousehook seems like an overkill solution for getting all the mouse events for a window. Why not just get the messages in the window's windowproc (possibly by subclassing if it's not your window)? Or is the window in another app? Mark

                                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                                        K 1 Reply Last reply
                                        0
                                        • C Code o mat

                                          Well, first you need to determine what that message is, as said, the application can TELL the shell what it wants to receive so bascily it can be anything. Try using spy++, maybe it helps, maybe it does not.

                                          > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                                          K Offline
                                          K Offline
                                          kcynic
                                          wrote on last edited by
                                          #20

                                          ok. i used spy++ and found the tray icon always receives TB_GETBUTTONINFO message. and i googled found someone said i almost couldn't hook that message in its owner window's message loop, because TB_GETBUTTONINFO is internal messages in controls. right? If so, maybe i only can hook its owner window's main message callback procedure. any idea?

                                          C 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups