|
-
May 4th, 2009, 09:51 PM
#1
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.
-
May 5th, 2009, 12:52 AM
#2
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.
-
May 5th, 2009, 01:11 PM
#3
Re: Running a function *after* OnInitDialog() has completed
 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()
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();
.....
}
-
May 5th, 2009, 01:33 PM
#4
Re: Running a function *after* OnInitDialog() has completed
 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.
-
May 5th, 2009, 02:34 PM
#5
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.
-
May 5th, 2009, 04:24 PM
#6
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.
-
May 5th, 2009, 07:35 PM
#7
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.
-
May 5th, 2009, 08:40 PM
#8
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.
-
May 5th, 2009, 09:44 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|