-
August 16th, 2015, 08:09 PM
#1
MFC Window Minimize Issue
Hello,
I have an MFC application and I have an issue where it does not disappear when minimized to the system tray.
It does correctly show an icon in the system tray when minimized, but it shows up just above the task bar on the left side of the screen. It does not display an icon in the taskbar, and I do not want an icon in the task bar.
How do I get the application to completely disappear from the screen?
I have included the relevant source code.

CODE:
Code:
// MyWinApp.cpp
#include "MyWinApp.h"
#include "MainFrame.h"
BOOL CMyWinApp::InitInstance()
{
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame; // a CWinApp member variable
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow(); // generates WM_PAINT msg
return TRUE;
}
// MyWinApp.h
#include <afxwin.h>
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// MainFrame.h
#include <afxwin.h>
class CMainFrame : public CFrameWnd
{
private:
/**
* MFC CONTROLS
*/
CMenu m_TrayMenu;
CEdit edtStatusDisplay;
/**
* MFC OBJECTS
*/
CWnd m_wndOwnder;
public:
CMainFrame(); // Constructor
BOOL SetTrayIcon(int intAction, int intIconId, LPCTSTR szTip);
/**
* INLINE
*/
BOOL AddTrayIcon(int intIconID, LPCTSTR szTip)
{ return SetTrayIcon(NIM_ADD, intIconID, szTip); }
BOOL ModifyTrayIcon(int intIconID, LPCTSTR szTip)
{ return SetTrayIcon(NIM_MODIFY, intIconID, szTip); }
BOOL DeleteTrayIcon()
{ return SetTrayIcon(NIM_DELETE, 0, NULL); }
/**
* END INLINE
*/
DECLARE_MESSAGE_MAP();
// window msg handling function
afx_msg BOOL PreCreateWindow(CREATESTRUCT& cs);
afx_msg void OnPaint();
afx_msg void OnDestroy( );
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg LRESULT OnTrayNotify(WPARAM wParam, LPARAM lParam);
afx_msg void OnSysTrayMenuRestore(void);
afx_msg void OnSysMenuExitApp(void);
};
// MainFrame.cpp
#include "MainFrame.h"
#include "resource.h"
#define IDC_EDT_DISPLAYSTATUS 150
#define NIF_ID 100
#define NIF_MSG (WM_USER+101)
#define DOODAD_PORTAL_STATUS_TIMER 1
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
// message calls this function
ON_WM_PAINT()
ON_WM_SYSCOMMAND()
ON_MESSAGE(NIF_MSG, OnTrayNotify)
ON_WM_TIMER()
END_MESSAGE_MAP()
CMainFrame::CMainFrame() : TIMER_STATUS_DURATION(3500) // Constructor
{
this->m_TrayMenu.LoadMenu(IDR_MENU_SYSTRAY);
/**
* ALWAYS CREATE WINDOW FIRST BEFORE
* TRYING TO GET A DEVICE CONTEXT
*/
this->Create(NULL, _T("Doodad 3.0 Powered by MFC"),
WS_MINIMIZEBOX | WS_SYSMENU,
CRect(10,10,830,515), NULL,
MAKEINTRESOURCE(IDR_MENU_APPMAIN));
CenterWindow(); // try this
this->edtStatusDisplay.CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(" "), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL,
CRect(451, 44, 795, 380), this, IDC_EDT_DISPLAYSTATUS);
}
void CMainFrame::OnPaint()
{
}
void CMainFrame::OnDestroy()
{
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CFrameWnd::PreCreateWindow(cs))
return FALSE;
// Create invisible window
if (!::IsWindow(m_wndOwnder.m_hWnd))
{
LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
if (!m_wndOwnder.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, 0))
return FALSE;
}
cs.hwndParent = m_wndOwnder.m_hWnd;
return TRUE;
}
LRESULT CMainFrame::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
UINT uMsg = (UINT) lParam;
switch(uMsg)
{
case WM_LBUTTONDBLCLK:
this->DeleteTrayIcon();
this->ShowWindow(SW_RESTORE);
break;
case WM_RBUTTONUP:
CPoint pt;
GetCursorPos(&pt);
m_TrayMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
break;
};
return TRUE;
}
void CMainFrame::OnSysTrayMenuRestore(void)
{
this->DeleteTrayIcon();
this->ShowWindow(SW_RESTORE);
}
void CMainFrame::OnSysMenuExitApp(void)
{
this->DeleteTrayIcon();
this->DestroyWindow();
}
BOOL CMainFrame::SetTrayIcon(int intAction, int intIconId, LPCTSTR szTip)
{
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(nid);
nid.hWnd = this->m_hWnd;
nid.uID = NIF_ID;
if(intAction != NIM_DELETE)
{
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = NIF_MSG;
nid.hIcon = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(intIconId));
Checked::tcscpy_s(nid.szTip, sizeof(nid.szTip)/sizeof(TCHAR), szTip);
}
return Shell_NotifyIcon(intAction, &nid);
}
void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
switch(nID)
{
case SC_MINIMIZE:
this->AddTrayIcon(IDI_MYAPP_OK, this->m_pMyApp->GetStatusMessageAsCStr());
AfxGetMainWnd()->ShowWindow(SW_HIDE);
break;
case SC_MAXIMIZE:
break;
case SC_RESTORE:
break;
default:
break;
}
CFrameWnd::OnSysCommand(nID, lParam);
}
/**
* PROTECTED METHODS
*/
-
August 17th, 2015, 01:10 AM
#2
Re: MFC Window Minimize Issue
[thread was moved from WinAPI Forum...]
Victor Nijegorodov
-
August 17th, 2015, 09:17 AM
#3
Re: MFC Window Minimize Issue
 Originally Posted by Grond
Hello,
I have an MFC application and I have an issue where it does not disappear when minimized to the system tray.
It does correctly show an icon in the system tray when minimized, but it shows up just above the task bar on the left side of the screen. It does not display an icon in the taskbar, and I do not want an icon in the task bar.
How do I get the application to completely disappear from the screen?
I have included the relevant source code.
The issue root cause is the window style, and indirectly, the way of minimizing.
- Window style. Any overlapped window normally minimizes like it does in your case. To completely disappear, it should be owned by invisible window, or should be not a popup/overlapped window, e.g. a tool window.
- Way of minimizing could be changed as an alternative. You intercept minimization command and override it with window hiding (like ShowWindow(SW_HIDE)). Unfortunately, this does not resolve the issue entirely, as taskbar icon won't disappear in this case, and you have to additionally take care of the icon removal by means of ITaskbarList interface. Closing window might resolve the original issue of visibility, but in this case the system tray icon should still have a window to handle the icon's messages (of course some sort of invisible window is preferable here, I believe).
Last edited by Igor Vartanov; August 18th, 2015 at 02:20 AM.
Best regards,
Igor
Tags for this Thread
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
|