CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2001
    Posts
    244

    Running a function *after* OnInitDialog() has completed

    I have an MFC Dialog project in VS2005, and I would like to run a Function after OnInitDialog has finished.

    Does MFC have a message or feature available that lets you run a function directly after OnInitDialog()? For example AfterInitDialog()

    At the moment I am using SetTimer/OnTimer with a static bool variable to run a function shortly after OnInitDialog() just once, then ignore all the following OnTimer calls. It works, but I was just wandering if there is a 'cleaner' solution.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Running a function *after* OnInitDialog() has completed

    No, there is no function or message. But the way to do this is post a message to the window itself, at the end of OnInitDialog. Add a handler and do your stuff there. It will be taken from the queue at the end of dialog initialization, so it's exactly what you want. But use PostMessage, as I said, not SendMessage.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2009
    Posts
    57

    Re: Running a function *after* OnInitDialog() has completed

    Quote Originally Posted by Anarchi View Post
    I have an MFC Dialog project in VS2005, and I would like to run a Function after OnInitDialog has finished.

    Does MFC have a message or feature available that lets you run a function directly after OnInitDialog()? For example AfterInitDialog()

    At the moment I am using SetTimer/OnTimer with a static bool variable to run a function shortly after OnInitDialog() just once, then ignore all the following OnTimer calls. It works, but I was just wandering if there is a 'cleaner' solution.

    Just call the function. i.e:

    Code:
    OnInitDialog(){
     
    .....
     
    MyFunction(); 
     
    .....
     
     
    }

  4. #4
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688

    Re: Running a function *after* OnInitDialog() has completed

    Quote Originally Posted by CNemo View Post
    Just call the function. i.e:

    Code:
    OnInitDialog(){
     
    .....
     
    MyFunction(); 
     
    .....
     
     
    }
    The problem with that is the dialog won't be visible at that time.

    The standard method is that proposed by Cilu.

    You can also try a dirty trick doing it in OnPaint() with the aid of a flag.

    something like
    [code]
    CYourDialog::CYourDialog()
    {
    flag=FALSE;
    }
    CYourDialog::OnPaint()
    {
    if (flag==FALSE){
    YourFunction();
    flag=TRUE;
    }
    }

    But this is not an elegant solution.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Running a function *after* OnInitDialog() has completed

    The problem with that is the dialog won't be visible at that time.
    Code:
    OnInitDialog(){
    
    ShowWindow(SW_SHOW); 
    .....
     
    MyFunction(); 
     
    .....
      
    }
    Now it is.

  6. #6
    Join Date
    Apr 2009
    Posts
    57

    Re: Running a function *after* OnInitDialog() has completed

    [quote=Skizmo;1839673]
    Code:
    OnInitDialog(){
     
    ShowWindow(SW_SHOW); 
    .....
     
    MyFunction(); 
     
    .....
     
    }


    I use that too!! There was a big debate about calling that function prior to OnInitDialog completing. I've used it and haven't seen any problem.

    .... as a matter of fact I also use ShowWindow in the MainFrame prior to displaying messageboxes in FormView Apps.

  7. #7
    Join Date
    Nov 2001
    Posts
    244

    Re: Running a function *after* OnInitDialog() has completed

    Ive used ShowWindow in OnInitDialog before, some controls don't render or other issues arise, so I try to avoid that. It depends on whats in your app.

    cilu, PostMessage is a great idea - why didnt I think of that Thanks.

  8. #8
    Join Date
    Apr 2009
    Posts
    57

    Re: Running a function *after* OnInitDialog() has completed

    Or you could design you Dialog box so the controls work with ShowWindow. ... that's what I do.


    I call ShowWindow after all controls have been intialized.

  9. #9
    Join Date
    Nov 2001
    Posts
    244

    Re: Running a function *after* OnInitDialog() has completed

    New Problem!!!!!

    I now use PostMessage() in OnInitDialog(), but for some reason, the Message is sent twice


    I tried different WM_ values as well, for example WM_USER+5, WM_USER+10, WM_USER+100 (thinking I was using the same WM_ value as another message).


    I have confirmed that I am only calling PostMessage once using a MessageBox directly before the PostMessage line.

    I put a messagebox in PreTranslateMessage (when the message = my WM_USER+5 value), and it shows twice!

    I even tried calling PostMessage from another function, it also sends the message twice. So its not specifically an OnInitDialog glitch.



    Is this normal?
    Last edited by Anarchi; May 5th, 2009 at 09:55 PM.

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