Transform icon like QQ

zhaozj2021-02-17  72

Transform icon like QQ

Before writing the code, let me introduce a function.

Above below MSDN2002

Shell_notifyicon function

-------------------------------------------------- ------------------------------

Send a message to the status area of ​​the missionaro

Bool Shell_Notifyicon (DWORD DWMESSAGE,

PNOTIFYICONDATA LPDATA

);

parameter

DWMessage Introduced DWORD parameters Develop actions to occur. It can be the following value NIM_ADD to add a icon to the status area. The HWND and UID of the Notifyicondata structure indicate that the icon will operate. NIM_DELETE Remove Icon. NIM_MODIFY Modification Icon NIM_SETFOCUS5.0 version can be used, this version can be found on Windows 2000, NT, 98/95 is an old version. Returns Focus to the Taskbar Notification Area (setting the focus to the taskbar message area). If the taskbar displays a menu, the user wants to cancel him with ESC, and use the NIM_SETFOCUS parameter to return to the message area.

NIM_SETVERSION 5.0 version indicates that the behavior of the taskbar is consistent with the version, and the version of the parameter can be found in the Notifyicondata structure.

LPDATA is incorporated into the address of the Notifyicondata structure.

Notifyicondata structure ------------------------------------------------ -------------------------------- contains the message handling system status area of ​​the taskbar. Syntax TypeDef struct _notify; // Structural size hWnd hWnd; // Accept the window UID UID of the taskbar message; // Application defined icon ID, when there are multiple icons, this ID does not mean a certain one must be redefined One (see code) uint uflags; // Mask points to those members valid (specifically see) uint ucallbackMessage; Hicon Hicon; Tchar Sztip [64]; DWORD DWSTATATE; DWORD DWSTATEMASK; TCHAR SZINFO [256]; union {uint Utimeout; uint Uversion;}; tchar szinfotitle [64]; dword dwinfoflags; guiding guiditem;} notifyicondata, * pNOTICONDATA;

Uflags

NIF_ICON UCALLBACKMESSAGE NIF_TIP SZTIP Valid Nif_State DWStatemask Valid NIF_INFO Use Bubble Tips Replacement Standard Tips Szinfo, Utimeout, Szinfotitle, DWINFOFLAGS Valid Nif_GuID Reserved

UcallbackMessage application definition message, the system uses it to handle the message of the taskbar status area. The HiCon characters status area icon, the version is different, the color bits of the icon are different. SZTIP string prompt. Version is different from the string length. 5.0 is 128 char. 4.0 is 64.

DWSTATE Icon Status 5.0 NIS_HIDDEN Hide NIS_Sharedicon Display DWSTATEMASK 5.0 Specifies the status of the change to be modified. For example, use NIS_HIDDEN to hide the icon. The Szinfo5.0 version is used for the air bubble tips for the longest 255 char. Utimeout millisecond delay value. Uversion specifies the title 63 of the version value Szinfotitle bubble tips 63. DWINFOFLAGS5.0 version. Flags add icons to bubble tips. The following values ​​NIIF_ERRORAn error icon.NIIF_INFOAn information icon.NIIF_NONENo icon.NIIF_WARNINGA warning icon.NIIF_ICON_MASKVersion 6.0. Reserved.NIIF_NOSOUNDVersion 6.0. Do not play the associated sound. Applies only to balloon ToolTips.guidItem6.0 version retained. The following introduction code VC6 Win32 first must ensure that set the shell32.lib in Project Setting / Link Library

// stdafx. Add #include

#define Normal 1 # Define Run 2 # define id_timer 1

#define uWM_notifyicon WM_USER 1

//trayicon.cpp// trayicon.cpp: defines the entry point for the application.//

#include "stdafx.h" #include "resource.h"

LResult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM);

Hinstance Hinst; Hicon Hicon [2]; uint uStatus = NORMAL; // Program Run Status Normal Icon Does Not Change RUN Icon Change ICION = 0; // Indicates But I'm used

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {// declare varibalestatic TCHAR szAppName [] = TEXT ( "tray icon"); TCHAR szCaption [] = TEXT ( "tray icon example of use"); HWND HWnd; msg msg; wndclass wndclass;

Hinst = hinstance;

HiCon [0] = Loadicon (Hinstance, MakeintResource); Hicon [1] = Loadicon (Hinstance, MakeintResource (IDi_ICON2));

WNDCLASS.STYLE = CS_HREDRAW | CS_VREDRAW; WNDCLASS.LPFNWNDPROC = WNDPROC; // Window Process address WNDCLASS.CBCLSEXTRA = 0; // Specify an additional assignment to WNDCLASS.cbWndextra = 0; // Specify additional byte assignment examples wnd wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) (GetStockObject (WHITE_BRUSH)); wndclass.lpszClassName = szAppName; wndclass.lpszMenuName = NULL; if (! RegisterClass (& wndclass)) {// ErrorMessageBox (); return 0;} // default window and the main window is not visible style no longer taskbar hwnd = CreateWindowEx (WS_EX_TOOLWINDOW, szAppName, szCaption, WS_OVERLAPPEDWINDOW | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, SW_SHOW); UpdateWindow (hwnd); 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; Notifyicondata TND;

tnd.cbSize = sizeof (NOTIFYICONDATA); tnd.hWnd = hwnd; strcpy (tnd.szTip, TEXT ( "sample tray")); tnd.uCallbackMessage = UWM_NOTIFYICON; tnd.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; tnd.uID = IDi_ICON; // There is definition in front, beware not Icon IDi_ICON1 or IDi_ICON2

Switch (Message) {// Timer Event Change Case WM_TIMER: IF (IICON == 0) IICON ; elseiicon -; tnd.hicon = Hicon [IICON]; Shell_Notifyicon (NIM_MODIFY, & TND); RETURN 0; Case WM_COMMAND : Switch (WPARAM)) {// Exit Case IDM_APP_EXIT: SendMessage (hwnd, wm_close, 0, 0); return 0; // Start Timercase IDM_Run: IF (SetTimer (HWND, ID_TIMER, 500, NULL) USTATUS = Run; return 0; // Stop Timercase IDM_Stop: KillTimer (hwnd, id_timer); IICON = 0; tnd.hicon = Hicon [IICON]; Shell_Notifyicon (Nim_Modify, & TND); uStatus = Normal; Return 0;} Return 0; / / Custom Event Handling Mouse on Trayicon Click UWM_Notifyicon: // If it is right-click on if ((uint) lParam == WM_RBUTTONDOWN) {// If it is idi_iconif ((uint) wPARAM == idi_icon) {Point PT HMENU HMENU, HSUBMENU;

hmenu = LoadMenu (hinst, MAKEINTRESOURCE (IDR_MENU1)); hsubmenu = GetSubMenu (hmenu, 0); // obtaining the current cursor position GetCursorPos (& pt); SetForegroundWindow (hwnd); if (uStatus == NORMAL) {EnableMenuItem (hmenu, IDM_STOP , MF_GRAYED); EnableMenuItem (hmenu, IDM_RUN, MF_ENABLED);} else {EnableMenuItem (hmenu, IDM_RUN, MF_GRAYED); EnableMenuItem (hmenu, IDM_STOP, MF_ENABLED);} // pop-up menu TrackPopupMenu (hsubmenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x , Pt.y, 0, hwnd, null; destroymenu (HMENU);}} Return 0; // When the form is generated in the tray area display icon Case WM_CREATE: TND.HICON = Hicon [0]; shell_notifyicon (NIM_ADD, & TND ); ruln 0; case wm_paint: hdc = beginpaint (hwnd, & ps); endpaint (hwnd, & ps); return 0; // Exit making removal icon Case WM_Close: shell_notifyicon (Nim_Delete, & Tnd); postquitMessage (0); return 0 ;} return DefWindowProc (hwnd, message, wParam, lParam);} / * void ErrorMessageBox () {LPVOID lpMsgBuf; FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (Lptstr) & lpmsg BUF, 0, NULL; MessageBox (NULL, (LPCTSTR) lpmsgbuf, "error", MB_OK | MB_ICONIONFORMATION

LocalFree (lpmsgbuf);} * /

Code download

my home page

转载请注明原文地址:https://www.9cbs.com/read-31131.html

New Post(0)