CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2001
    Location
    Texas, USA
    Posts
    339

    DoModal and call function

    I have a window I am opening from the parent Dialog by using DoModal:

    CMyDlg dml;

    dml.DoModal;

    When the window opens I want it to run a function on open. Lets just for example say copy files and show a progress bar. Where can I put the function call so that when DoModal is called that function will execute?

    Thank you in advance.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: DoModal and call function

    You could try handling WM_SHOW.

  3. #3
    Join Date
    Oct 2001
    Location
    Texas, USA
    Posts
    339

    Re: DoModal and call function

    Just tried it. The Modal window does not show until the function is complete. I want the window to open the start the function.

    Also forgot to put in the first post this is Visual C++ v6.
    Last edited by strych9; September 20th, 2012 at 09:15 AM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: DoModal and call function

    Quote Originally Posted by strych9 View Post
    Just tried it. The Modal window does not show until the function is complete. I want the window to open the start the function.

    Also forgot to put in the first post this is Visual C++ v6.
    OnInitDialog()

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2001
    Location
    Texas, USA
    Posts
    339

    Re: DoModal and call function

    I tried that but the loop of the function keeps the Modal window from showing till it is done.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: DoModal and call function

    I'm surprised WM_SHOW didn't work. You could try WM_ACTIVATE also. Another trick people use sometimes is to set a short timer in OnInitDialog, then start the function when the timer goes off.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: DoModal and call function

    OnInitDialog is called before the dialog is shown.
    One simple and reliable solution to perform a lengthy task while the dialog is visible in order to show a progress bar, is to post an application-defined message from OnInitDialog.
    Like in the following example:

    Code:
    #define WM_APP_POSTINITDIALOG (WM_APP+1)
    
    class CMyDialog : public CDialog
    {
       //...
       afx_msg LRESULT OnAppPostInitDialog(WPARAM wParam, LPARAM lParam);
    };
    Code:
       // ...
       //}}AFX_MSG_MAP
       ON_MESSAGE(WM_APP_POSTINITDIALOG, OnAppPostInitDialog)
    END_MESSAGE_MAP()
    
    BOOL CMyDialog::OnInitDialog()
    {
       CDialog::OnInitDialog();
       // ...
       // ...
       // Here the dialog is NOT yet visible.
       PostMessage(WM_APP_POSTINITDIALOG); 
       return TRUE;
    }
    
    LRESULT CMyDialog::OnAppPostInitDialog(WPARAM wParam, LPARAM lParam)
    {
       // Here the dialog IS visible.
       // do some lengthy task, show progress, etc...
       return 0;
    }
    Last edited by ovidiucucu; September 20th, 2012 at 10:31 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Oct 2001
    Location
    Texas, USA
    Posts
    339

    Re: DoModal and call function

    I will give it a try. Thanks for the help everyone!

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: DoModal and call function

    I'd recommend using a worker thread for your lengthy task. If you don't, you might find that other aspects of the dialog stop working or become very clunky. For example, the dialog itself might display okay but once the lengthy task is running, the dialog becomes unresponsive if you try to move it or resize it
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: DoModal and call function

    Quote Originally Posted by John E View Post
    I'd recommend using a worker thread for your lengthy task [...]
    Yes John, that's OK but not always. It's possible to be required to complete the task before user does some other action (and only show the progress). This case a worker thread isn't necessary.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    Oct 2001
    Location
    Texas, USA
    Posts
    339

    Re: DoModal and call function

    Quote Originally Posted by ovidiucucu View Post
    OnInitDialog is called before the dialog is shown.
    One simple and reliable solution to perform a lengthy task while the dialog is visible in order to show a progress bar, is to post an application-defined message from OnInitDialog.
    Like in the following example:

    Code:
    #define WM_APP_POSTINITDIALOG (WM_APP+1)
    
    class CMyDialog : public CDialog
    {
       //...
       afx_msg LRESULT OnAppPostInitDialog(WPARAM wParam, LPARAM lParam);
    };
    Code:
       // ...
       //}}AFX_MSG_MAP
       ON_MESSAGE(WM_APP_POSTINITDIALOG, OnAppPostInitDialog)
    END_MESSAGE_MAP()
    
    BOOL CMyDialog::OnInitDialog()
    {
       CDialog::OnInitDialog();
       // ...
       // ...
       // Here the dialog is NOT yet visible.
       PostMessage(WM_APP_POSTINITDIALOG); 
       return TRUE;
    }
    
    LRESULT CMyDialog::OnAppPostInitDialog(WPARAM wParam, LPARAM lParam)
    {
       // Here the dialog IS visible.
       // do some lengthy task, show progress, etc...
       return 0;
    }
    I gave this a shot and the window did not come up until the code in OnAppPostInitDialog had executed. Not really wanting to thread this.

  12. #12
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: DoModal and call function

    Quote Originally Posted by strych9 View Post
    I gave this a shot and the window did not come up until the code in OnAppPostInitDialog had executed.
    This is the kind of problem that might even behave differently with different versions of Windows.

    It's probably not ideal - but why not give the dialog an 'OK' button and a 'Cancel' button so that the user needs to issue a confirmation before the task gets carried out (or alternatively, cancel). You could rename them "Confirm" and "Abort" or something like that. I still think a worker thread is your best option though. I sometimes have dialog boxes where all the controls are initially grayed out until some background task has completed but I still use a worker thread so that the user can at least move, minimize or resize the dialog.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: DoModal and call function

    Modal dialogs only:
    Code:
    #include <afxwin.h>
    #include <afxpriv.h>
    #include "resource.h"
    
    class CMyDlg: public CDialog
    {
    	BOOL m_openDialog;
    public:
    	CMyDlg(): CDialog(IDD_MAIN) {}
    
    	BOOL OnInitDialog()
    	{
    		CDialog::OnInitDialog();
    
    		m_openDialog = TRUE;
    
    		return TRUE;
    	}
    
    	LRESULT OnKickIdle(WPARAM wp, LPARAM lp)
    	{
    		if (m_openDialog)
    		{
    			m_openDialog = FALSE;
    			AfxMessageBox(TEXT("Open Dialog"));
    		}
    		return 0;
    	}
    
    	DECLARE_MESSAGE_MAP()
    };
    
    BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    	ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
    END_MESSAGE_MAP()
    
    
    class CMyApp: public CWinApp
    {
    	BOOL InitInstance()
    	{
    		CMyDlg dlg;
    		dlg.DoModal();
    		return FALSE;
    	}
    };
    
    CMyApp theApp;
    Sample attached.
    Attached Files Attached Files
    Best regards,
    Igor

  14. #14
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: DoModal and call function

    Quote Originally Posted by strych9 View Post
    I gave this a shot and the window did not come up until the code in OnAppPostInitDialog had executed.
    It MUST work. Are you sure you have used PostMessage to post WM_APP_POSTINITDIALOG, as suggested and NOT SendMessage?
    Last edited by ovidiucucu; September 21st, 2012 at 07:50 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured