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. WM_KILLFOCUS problem

WM_KILLFOCUS problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
22 Posts 4 Posters 1 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.
  • D danginkgo

    In the main window callback function I get the WM_KILLFOCUS message when I click on the window, and only then I get the WM_SETFOCUS message. Is that OK? I thought that WM_KILLFOCUS message is sent to a window immediatly before it looses focus. I want to register some values when the window has keyboard focus and unregister the values when the window looses focus. How can I do that?

    danginkgo

    S Offline
    S Offline
    SandipG
    wrote on last edited by
    #2

    do you have child controls on your main window??

    Regards, Sandip.

    D 1 Reply Last reply
    0
    • S SandipG

      do you have child controls on your main window??

      Regards, Sandip.

      D Offline
      D Offline
      danginkgo
      wrote on last edited by
      #3

      I have child controls on the main window.

      danginkgo

      S 1 Reply Last reply
      0
      • D danginkgo

        I have child controls on the main window.

        danginkgo

        S Offline
        S Offline
        SandipG
        wrote on last edited by
        #4

        then probably WM_KILLFOCUS is for one of the child window which was having focus earlier. Check the vlaue of wParam in WM_KILLFOCUS. It is a handle to new window which will get the focus. You can verify that WM_KILLFOCUS was not for your main window by comparing wParam with your main window handle. And to find which window lost the focus check value of wParam in WM_SETFOCUS.

        Regards, Sandip.

        D 2 Replies Last reply
        0
        • D danginkgo

          In the main window callback function I get the WM_KILLFOCUS message when I click on the window, and only then I get the WM_SETFOCUS message. Is that OK? I thought that WM_KILLFOCUS message is sent to a window immediatly before it looses focus. I want to register some values when the window has keyboard focus and unregister the values when the window looses focus. How can I do that?

          danginkgo

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

          Check from which window you get these messages. :doh:

          Greetings from Germany

          1 Reply Last reply
          0
          • S SandipG

            then probably WM_KILLFOCUS is for one of the child window which was having focus earlier. Check the vlaue of wParam in WM_KILLFOCUS. It is a handle to new window which will get the focus. You can verify that WM_KILLFOCUS was not for your main window by comparing wParam with your main window handle. And to find which window lost the focus check value of wParam in WM_SETFOCUS.

            Regards, Sandip.

            D Offline
            D Offline
            danginkgo
            wrote on last edited by
            #6

            OK, but if the main window loses focus, doesn't it get the WM_KILLFOCUS message? I just have to unregister some hotkeys if the window loses focus. How can I do that?

            danginkgo

            1 Reply Last reply
            0
            • S SandipG

              then probably WM_KILLFOCUS is for one of the child window which was having focus earlier. Check the vlaue of wParam in WM_KILLFOCUS. It is a handle to new window which will get the focus. You can verify that WM_KILLFOCUS was not for your main window by comparing wParam with your main window handle. And to find which window lost the focus check value of wParam in WM_SETFOCUS.

              Regards, Sandip.

              D Offline
              D Offline
              danginkgo
              wrote on last edited by
              #7

              How can I know when the main window loses focus?

              danginkgo

              S 1 Reply Last reply
              0
              • D danginkgo

                How can I know when the main window loses focus?

                danginkgo

                S Offline
                S Offline
                SandipG
                wrote on last edited by
                #8

                I dont have much idea about SDK, but Try this I hope it works, WM_SETFOCUS: if the wParam is handle to your main window then it means that your window has lost the focus. (Unregister ur hot keys) WM_KILLFOCUS: if wParam is handle to your main window then it means that your window is going to get the focus (register your hot keys). If this doesnt work may be some one can correct as i am really not sure. Ohh I forgot about HWND parameter of WindowProc. I guess It will point to your main window whenever whenever WM_KILLFOCUS will be generated for it and similarly for SetFocus.

                Regards, Sandip.

                modified on Tuesday, June 24, 2008 5:07 AM

                D 1 Reply Last reply
                0
                • S SandipG

                  I dont have much idea about SDK, but Try this I hope it works, WM_SETFOCUS: if the wParam is handle to your main window then it means that your window has lost the focus. (Unregister ur hot keys) WM_KILLFOCUS: if wParam is handle to your main window then it means that your window is going to get the focus (register your hot keys). If this doesnt work may be some one can correct as i am really not sure. Ohh I forgot about HWND parameter of WindowProc. I guess It will point to your main window whenever whenever WM_KILLFOCUS will be generated for it and similarly for SetFocus.

                  Regards, Sandip.

                  modified on Tuesday, June 24, 2008 5:07 AM

                  D Offline
                  D Offline
                  danginkgo
                  wrote on last edited by
                  #9

                  I tried but it doesn't work. For WM_SETFOCUS and WM_KILLFOCUS , the wParam is not the main window HWND. Ex. wParam = 0x000209fc This->hwndView = 0x000209ee hWnd = 0x000209ee (HWND parameter of WindowProc) So, I can't register and unregister if I get those messages.

                  danginkgo

                  S 1 Reply Last reply
                  0
                  • D danginkgo

                    I tried but it doesn't work. For WM_SETFOCUS and WM_KILLFOCUS , the wParam is not the main window HWND. Ex. wParam = 0x000209fc This->hwndView = 0x000209ee hWnd = 0x000209ee (HWND parameter of WindowProc) So, I can't register and unregister if I get those messages.

                    danginkgo

                    S Offline
                    S Offline
                    SandipG
                    wrote on last edited by
                    #10

                    did you manage to get it working it or not can you paste some code snippets for the same.

                    Regards, Sandip.

                    D 3 Replies Last reply
                    0
                    • S SandipG

                      did you manage to get it working it or not can you paste some code snippets for the same.

                      Regards, Sandip.

                      D Offline
                      D Offline
                      danginkgo
                      wrote on last edited by
                      #11

                      This is the callback function for the main (view) window: LRESULT CALLBACK FldViewWndProc (HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam){ PluginInstance* This; ................................................ case WM_KILLFOCUS: This = (PluginInstance*) GetWindowLong(hWnd, 0); if((HWND)wParam==This->hwndView) register hotkeys break; case WM_SETFOCUS: This = (PluginInstance*) GetWindowLong(hWnd, 0); if((HWND)wParam==This->hwndView) unregister hotkeys break; .................................................. }

                      danginkgo

                      S 1 Reply Last reply
                      0
                      • D danginkgo

                        This is the callback function for the main (view) window: LRESULT CALLBACK FldViewWndProc (HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam){ PluginInstance* This; ................................................ case WM_KILLFOCUS: This = (PluginInstance*) GetWindowLong(hWnd, 0); if((HWND)wParam==This->hwndView) register hotkeys break; case WM_SETFOCUS: This = (PluginInstance*) GetWindowLong(hWnd, 0); if((HWND)wParam==This->hwndView) unregister hotkeys break; .................................................. }

                        danginkgo

                        S Offline
                        S Offline
                        SandipG
                        wrote on last edited by
                        #12

                        are you getting the same values in both cases you mentioned in your previous reply.

                        Regards, Sandip.

                        D 1 Reply Last reply
                        0
                        • S SandipG

                          did you manage to get it working it or not can you paste some code snippets for the same.

                          Regards, Sandip.

                          D Offline
                          D Offline
                          danginkgo
                          wrote on last edited by
                          #13

                          In the documentation I found that the wParam for WM_KILLFOCUS is the window handler for the window that receive focus, so I'll get this message in the WindowProc function if I click on the main window. So, I can't use this message, because I want to unregister the hotkeys when the main window loses focus, not immediately before it gains focus. Is it wright what I wrote?

                          danginkgo

                          modified on Tuesday, June 24, 2008 7:31 AM

                          1 Reply Last reply
                          0
                          • S SandipG

                            are you getting the same values in both cases you mentioned in your previous reply.

                            Regards, Sandip.

                            D Offline
                            D Offline
                            danginkgo
                            wrote on last edited by
                            #14

                            yes, the same values

                            danginkgo

                            1 Reply Last reply
                            0
                            • S SandipG

                              did you manage to get it working it or not can you paste some code snippets for the same.

                              Regards, Sandip.

                              D Offline
                              D Offline
                              danginkgo
                              wrote on last edited by
                              #15

                              The problem is I don't get any message in WindowProc if the window loses focus.

                              danginkgo

                              S 1 Reply Last reply
                              0
                              • D danginkgo

                                The problem is I don't get any message in WindowProc if the window loses focus.

                                danginkgo

                                S Offline
                                S Offline
                                SandipG
                                wrote on last edited by
                                #16

                                Hi I tried it in a sample application 1> Main window with two edit controls. 2> In WM_LBUTTONDOWN i called SetFocus(hWnd); I noticed is that if i dont call setfocus explicitly i dont get the WM_SETFOCUS notification. and moreover when i switch between the two edit control i dont get any notification. may be you can try something like following. i dont see any need of using GetWindowLong. case WM_LBUTTONDOWN: SetFocus(hWnd); break; case WM_KILLFOCUS: unregisterkeys; break; case WM_SETFOCUS: registerkeys; break; I hope it helps.

                                Regards, Sandip.

                                D 2 Replies Last reply
                                0
                                • S SandipG

                                  Hi I tried it in a sample application 1> Main window with two edit controls. 2> In WM_LBUTTONDOWN i called SetFocus(hWnd); I noticed is that if i dont call setfocus explicitly i dont get the WM_SETFOCUS notification. and moreover when i switch between the two edit control i dont get any notification. may be you can try something like following. i dont see any need of using GetWindowLong. case WM_LBUTTONDOWN: SetFocus(hWnd); break; case WM_KILLFOCUS: unregisterkeys; break; case WM_SETFOCUS: registerkeys; break; I hope it helps.

                                  Regards, Sandip.

                                  D Offline
                                  D Offline
                                  danginkgo
                                  wrote on last edited by
                                  #17

                                  My code is exactly like what you wrote. The problem is I don't get the WM_KILLFOCUS message only when I return to the app window. If the focus is on the app window and I go to other window then I don't get any message.

                                  danginkgo

                                  S 1 Reply Last reply
                                  0
                                  • D danginkgo

                                    My code is exactly like what you wrote. The problem is I don't get the WM_KILLFOCUS message only when I return to the app window. If the focus is on the app window and I go to other window then I don't get any message.

                                    danginkgo

                                    S Offline
                                    S Offline
                                    SandipG
                                    wrote on last edited by
                                    #18

                                    No when i go from app window to edit control i get the WM_KILLFOCUS notifiction. also when i call SetFocus() i do get WM_SETFOCUS notification.

                                    Regards, Sandip.

                                    1 Reply Last reply
                                    0
                                    • S SandipG

                                      Hi I tried it in a sample application 1> Main window with two edit controls. 2> In WM_LBUTTONDOWN i called SetFocus(hWnd); I noticed is that if i dont call setfocus explicitly i dont get the WM_SETFOCUS notification. and moreover when i switch between the two edit control i dont get any notification. may be you can try something like following. i dont see any need of using GetWindowLong. case WM_LBUTTONDOWN: SetFocus(hWnd); break; case WM_KILLFOCUS: unregisterkeys; break; case WM_SETFOCUS: registerkeys; break; I hope it helps.

                                      Regards, Sandip.

                                      D Offline
                                      D Offline
                                      danginkgo
                                      wrote on last edited by
                                      #19

                                      It works now. It seems that the problem was that I called MessageBox() function in my code with wrong HWND. That massed up the messages. I took it out of the code and it works fine. Thank you very much for your help.

                                      danginkgo

                                      S 1 Reply Last reply
                                      0
                                      • D danginkgo

                                        It works now. It seems that the problem was that I called MessageBox() function in my code with wrong HWND. That massed up the messages. I took it out of the code and it works fine. Thank you very much for your help.

                                        danginkgo

                                        S Offline
                                        S Offline
                                        SandipG
                                        wrote on last edited by
                                        #20

                                        Good its working now You are welcome :).

                                        Regards, Sandip.

                                        1 Reply Last reply
                                        0
                                        • D danginkgo

                                          In the main window callback function I get the WM_KILLFOCUS message when I click on the window, and only then I get the WM_SETFOCUS message. Is that OK? I thought that WM_KILLFOCUS message is sent to a window immediatly before it looses focus. I want to register some values when the window has keyboard focus and unregister the values when the window looses focus. How can I do that?

                                          danginkgo

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #21

                                          danginkgo wrote:

                                          I want to register some values when the window has keyboard focus and unregister the values when the window looses focus.

                                          It sounds like you need to use WM_ACTIVATEAPP instead.

                                          "Love people and use things, not love things and use people." - Unknown

                                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                          D 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