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. MessageBox not getting Focus

MessageBox not getting Focus

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

    Hi, I have an MFC application. I run a thread in middle. After completing the thread i have a message box that popsup. But messagebox doesnt have focus. Can anyone let me know how to get the focus back to message box? Thanks in advance. Code snippet: .... RunCmd("test.bat", SW_SHOW); MessageBox("dfas","fdifd",MB-OK); ..... Test.bat has come command to format disk etc. Regards..

    N 1 Reply Last reply
    0
    • U User 3831761

      Hi, I have an MFC application. I run a thread in middle. After completing the thread i have a message box that popsup. But messagebox doesnt have focus. Can anyone let me know how to get the focus back to message box? Thanks in advance. Code snippet: .... RunCmd("test.bat", SW_SHOW); MessageBox("dfas","fdifd",MB-OK); ..... Test.bat has come command to format disk etc. Regards..

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Try How to steal focus on 2K/XP[^]

      nave [OpenedFileFinder] [My Blog]

      U 1 Reply Last reply
      0
      • N Naveen

        Try How to steal focus on 2K/XP[^]

        nave [OpenedFileFinder] [My Blog]

        U Offline
        U Offline
        User 3831761
        wrote on last edited by
        #3

        Thanks for the reply. But that doesnt have the answer for my problem. After launching a thread and completing the foucus is not going to main window. In a dialog based application, I run: WinExec("test.bat"); Sleep("5000"); MessageBox("sdosn"....); MessageBox is not having focus. If i remove Thread executions in between (WinExec). Then i get the focus properly. Any updates on this.

        N 1 Reply Last reply
        0
        • U User 3831761

          Thanks for the reply. But that doesnt have the answer for my problem. After launching a thread and completing the foucus is not going to main window. In a dialog based application, I run: WinExec("test.bat"); Sleep("5000"); MessageBox("sdosn"....); MessageBox is not having focus. If i remove Thread executions in between (WinExec). Then i get the focus properly. Any updates on this.

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          Member 3834630 wrote:

          WinExec("test.bat");

          Firstly I want to say that WinExec function is Obsolete. you should use, CreateProcess() or ShellExecute() to create new process.

          Member 3834630 wrote:

          MessageBox is not having focus. If i remove Thread executions in between (WinExec). Then i get the focus properly.

          Means if you comment out the WinExec() function, your message box is getting the focus and after closing the messagebox, your main dialog is getting the focus?

          nave [OpenedFileFinder] [My Blog]

          U 1 Reply Last reply
          0
          • N Naveen

            Member 3834630 wrote:

            WinExec("test.bat");

            Firstly I want to say that WinExec function is Obsolete. you should use, CreateProcess() or ShellExecute() to create new process.

            Member 3834630 wrote:

            MessageBox is not having focus. If i remove Thread executions in between (WinExec). Then i get the focus properly.

            Means if you comment out the WinExec() function, your message box is getting the focus and after closing the messagebox, your main dialog is getting the focus?

            nave [OpenedFileFinder] [My Blog]

            U Offline
            U Offline
            User 3831761
            wrote on last edited by
            #5

            Hi, Sorry, I am actually using CreatProcess() only. To make it simple i put WinExec() But after calling CreateProcess() which calls a batch file and after completing the process, i display a MessageBox(sdkjfo..), the MessageBox() gets displayed but doesnt have the focus. Yes, if i comment the CreateProcess() then MessageBox() gets the focus. Regards.

            N 1 Reply Last reply
            0
            • U User 3831761

              Hi, Sorry, I am actually using CreatProcess() only. To make it simple i put WinExec() But after calling CreateProcess() which calls a batch file and after completing the process, i display a MessageBox(sdkjfo..), the MessageBox() gets displayed but doesnt have the focus. Yes, if i comment the CreateProcess() then MessageBox() gets the focus. Regards.

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              Did you try modifying the code as I mentioned yesterday?

              WinExec("test.bat");
              Sleep("5000");
              //Attach foreground window thread
              //to our thread
              AttachThreadInput(
              GetWindowThreadProcessId(
              ::GetForegroundWindow(),NULL),
              GetCurrentThreadId(),TRUE);

              MessageBox("sdosn"....);
              //Detach the attached thread
              AttachThreadInput(
              GetWindowThreadProcessId(
              ::GetForegroundWindow(),NULL),
              GetCurrentThreadId(),FALSE);

              nave [OpenedFileFinder] [My Blog]

              U 1 Reply Last reply
              0
              • N Naveen

                Did you try modifying the code as I mentioned yesterday?

                WinExec("test.bat");
                Sleep("5000");
                //Attach foreground window thread
                //to our thread
                AttachThreadInput(
                GetWindowThreadProcessId(
                ::GetForegroundWindow(),NULL),
                GetCurrentThreadId(),TRUE);

                MessageBox("sdosn"....);
                //Detach the attached thread
                AttachThreadInput(
                GetWindowThreadProcessId(
                ::GetForegroundWindow(),NULL),
                GetCurrentThreadId(),FALSE);

                nave [OpenedFileFinder] [My Blog]

                U Offline
                U Offline
                User 3831761
                wrote on last edited by
                #7

                Yes, this worked.. Thank you. If you dont mind, Can you explain why it was earlier failing with out the code and what this code does.

                N 1 Reply Last reply
                0
                • U User 3831761

                  Yes, this worked.. Thank you. If you dont mind, Can you explain why it was earlier failing with out the code and what this code does.

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #8

                  Did you read the link I gave you yesterday? Actually in windows XP or later only a thread that own the current active foreground window has the privilege to set another window in foreground. So i guess when you create another process, your application loose the foreground window property

                  nave [OpenedFileFinder] [My Blog]

                  U 1 Reply Last reply
                  0
                  • N Naveen

                    Did you read the link I gave you yesterday? Actually in windows XP or later only a thread that own the current active foreground window has the privilege to set another window in foreground. So i guess when you create another process, your application loose the foreground window property

                    nave [OpenedFileFinder] [My Blog]

                    U Offline
                    U Offline
                    User 3831761
                    wrote on last edited by
                    #9

                    Hi Naveen, This solution is working fine on regular OS (vista, XP) but it is failing on Windows PE environment. Any other to way get the focus? Thanks, Ram

                    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