File Dialog after dialog creation
-
I have Visual c++ dialog application . once the dialog has been created immediately I need to show a File Dialog . Doing it in OnInitDialog doesnt work as expected as it pops up the file dialog . Is there some other point where i can make this call or do i have to have some internal variable that a timer checks after a while and then shows the dialog ? Engineering is the effort !
-
I have Visual c++ dialog application . once the dialog has been created immediately I need to show a File Dialog . Doing it in OnInitDialog doesnt work as expected as it pops up the file dialog . Is there some other point where i can make this call or do i have to have some internal variable that a timer checks after a while and then shows the dialog ? Engineering is the effort !
Well there's a few ways to tackle this problem. Indeed you could use a timer. But the most important thing for you to understand is how message queues in Windows work. However, first the answer to your question. Try creating your own custom message ad handler for it first therefore at the top of your dialog CPP file...
#define WM_MYOWNMESSAGE WM_USER + 1001;
... Then in the header file put...afx_msg LRESULT OnMyOwnMessage(WPARAM wParam, LPARAM lParam);
...Then the appropriate body for it...LRESULT CMyDialog::OnMyOwnMessage(WPARAM wParam, LPARAM lParam) { ... Show the file dialog box (I know it has to be modal) return 0; };
... And finally in the body put...ON_MESSAGE(WM_MYOWNMESSAGE, OnMyOwnMessage)
... in the BEGIN_MESSAGE_MAP section... ... To call the message handler put this in the OnInitDialog function just before it returns... PostMessage(WM_MYOWNMESSAGE, 0, 0); So okay, this should work. But why should this work? You could do the same thing with a timer (Its another message WM_TIMER). Well as you well know OnInitDialog gets called when all the handles for all the windows you are creating are created but not shown. We post a custom message on to the message queue of the dialog box. This eventually gets processed. As I'm placing it on the message queue in OnInitDialog it will only get processed after this point in which the dialog box will be shown. I'm sure there are other ways but I find this one works very well for me :!)