() Using the windows program --Shell_NotifyIcon function C

Shell_NotifyIcon function uses

1.Shell_NotifyIcon Introduction

== Shell_NotifyIcon Windows API function is used to generate the system tray icon ==

Prototype:

BOOL Shell_NotifyIcon(
	   DWORD              dwMessage,
	   PNOTIFYICONDATA    lpdata
	   );

Parameters (dwMessage, Ipdata)

1. dwMessage:[输入参数] 说明要执行的动作。动作的可选值如下

NIM_ADD add an icon to the tray
NIM_DELETE delete an icon from the tray
NIM_MODIFY modify the icon
NIM_SETFOCUS focus will return tray. This message is usually sent after completion of the operation in a user interface tray. For example, a tray icon to display a shortcut menu, and then the user presses the ESC key operation, which is the use NIM_SETFOCUS the focus remain on tray. This can only be used in the system shell and common control DLL5.0 version of the above.
NIM_SETVERSION specify a particular version of the system control housing and the conventional DLL. The default value is 0, win95 indication mode. The only system shell and common control DLL5.0 or later can be used.

  2.lpdata:【输入参数】 一个指向NOTUFYICONDATA结构的指针。
	结构说明如下:
	typedef struct _NOTIFYICONDATA {
	DWORD     cbSize;
	HWND      hWND;
	UINT      uFlags;
	UINT      uCallbackMessage;
	HICON     hIcon;
	TCHAR     szTip[64];
	DWORD     dwState;
	DWORD     dwStateMask;
	TCHAR     szInfo [256];
	union {
		   UINT 	uTimeout;
		   UINT 	uVersion;
	  };
	TCHAR     szInfoTitle[64];
	DWORD     dwInfoFlags;
	GUID      guidItem;
	} NOTIFYICONDATA, *PNOTIFYICONDATA;
cbSize 以字节记的结构大小,以适应不同版本。
hWnd 接收Windows消息的窗口句柄。
uID 托盘图标的ID
uFlags 指示结构中的哪些成员包含有效数据,可选值: NIF_ICON, NIF_MESSAGE,NIF_TIP,NIF_STATE, NIF_INFO, NIF_GUID.
uCallbackMessage 回调消息ID, 由用户自定义。与一个自定义的消息处理函数关联。
hIcon 托盘图标句柄。
szTip 托盘图标的提示字符串。
注意:以下数据成员仅限系统外壳与常用控制DLL5.0及以上版本才有效。
dwState 图标的状态:NIS_HIDDEN-隐藏, 或NIS_SHAREDICON-可视。
dwStateMask 图标状态掩码,用以设置dwState
szInfo 气球型提示 (Balloon ToolTip) 的字符串。
uTimeout 以毫秒计的提示显示时间
uVersion 确定所依赖的版本。 0-Win95, NOTIFYICON_BERSION-Win2000
SzInfoTitle 气球型提示的标题
dwInfoFlags 设置气球型提示所用的图标(类似MessageBox中所使用的图标):
 	NIIF_ERROR  错误
	NIIF_INFO 信息
	NIIF_NONE 没有图标
	NIIF_WARNING 警告
	NIIF_ICON_MASK 6.0版本保留
	NIIF_NOSOUND 限6.0版本, 不播放对应的声音
guidItem 6.0版本保留

Use ideas:

I click on the window is added to minimize the incidents of this function, for example, I click on the minimize and then produced a tray at a program icon.
1. First define a global variable:
the NOTIFYICONDATA e
Here Insert Picture Description
2. e then initialized in place, I am in a while (GetMessage (& msg, NULL , 0, 0)) of the main window before the initialization statement.
Here Insert Picture Description
3. Finally, add the function minimized: Shell_NotifyIcon (NIM_ADD, & e) ;

Detailed code below (pro-test):

#include <windows.h>  
#include <tchar.h>  
#include "ShellAPI.h"  
#include "resource1.h"  
#include <string.h>

#define WM_IAWENTRAY    WM_USER+5  //系统托盘的自定义消息  

//定义全局变量  
HINSTANCE g_hInst;
NOTIFYICONDATA e;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR szCmdLine, int iCmdShow)
{
	g_hInst = hInstance; //获取该程序实例句柄  

	static TCHAR szAppName[] = TEXT("TimeWork");
	HWND         hwnd;
	MSG          msg;
	WNDCLASS     wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName,                  // window class name  
		TEXT("托盘程序"), // window caption  
		WS_OVERLAPPEDWINDOW,        // window style  
		CW_USEDEFAULT,              // initial x position  
		CW_USEDEFAULT,              // initial y position  
		CW_USEDEFAULT,              // initial x size  
		CW_USEDEFAULT,              // initial y size  
		NULL,                       // parent window handle  
		NULL,                       // window menu handle  
		hInstance,                  // program instance handle  
		NULL);                     // creation parameters  

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	//初始化NOTIFYICONDATA结构  
	e.cbSize = sizeof(NOTIFYICONDATA);
	e.hWnd = hwnd;
	e.uID = IDI_ICON1;
	e.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	e.uCallbackMessage = WM_IAWENTRAY;
	e.hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
	strcpy_s(e.szTip,20,"我太难了!"); //添加的托盘提示符字符串,鼠标放在托盘处会显示提示文字
//	_tcscpy(e.szTip, TEXT("TimeWork正在工作!\r\n点击打开主界面")); //_tcscpy是windows宏,需包含头文件tchar.h  

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc;
	PAINTSTRUCT ps;
	RECT        rect;

	switch (message)
	{
	case WM_CREATE:
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		GetClientRect(hwnd, &rect);

		DrawText(hdc, TEXT("Hello, my fridents!"), -1, &rect,
			DT_SINGLELINE | DT_CENTER | DT_VCENTER);

		EndPaint(hwnd, &ps);
		return 0;

	case WM_IAWENTRAY:
		if (wParam == IDI_ICON1) {
			if (lParam == WM_LBUTTONDOWN) {
				//还可以在这个地方加入一个菜单,鼠标右键单击时显示  
				ShowWindow(hwnd, SW_SHOWNORMAL);
				return TRUE;
			}
		}
		return FALSE;
	case WM_SYSCOMMAND:
		switch (wParam)
		{
		case SC_CLOSE:
			DestroyWindow(hwnd);
			PostQuitMessage(0);
			break;
		case SC_MINIMIZE:
			ShowWindow(hwnd, SW_HIDE);
			Shell_NotifyIcon(NIM_ADD, &e); //最小化时隐藏窗口并设置系统托盘  
			break;
		}
		return 0;

	case WM_DESTROY:
		Shell_NotifyIcon(NIM_DELETE, &e);
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam); 

}

Specific project file, see: https: //download.csdn.net/download/sf9090/11884292
Note: icons which add their own resources
will not see the added resources of large cattle blog: https: //blog.csdn.net/nullccc/article / details / 81352470
herein by reference: HTTPS: //www.cnblogs.com/duzouzhe/archive/2010/04/08/1707050.html
https://blog.csdn.net/say_high/article/details/11092961

Published 14 original articles · won praise 23 · views 3295

Guess you like

Origin blog.csdn.net/sf9090/article/details/102640368