VC ++ System Tray Icon implementation

Source: Internet
Author: User
From: http://blog.csdn.net/xian0617/article/details/5873248 C ++ System Tray Icon implementation classification:
My c ++ history 2010-09-09 14: 00281 people read comments (3) collect reports

There are several icons on the right side of the taskbar, such as the input method switch icon and volume control icon. In addition, we often encounter software with tray icons, such as Kingsoft, anti-virus software with real-time monitoring, etc. These software runs in the background, usually does not occupy too much screen resources, just put a small sign on the notification bar. If necessary, you can click the icon to perform menu operations or activate its main window. Sometimes our own program also hopes to have a similar effect. This article describes in detail how to design this kind of Tray Icon program with VC ++ 6.0.

---- The notifyicondata structure contains the information used by the system to process the tray icons, including the selected icons, callback messages, prompt messages, and corresponding windows of the icons. It is defined:

[CPP]
View plaincopyprint?

  1. // System-defined struct
  2. Typedef struct _ policyicondata
  3. {
  4. DWORD cbsize; // the size of the structure in bytes
  5. Hwnd; // handle for receiving the notification message from the tray icon
  6. Uint uid; // ID of the icon defined by the application
  7. Uint uflags; // set the attributes of the icon
  8. Uint ucallbackmessage; // the ID of the message defined by the application. The message is sent to hwnd.
  9. Hicon; // the handle of the icon
  10. Char sztip [64]; // The prompt message displayed when you move your cursor over the icon
  11. } Notifyicondata, * ppolicyicondata;
  12. /*
  13. In this structure, the uflags member can make one or more of the following combinations:
  14. The specified hicon member is valid.
  15. The value of the nif_message setting Member ucallbackmessage is valid.
  16. The error message returned when the member sztip is set to valid.
  17. */

Shell_policyicon Function

---- The global function shell_policyicon () is used to add, delete, or modify icons on the tray. Its prototype is:

[CPP]
View plaincopyprint?

  1. Winshellapi bool winapi shell_policyicon (DWORD dwmessage, pnotifyicondata pnid );
  2. /*
  3. --- Pnid is a pointer to the policyicondata structure; dwmessage is a transmitted message, which can be one of the following messages:
  4. Nim_add add icon
  5. Nim_delete Delete icon
  6. Nim_modify
  7. */

Use instance

1) add protection member variables in the frame or dialog to be processed

[CPP]
View plaincopyprint?

  1. Protected:
  2. Policyicondata m_tnid;

2) Add the wm_create message function for the corresponding frame or dialog, and add the code generated by the tray to the corresponding implementation function.

Define the message in the corresponding class declaration file, and add the code before return in the oncreate Function

[CPP]
View plaincopyprint?

  1. // Define the message in the corresponding class declaration File
  2. # Define mywm_policyicon wm_user + 1
  3. // Add the pallet Generation Code before return in the oncreate Function
  4. M_tnid.cbsize = sizeof (policyicondata );
  5. M_tnid.hwnd = This-> m_hwnd;
  6. M_tnid.uflags = nif_message | nif_icon | nif_tip;
  7. M_tnid.ucallbackmessage = mywm_policyicon;
  8. // User-defined callback message
  9. Cstring sztooltip;
  10. Sztooltip = _ T ("tray icon instance"); // text displayed when you move the mouse over the tray
  11. _ Tcscpy (m_tnid.sztip, sztooltip );
  12. M_tnid.uid = idr_mainframe;
  13. Hicon;
  14. Hicon = afxgetapp ()-> loadicon (idr_mainframe );
  15. M_tnid.hicon = hicon;
  16. : Shell_policyicon (nim_add, & m_tnid );
  17. If (hicon): destroyicon (hicon );

3) Compile message processing functions

---- To process the icon callback message, such as double-clicking with the left mouse button or right-clicking the message, we reload the windowproc () function. In addition, we also hope that the icon will not appear in the blank area of the taskbar when the main frame window is minimized, and the corresponding processing will be done in this function at the same time.

Do not forget to edit your own menu. Here idr_right_menu is used for the moment.
Flag

[CPP]
View plaincopyprint?

  1. Lresult *** frame/* or dialog */: windowproc (uint message, wparam, lparam)
  2. {
  3. Switch (Message ){
  4. Case mywm_policyicon:
  5. // For user-defined messages
  6. If (lparam = wm_lbuttondblclk)
  7. {
  8. // The main window appears when you double-click the mouse
  9. Afxgetapp ()-> m_pmainwnd-> showwindow (sw_show );
  10. }
  11. Else if (lparam = wm_rbuttondown ){
  12. // Right-click the pop-up menu
  13. Cmenu menu;
  14. Menu. loadmenu (idr_right_menu );
  15. // Load the predefined menu
  16. Cmenu * pmenu = menu. getsubmenu (0 );
  17. Cpoint Pos;
  18. Getcursorpos (& Pos );
  19. Pmenu-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, POS. X, POS. Y, afxgetmainwnd ());
  20. }
  21. Break;
  22. Case wm_syscommand:
  23. // If it is a system message
  24. Switch (wparam ){
  25. Case SC _minimize:
  26. // The main window is hidden when the minimum message is received
  27. Showwindow (sw_hide );
  28. Return 0;
  29. Break;
  30. Case SC _close:
  31. : Shell_policyicon (nim_delete, & m_nid); // Delete the system tray icon when it is disabled
  32. Break;
  33. }
  34. Break;
  35. }
  36. Return cframewnd/* or cdialog */: windowproc (message, wparam, lparam); // call the message processing function of the parent class.
  37. }

Now, we have implemented the common functions of the tray icon program. We can also call the shell_policyicon () function to change different status indicators,

There are several icons on the right side of the taskbar, such as the input method switch icon and volume control icon. In addition, we often encounter software with tray icons, such as Kingsoft, anti-virus software with real-time monitoring, etc. These software runs in the background, usually does not occupy too much screen resources, just put a small sign on the notification bar. If necessary, you can click the icon to perform menu operations or activate its main window. Sometimes our own program also hopes to have a similar effect. This article describes in detail how to design this kind of Tray Icon program with VC ++ 6.0.

---- The notifyicondata structure contains the information used by the system to process the tray icons, including the selected icons, callback messages, prompt messages, and corresponding windows of the icons. It is defined:

[CPP]
View plaincopyprint?

  1. // System-defined struct
  2. Typedef struct _ policyicondata
  3. {
  4. DWORD cbsize; // the size of the structure in bytes
  5. Hwnd; // handle for receiving the notification message from the tray icon
  6. Uint uid; // ID of the icon defined by the application
  7. Uint uflags; // set the attributes of the icon
  8. Uint ucallbackmessage; // the ID of the message defined by the application. The message is sent to hwnd.
  9. Hicon; // the handle of the icon
  10. Char sztip [64]; // The prompt message displayed when you move your cursor over the icon
  11. } Notifyicondata, * ppolicyicondata;
  12. /*
  13. In this structure, the uflags member can make one or more of the following combinations:
  14. The specified hicon member is valid.
  15. The value of the nif_message setting Member ucallbackmessage is valid.
  16. The error message returned when the member sztip is set to valid.
  17. */

Shell_policyicon Function

---- The global function shell_policyicon () is used to add, delete, or modify icons on the tray. Its prototype is:

[CPP]
View plaincopyprint?

  1. Winshellapi bool winapi shell_policyicon (DWORD dwmessage, pnotifyicondata pnid );
  2. /*
  3. --- Pnid is a pointer to the policyicondata structure; dwmessage is a transmitted message, which can be one of the following messages:
  4. Nim_add add icon
  5. Nim_delete Delete icon
  6. Nim_modify
  7. */

Use instance

1) add protection member variables in the frame or dialog to be processed

[CPP]
View plaincopyprint?

  1. Protected:
  2. Policyicondata m_tnid;

2) Add the wm_create message function for the corresponding frame or dialog, and add the code generated by the tray to the corresponding implementation function.

Define the message in the corresponding class declaration file, and add the code before return in the oncreate Function

[CPP]
View plaincopyprint?

  1. // Define the message in the corresponding class declaration File
  2. # Define mywm_policyicon wm_user + 1
  3. // Add the pallet Generation Code before return in the oncreate Function
  4. M_tnid.cbsize = sizeof (policyicondata );
  5. M_tnid.hwnd = This-> m_hwnd;
  6. M_tnid.uflags = nif_message | nif_icon | nif_tip;
  7. M_tnid.ucallbackmessage = mywm_policyicon;
  8. // User-defined callback message
  9. Cstring sztooltip;
  10. Sztooltip = _ T ("tray icon instance"); // text displayed when you move the mouse over the tray
  11. _ Tcscpy (m_tnid.sztip, sztooltip );
  12. M_tnid.uid = idr_mainframe;
  13. Hicon;
  14. Hicon = afxgetapp ()-> loadicon (idr_mainframe );
  15. M_tnid.hicon = hicon;
  16. : Shell_policyicon (nim_add, & m_tnid );
  17. If (hicon): destroyicon (hicon );

3) Compile message processing functions

---- To process the icon callback message, such as double-clicking with the left mouse button or right-clicking the message, we reload the windowproc () function. In addition, we also hope that the icon will not appear in the blank area of the taskbar when the main frame window is minimized, and the corresponding processing will be done in this function at the same time.

Do not forget to edit your own menu. Here idr_right_menu is used for the moment.
Flag

[CPP]
View plaincopyprint?

  1. Lresult *** frame/* or dialog */: windowproc (uint message, wparam, lparam)
  2. {
  3. Switch (Message ){
  4. Case mywm_policyicon:
  5. // For user-defined messages
  6. If (lparam = wm_lbuttondblclk)
  7. {
  8. // The main window appears when you double-click the mouse
  9. Afxgetapp ()-> m_pmainwnd-> showwindow (sw_show );
  10. }
  11. Else if (lparam = wm_rbuttondown ){
  12. // Right-click the pop-up menu
  13. Cmenu menu;
  14. Menu. loadmenu (idr_right_menu );
  15. // Load the predefined menu
  16. Cmenu * pmenu = menu. getsubmenu (0 );
  17. Cpoint Pos;
  18. Getcursorpos (& Pos );
  19. Pmenu-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, POS. X, POS. Y, afxgetmainwnd ());
  20. }
  21. Break;
  22. Case wm_syscommand:
  23. // If it is a system message
  24. Switch (wparam ){
  25. Case SC _minimize:
  26. // The main window is hidden when the minimum message is received
  27. Showwindow (sw_hide );
  28. Return 0;
  29. Break;
  30. Case SC _close:
  31. : Shell_policyicon (nim_delete, & m_nid); // Delete the system tray icon when it is disabled
  32. Break;
  33. }
  34. Break;
  35. }
  36. Return cframewnd/* or cdialog */: windowproc (message, wparam, lparam); // call the message processing function of the parent class.
  37. }

Now, we have implemented the common functions of the tray icon program. We can also call the shell_policyicon () function to change different status indicators,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.