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 a WM message into a C++ class

Intercept a WM message into a C++ class

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
7 Posts 4 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.
  • 4 Offline
    4 Offline
    4288
    wrote on last edited by
    #1

    I was wondering how to intercept a WM_ specific message into a C++ class, I need to use the RegisterDeviceNotification function to register a specific WM_ message (I need a window here right?) and then intercept it and call the appropriate function The C++ class should be included in any project, from a console one to a win32 to a mfc one. My idea is to create the skeleton of a win32 window and then use the WndProc for that window, is this stupid? Is there any (surely) better solution?

    ---

    B K B 3 Replies Last reply
    0
    • 4 4288

      I was wondering how to intercept a WM_ specific message into a C++ class, I need to use the RegisterDeviceNotification function to register a specific WM_ message (I need a window here right?) and then intercept it and call the appropriate function The C++ class should be included in any project, from a console one to a win32 to a mfc one. My idea is to create the skeleton of a win32 window and then use the WndProc for that window, is this stupid? Is there any (surely) better solution?

      ---

      B Offline
      B Offline
      Baltoro
      wrote on last edited by
      #2

      You definitely need to have a Window Proc to recieve your message, because it is associated specifically with a registered Windows class. The documentation over at MSDN should show you how. See this: Windows Procedures Overview[^] You can also define your own custom Windows messages using a define, and by selecting a value above WM_USER.

      1 Reply Last reply
      0
      • 4 4288

        I was wondering how to intercept a WM_ specific message into a C++ class, I need to use the RegisterDeviceNotification function to register a specific WM_ message (I need a window here right?) and then intercept it and call the appropriate function The C++ class should be included in any project, from a console one to a win32 to a mfc one. My idea is to create the skeleton of a win32 window and then use the WndProc for that window, is this stupid? Is there any (surely) better solution?

        ---

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        an own WinProc is the generic solution. But be aware that it dont gets "spaghetti ocde" :wtf:

        Press F1 for help or google it. Greetings from Germany

        4 1 Reply Last reply
        0
        • K KarstenK

          an own WinProc is the generic solution. But be aware that it dont gets "spaghetti ocde" :wtf:

          Press F1 for help or google it. Greetings from Germany

          4 Offline
          4 Offline
          4288
          wrote on last edited by
          #4

          If I put the WinProc function in my class, it will need a hwnd handle I think. What handle am I supposed to give it? Should I require one to the application which is going to use my class? (this would exclude console apps, that's just a pity)

          ---

          K 1 Reply Last reply
          0
          • 4 4288

            I was wondering how to intercept a WM_ specific message into a C++ class, I need to use the RegisterDeviceNotification function to register a specific WM_ message (I need a window here right?) and then intercept it and call the appropriate function The C++ class should be included in any project, from a console one to a win32 to a mfc one. My idea is to create the skeleton of a win32 window and then use the WndProc for that window, is this stupid? Is there any (surely) better solution?

            ---

            B Offline
            B Offline
            Bacon Ultimate Cheeseburger
            wrote on last edited by
            #5

            One approach would be to store all message processing objects in a std::map<> container. Although this will only give you a 1 to 1 relationship between the expected window message and the object which handles it you'll have a nice start to begin expanding. When you receive the device notification message you can retrieve the object for that message and then call the appropriate method to handle the operation.

            // Call this from your window procedure
            void HandleDeviceMessage(UINT msg, WPARAM wParam, LPARAM lParam)
            {
            ProcObject *obj;
            std::map::iterator iter;

            iter = m\_DevNotifyMap.find(msg);
            if(iter == m\_DevNotifyMap.end()) {
                return; // no object available to handle this notification
            }
            
            iter->second->OnNotification(msg, wParam, lParam); 
            

            }

            I am a lean mean ground beef machine!!!

            1 Reply Last reply
            0
            • 4 4288

              If I put the WinProc function in my class, it will need a hwnd handle I think. What handle am I supposed to give it? Should I require one to the application which is going to use my class? (this would exclude console apps, that's just a pity)

              ---

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

              yes you need a hwnd for it. The main hwnd of the app is fine. If you havent a hwnd in your app, you must create one. It is common use to have in this cases an invisible (or minimized) window. Believe me   :-O

              Press F1 for help or google it. Greetings from Germany

              4 1 Reply Last reply
              0
              • K KarstenK

                yes you need a hwnd for it. The main hwnd of the app is fine. If you havent a hwnd in your app, you must create one. It is common use to have in this cases an invisible (or minimized) window. Believe me   :-O

                Press F1 for help or google it. Greetings from Germany

                4 Offline
                4 Offline
                4288
                wrote on last edited by
                #7

                Ok the invisible window seems the right way. Thank you all guys

                ---

                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