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 and Modal dialog

MessageBox and Modal dialog

Scheduled Pinned Locked Moved C / C++ / MFC
c++asp-nethelpquestion
3 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.
  • R Offline
    R Offline
    rwestgraham
    wrote on last edited by
    #1

    Sorry if this is a dumb ? but I am fairly new to C++. I am writing an application that has no user events. I am using only WIN32 - NO MFC - because it is basically a bootstrapper application and I do not want any dependencies other than core Windows APIs. I display a small status dialog that provides progress messages. If all goes well, it runs, displays it's messages, and the bootstrapper application terminates after launching another application. That part works fine. But, if a condition is not met, I would like to display a message box. When I do this, the whole thing locks up. I am creating the messagebox as a child of the dialog, but it still does not process it's messages or even receive focus. I've tried making the main dialog modeless - did not help. I find the SDK docs on message processing rather confusing. Can anyone point me in the right direction? Thanks!!!! R :-)

    B 1 Reply Last reply
    0
    • R rwestgraham

      Sorry if this is a dumb ? but I am fairly new to C++. I am writing an application that has no user events. I am using only WIN32 - NO MFC - because it is basically a bootstrapper application and I do not want any dependencies other than core Windows APIs. I display a small status dialog that provides progress messages. If all goes well, it runs, displays it's messages, and the bootstrapper application terminates after launching another application. That part works fine. But, if a condition is not met, I would like to display a message box. When I do this, the whole thing locks up. I am creating the messagebox as a child of the dialog, but it still does not process it's messages or even receive focus. I've tried making the main dialog modeless - did not help. I find the SDK docs on message processing rather confusing. Can anyone point me in the right direction? Thanks!!!! R :-)

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      The problem with MessageBox is that it takes over input and doesn't allow the creating thread to run. In effect, the MessageBox takes over the focus, just like a modal dialog. So you'll want to come up with a way to run the MessageBox function in a thread separate from your main dialog's thread. You may consider creating an additional modeless dialog that hangs off of your main dialog. You don't have to display the dialog (never call its ShowWindow(SW_SHOW)), and it doesn't have to have any special features. It only exists to pop up the MessageBox. Maybe add a method to this modeless dialog, DisplayMessage(msg) or something, that calls MessageBox. I'm not able to test this now, but I think it'll work. It shouldn't tie up your main dialog's message loop, only the message loop of the invisible modeless dialog. Just make sure that you don't call the DisplayMessage method in the modeless while another message is being displayed. You'll also have to make sure the modeless message box gets cleaned up correctly (via DestroyWindow and (just like all modeless dialogs) don't let it be closed by the user. Alternately, if you're good with Win32 threads, you can define a thread that you can fire from your main dialog. Given a string to display (and the MB_OK, etc. codes), this thread's main execution function simply calls MessageBox(your params), then exits. Just be sure you clean up the threads nicely so as not to leave "zombie" threads. Hope this gives you some ideas. Bob Ciora

      R 1 Reply Last reply
      0
      • B Bob Ciora

        The problem with MessageBox is that it takes over input and doesn't allow the creating thread to run. In effect, the MessageBox takes over the focus, just like a modal dialog. So you'll want to come up with a way to run the MessageBox function in a thread separate from your main dialog's thread. You may consider creating an additional modeless dialog that hangs off of your main dialog. You don't have to display the dialog (never call its ShowWindow(SW_SHOW)), and it doesn't have to have any special features. It only exists to pop up the MessageBox. Maybe add a method to this modeless dialog, DisplayMessage(msg) or something, that calls MessageBox. I'm not able to test this now, but I think it'll work. It shouldn't tie up your main dialog's message loop, only the message loop of the invisible modeless dialog. Just make sure that you don't call the DisplayMessage method in the modeless while another message is being displayed. You'll also have to make sure the modeless message box gets cleaned up correctly (via DestroyWindow and (just like all modeless dialogs) don't let it be closed by the user. Alternately, if you're good with Win32 threads, you can define a thread that you can fire from your main dialog. Given a string to display (and the MB_OK, etc. codes), this thread's main execution function simply calls MessageBox(your params), then exits. Just be sure you clean up the threads nicely so as not to leave "zombie" threads. Hope this gives you some ideas. Bob Ciora

        R Offline
        R Offline
        rwestgraham
        wrote on last edited by
        #3

        Thanks for the input. I had already considered spawning a second thread but instead I kept reading the SDK info on message loops and thinking "Surely this cannot be as difficult as I am making it". So I went with your second suggestion. I just call a wrapper function that spawns the thread and goes into a WaitForSingleObject state to suspend the main execution until the user dismisses the messagebox. Works great! Thanks, Robert

        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