Click here to Skip to main content
11,641,416 members (59,654 online)
Click here to Skip to main content
Add your own
alternative version

Minimizing windows to the System Tray

, 18 Oct 2000 153.4K 3.1K 74
A set of routines that show how easy it is to minimise your windows to the system tray
minimizetotray_demo.zip
MinimizeToTray
Debug
MinimizeToTray.dsp
MinimizeToTray.dsw
Release
MinimizeToTray.exe
world.ico
// MinimizeToTray
//
// A sample app to show how to make it produce a custom caption animation to
// make it look like we are minimizing to and maximizing from the system tray
//
// Copyright 2000 Matthew Ellis <m.t.ellis@bigfoot.com>
#include "stdafx.h"
#include "resource.h"

#include "MinimizeToTray.h"

HINSTANCE g_hInstance;

#define WM_TRAYMESSAGE WM_USER

static VOID ShowNotifyIcon(HWND hWnd,BOOL bAdd)
{
  NOTIFYICONDATA nid;
  ZeroMemory(&nid,sizeof(nid));
  nid.cbSize=sizeof(NOTIFYICONDATA);
  nid.hWnd=hWnd;
  nid.uID=0;
  nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
  nid.uCallbackMessage=WM_TRAYMESSAGE;
  nid.hIcon=LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));
  lstrcpy(nid.szTip,TEXT("Double-click to maxmize!"));

  if(bAdd)
    Shell_NotifyIcon(NIM_ADD,&nid);
  else
    Shell_NotifyIcon(NIM_DELETE,&nid);
}

static BOOL CALLBACK DialogProc(HWND hWnd,UINT uiMsg,WPARAM wParam,LPARAM lParam)
{
  static bool bHideIcon=false;

  switch(uiMsg)
  {
    case WM_SYSCOMMAND:
      if(wParam==SC_MINIMIZE)
      {
	MinimizeWndToTray(hWnd);
	ShowNotifyIcon(hWnd,TRUE);

	// Return TRUE to tell DefDlgProc that we handled the message, and set
	// DWL_MSGRESULT to 0 to say that we handled WM_SYSCOMMAND
	SetWindowLong(hWnd,DWL_MSGRESULT,0);
	return TRUE;
      }
      break;

    case WM_TRAYMESSAGE:
      switch(lParam)
      {
	case WM_LBUTTONDBLCLK:
	  RestoreWndFromTray(hWnd);

	  // If we remove the icon here, the following WM_LBUTTONUP (the user
	  // releasing the mouse button after a double click) can be sent to
	  // the icon that occupies the position we were just using, which can
	  // then activate, when the user doesn't want it to. So, set a flag
	  // so that we remove the icon on the next WM_LBUTTONUP
	  bHideIcon=true;
	  return TRUE;

	case WM_LBUTTONUP:
	  // The user has previously double-clicked the icon, remove it in
	  // response to this second WM_LBUTTONUP
	  if(bHideIcon)
	  {
	    ShowNotifyIcon(hWnd,FALSE);
	    bHideIcon=false;
	  }
	  return TRUE;
      }
      break;

    case WM_CLOSE:
      MinimizeWndToTray(hWnd);
      ShowNotifyIcon(hWnd,TRUE);
      return TRUE;

    case WM_COMMAND:
      PostQuitMessage(0);
      break;
  }

  return FALSE;
}

// Simple entry point and message pump
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
  LPSTR lpCmdLine,int nCmdShow)
{
  g_hInstance=hInstance;

  HWND hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);
  if(hWnd)
  {
    MSG msg;
    while(GetMessage(&msg,hWnd,0,0))
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Matthew Ellis
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 19 Oct 2000
Article Copyright 2000 by Matthew Ellis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid