problem with A simple modeless messagebox

This is a discussion on problem with A simple modeless messagebox within the C++ Programming forums, part of the General Programming Boards category; hi i am trying to create a simple messagebox afxmessagebox will do just fine but the problem is - it ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    problem with A simple modeless messagebox

    hi

    i am trying to create a simple messagebox
    afxmessagebox will do just fine but the problem is
    - it blocks my main application
    - i cannot create multiples messagebox (which i need to)

    so after snooping around the place, i discovered that a solution would be to create a worker thread to handle the messageboxes

    here's my code

    Code:
    UINT pg_price::th_msgbox ( LPVOID lpvParam ) 
    {
        pg_price *th_msgbox = (pg_price*)lpvParam;
    	
    	CDialog* pDialog;
    	pDialog = new CDialog();
    	pDialog->Create(IDD_MESSAGEBOX, NULL);
    	pDialog->ShowWindow(SW_SHOW);
        return 0;
    }

    here's how it's called:
    Code:
    void pg_price::OnSomeEvent() 
    {
    	AfxBeginThread ( th_msgbox, (LPVOID)this );
    }
    but the problem is that the messagebox displays for a split second and disappears. i believe it's because the worker thread returns and hence the dialog goes? or is there another reason for that??

    how do i solve this problem of creating a dialog messagebox that
    - doesnt block my main application
    - allows my main application to continue running,processing
    - multiple messageboxes

    please assist:wave:

  2. #2
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    nmot sure if this is the idea you intend but here....

    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, int nCmdShow)
    {
        MessageBox(NULL, "Test box", "Note", MB_OK);
        return 0;
    }

  3. #3
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    tell me if thats not what you want *subscribes* im intrested to see how this plays out

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Call pDialog->DoModal() after the call to Create().
    If your dialog resource does not have WS_VISIBLE, then call ShowWindow() before DoModal() as well.

    Don't bother with passing "this" to the thread if you're not going to use it

    gg

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by C+noob
    nmot sure if this is the idea you intend but here....

    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, int nCmdShow)
    {
        MessageBox(NULL, "Test box", "Note", MB_OK);
        return 0;
    }
    thanks for your help but i get
    "illegal call of non-static member function" while compiling
    and i am doing this MFC style

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by Codeplug
    Call pDialog->DoModal() after the call to Create().
    If your dialog resource does not have WS_VISIBLE, then call ShowWindow() before DoModal() as well.

    Don't bother with passing "this" to the thread if you're not going to use it

    gg
    thanks but i get an assertion error when i put the DoModal command in

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    i tried it in another way and it worked

    Code:
    UINT pg_price::th_msgbox ( LPVOID lpvParam ) 
    {
        pg_price *th_msgbox = (pg_price*)lpvParam;
    	
    	CDialog* pDialog;
    	pDialog = new CDialog();
    	pDialog->MessageBox(th_msgbox->messagebox, "Time Is Up",MB_OK);
        return 0;
    }
    thanks for your great help!

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Getting rusty on my MFC....
    Use the constructor for modal dialog boxes:
    Code:
    UINT pg_price::th_msgbox(LPVOID lpvParam) 
    {
        CDialog dlg(IDD_MESSAGEBOX);
        dlg.ShowWindow(SW_SHOWNORMAL);
        dlg.DoModal();
        return 0;
    }
    Declare the method within the pg_price class like so:
    Code:
    static UINT th_msgbox(LPVOID lpvParam);
    The better way of doing this is with modelss dialog boxes.

    If all you want is a message box, then simply call AfxMessageBox().

    gg
    Last edited by Codeplug; 07-05-2005 at 10:53 PM.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think if you want to use a modeless dialog in a thread, you could try creating a user interface thread and then when the modeless dialog is destroyed post a WM_QUIT message to the thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 09:49 AM
  3. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 10:34 PM
  4. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 03:07 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21