How to hide a window in TaskBar

13 01 2009

description5

 

 

Sometimes we may want to hide an application or window while it’s minimizing or some other situations and need to show our application icon in the taskbar notification area or tray area  just like Messenger applications hide it to taskbar once it closed but the application is still running.

hideapp

 howcanidoit13 

 

 

We could establish this by using the NOTIFYICONDATA  and Shell_NotifyIcon functions. The NOTIFYICONDATA   structure contains information that the system needs to process taskbar status area messages and the Shell_NotifyIcon function sends a message to the system to add, modify, or delete an icon from the taskbar status area. Please check these functions in the MSDN to get more information about the parameters.

 mycodesnippet4

 

 

Please check the code sample, which hide the application icon to tray area once it minimize and it will popup a menu when we right click on it. 

header file

//{{AFX_MSG(CTest1Dlg)
afx_msg void OnMaximizeApp();
afx_msg void OnAbout();
afx_msg void OnExit();
//}}AFX_MSG
 DECLARE_MESSAGE_MAP()

Implementation file

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CTest1Dlg)
ON_WM_SIZE()
ON_COMMAND(WM_USER+1,  OnMaximizeApp)
ON_COMMAND(WM_USER+2,  OnAbout)
ON_COMMAND(WM_USER+3,  OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMyDialog::OnSize(UINT nType, int cx, int cy)
{
   CDialog::OnSize(nType, cx, cy);
  
   //track the window minimizing message
   if(nType == SIZE_MINIMIZED)
   {
      NOTIFYICONDATA niData;
      ZeroMemory(&niData,sizeof(NOTIFYICONDATA));
     
      niData.cbSize = sizeof(niData);
      niData.hWnd   = this->m_hWnd;
      niData.uID    = 100;
      niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
      //u could load ur own icon here
      niData.hIcon  = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
     
      //set the CALLBACK message ID
      niData.uCallbackMessage =  WM_USER + 200; 
     
      //Adds our App. icon to the taskbar status area
      Shell_NotifyIcon( NIM_ADD, &niData);     
     
      //hide our application/window
      this->ShowWindow(SW_HIDE);        
   }  
}
LRESULT CMyDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
  
   //if  the message is from our application
   //which is hiding in the tray area
   if( message == (WM_USER + 200) )
   {
      //trace the right mouse click msg
      if(lParam == WM_RBUTTONDOWN)
      {
         CMenu myMenu;
         myMenu.CreatePopupMenu();        
        
         POINT pt;
         //Get the current cursor point
         GetCursorPos(&pt);
        
         //add menu items
         myMenu.AppendMenu( MF_POPUP, WM_USER + 1, _T("Maximize App.") );
         myMenu.AppendMenu( MF_POPUP, WM_USER + 2, _T("About.") );
         myMenu.AppendMenu( MF_POPUP, WM_USER + 3, _T("Exit") );
        
         myMenu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,pt.x,pt.y,this );
      }  
   }
  
   return CDialog::WindowProc(message, wParam, lParam);
}
void CMyDialog::OnAbout()
{
   //Add code here
}
void CMyDialog::OnMaximizeApp()
{
   this->ShowWindow(SW_RESTORE);
}
void CMyDialog::OnExit()
{
   EndDialog(1);  
}

mynote4

 

 

 

You can keep your application compatible with all Shell32.dll version by checking the dll version installed on your machine by the function DllGetVersion.

Advertisements

Actions

Information

7 responses

10 02 2010
Carlos Olmedo

Your code is working fine for me but.. do you know how to make a CDialog hidden at startup? I’m searching on google and none of the tips i found works for me.

Thanks

11 02 2010
Sanoop S P

Do you want to run your application at system startup and then it hide?

1 07 2011
Dimas

Nice post. Helped me solve my task.

One little remark:
line 19: niData.hWnd = this->m_hWnd;
“this” may be omitted =)

1 07 2011
Sanoop S P

Thanks a lot for your comment 🙂

5 10 2011
top criminal attorneys wv

This could be this blog’s best writing on the web

13 10 2011
hotel charme provence

This definitely makes great sense

11 02 2012
How to prevent desktop from locking « C++ and VC++ Tips

[…] you want to hide this sample application to taskbar please see one of the oldest post. Rate this: Like this:LikeBe the first to like this […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: