Example of a SysTray App in Win32
2012-11-28 13:11
447 查看
Example of a SysTray App in Win32
By Mark Zitnik,13 May 2007
μ 2.81, σa 2.92 [?] |
Download source - 21.2 KB
Introduction
Have you ever wondered how to create a cool application that runs in the background like a screen capture?About the demo
The demo is a basic systray application with a popup menu and disable/enable option. It is the basic skeleton for anyone who wants to create a systray application.
How to create a systray (taskbar) application
Include the ShellAPI library
Collapse |
Copy Code
#include <shellapi.h>
Init the NOTIFYICONDATA struct
Collapse |
Copy Code
nidApp.cbSize = sizeof(NOTIFYICONDATA); nidApp.hWnd = (HWND) hWnd; nidApp.uID = IDI_SYSTRAYDEMO; nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nidApp.hIcon = hMainIcon; nidApp.uCallbackMessage = WM_USER_SHELLICON; LoadString(hInstance, IDS_APPTOOLTIP,nidApp.szTip,MAX_LOADSTRING);
Show the systary icon
Collapse |
Copy Code
Shell_NotifyIcon(NIM_ADD, &nidApp);
Response to the message
Now our application gets a callback message when the mouse is moving over the systray icon. In our window callback function:Collapse |
Copy Code
switch (message) { case WM_USER_SHELLICON: // systray msg callback switch(LOWORD(lParam)) { case WM_RBUTTONDOWN:
Now we are monitoring the right button click.
Create a dynamic popup menu
Collapse |
Copy Code
UINT uFlag = MF_BYPOSITION|MF_STRING; GetCursorPos(&lpClickPoint); hPopMenu = CreatePopupMenu(); InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_ABOUT,_T("About")); if ( bDisable == TRUE ) { uFlag |= MF_GRAYED; } InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_TEST2,_T("Test 2")); InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_TEST1,_T("Test 1")); InsertMenu(hPopMenu,0xFFFFFFFF,MF_SEPARATOR,IDM_SEP,_T("SEP")); if ( bDisable == TRUE ) { InsertMenu(hPopMenu,0xFFFFFFFF, MF_BYPOSITION|MF_STRING,IDM_ENABLE,_T("Enable")); } else { InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_DISABLE,_T("Disable")); } InsertMenu(hPopMenu,0xFFFFFFFF,MF_SEPARATOR,IDM_SEP,_T("SEP")); InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_EXIT,_T("Exit")); SetForegroundWindow(hWnd); TrackPopupMenu(hPopMenu,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_BOTTOMALIGN, lpClickPoint.x, lpClickPoint.y,0,hWnd,NULL);
When the application is closed
We need to delete the systray.Collapse |
Copy Code
Shell_NotifyIcon(NIM_DELETE,&nidApp);
A word from the author
More information can be found from the MSDN site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shell_notifyicon.asp.License
This article, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)
About the Author
Mark Zitnik |
相关文章推荐
- win32 Advanced customization of WebBrowser Control in Dialog based app.
- Example code of searching in WordNet Dictionary with JAWS library
- Android之JNI ERROR (app bug): accessed stale global reference 0xb39533f2 (index 19708 in a table of s
- How Synchronization works in Java ? Example of synchronized Block and Method
- example of python operator overloadind
- Rails Web App Learning in action (1)--the basic version of students selective courses
- Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your a
- Tips of Programming in Google App Engine
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- Example of util:properties in Spring
- Example of Using Google Maps Service and GPS in Android
- Example of Unix commands implemented in Java
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- Example of how to use both JDK 7 and JDK 8 in one build.--reference
- 10 points about instanceof operator in Java with example
- Example of Sending email with attachments in C#
- how to view printf output in win32 app on visual studio 2010?
- The value of CFBundleShortVersionString in your WatchKit app's Info.plist (1.0) does not match
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
- Including a Django app's url.py is resulting in a 404 (The current URL, rango/, didn't match any of)