I have found an article in the Codeguru articles titled "SDI/MDI MFC Application in Windows System Tray", which explains to hide an App you simply call ShowWindow(SW_HIDE) in the application's InitInstance.
My problem is I am using VC6 with a Dialog based application and want it to appear when run in the system Tray and only show up when the icon in taskbar is clicked. In a MFC Dialog based project the dialog is created with a DoModal call hence I can not call the ShowWindow(SW_HIDE) here, if I call it in the InitDialog, it does not work either.
I need to application to hide on startup, what am I missing?
Thanks for the suggestion, I put the suggested code in the InitDialog function unfortunately it did not work.
I put the ShowWindow(SW_HIDE) behind a button and it worked fine, I went into the MSDN documentation and it says:
The first time an application calls ShowWindow, it should use the WinMain functions nCmdShow parameter as it's nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specifed by the WinMain function's nCmdShow parameter.
Therefore I cannot see a way to get it to hide a Dialog based app automatically when it starts, maybe I will have to think of a way of getting an SDI to look like a dialog app to solve this?
It still refuses to hide when it starts, yes it minimises but I need to have it not appear in the Taskbar as the access will soley be through the system tray icon. ShowWindow(SW_HIDE) seems to only hide the dialog properly after initial dialog, say behind a button press event. Any Ideas?
There is an article on CG about this, written by Warren Young. I don't have time to find it now, but here is the demo project from it (see attachment).
...but I need to have it not appear in the Taskbar as the access will soley be through the system tray icon. ShowWindow(SW_HIDE) seems to only hide the dialog properly after initial dialog, say behind a button press event. Any Ideas?
It's so easy:
Code:
BOOL CMyAPp::InitInstance()
{
CFrameWnd* pFrame = new CFrameWnd;
pFrame->Create( 0, TEXT("") ); // void frame will invisible forever
m_pMainWnd = pFrame;
CMyCoolTrayDlg dlg( pFrame ); // a child of invisible parent never appears in Taskbar
dlg.DoModal();
pFrame->DestroyWindow();
return FALSE;
}
Suppose it will help.
The problem could happen when you try to avoid of any blinking of a dialog. And the root of problem is hidden in the fact that you create a dialog as modal - MFC internals forces a modal dialog to be shown in any case. The solution is to create a modeless dialog and just don't show it.
I have found an article in the Codeguru articles titled "SDI/MDI MFC Application in Windows System Tray", which explains to hide an App you simply call ShowWindow(SW_HIDE) in the application's InitInstance.
My problem is I am using VC6 with a Dialog based application and want it to appear when run in the system Tray and only show up when the icon in taskbar is clicked. In a MFC Dialog based project the dialog is created with a DoModal call hence I can not call the ShowWindow(SW_HIDE) here, if I call it in the InitDialog, it does not work either.
I need to application to hide on startup, what am I missing?
Thanks in advance.
KnNeeded
You can try the following way
1. Make a user define Message and map it to your main dialog. Say WM_USERWNDHIDE.
2. Then Hide the window into it. Like
void CDlgTrayDlg::OnHideWindow()
{
ShowWindow(SW_HIDE);
}
3. Declare a boolean flag into the dialog class and make it TRUE into your dialog class.
4. add the following code to your OnPaint function
if( m_bTrayIcon )
{
PostMessage(WM_COMMAND,WM_USERWNDHID);
}
5. Now handle the TrayIcon handling code in appropriate place and make the
above m_bTrayIcon flag true false appropriately.
Got it now, to avoid the blinking problem I have decided to go with the modeless dialog suggested by Igor Vartanov, thanks. Its a bit more effort but worth it in my case to make it look a bit more professional.
If I used Igor's solution, how would I go about exiting the app? I tried calling OnOK in the dialog class but no go. postmessage WM_CLOSE doesn't work either.
1. A dialog is modeless.
So we should close it with DestroyWindow(). It would destroy window but not dialog object. For the latter we should overload PostNcDestroy() and insert a line
Code:
delete this;
at the very end.
2. If you had accepted the method dealing with the invisible parent of a dialog you should take care about main window or (if you don't care about it) just terminate message loop.
Code:
// gracefully close main window
AfxGetMainWnd()->SendMessage(WM_CLOSE);
// terminate message loop with no remorse
PostQuitMessage(0);
Any of that should be done right after dialog window was destroyed.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.