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() :p
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.
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.
Re: Running a function *after* OnInitDialog() has completed
Quote:
Originally Posted by
Anarchi
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() :p
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();
.....
}
Re: Running a function *after* OnInitDialog() has completed
Quote:
Originally Posted by
CNemo
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.
Re: Running a function *after* OnInitDialog() has completed
Quote:
The problem with that is the dialog won't be visible at that time.
Code:
OnInitDialog(){
ShowWindow(SW_SHOW);
.....
MyFunction();
.....
}
Now it is.
Re: Running a function *after* OnInitDialog() has completed
[quote=Skizmo;1839673]
Code:
OnInitDialog(){
ShowWindow(SW_SHOW);
.....
MyFunction();
.....
}
:D 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.
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 :p Thanks.
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. :p
I call ShowWindow after all controls have been intialized.
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 :blush:
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?