Design tray icon program with VC

xiaoxiao2021-04-10  569

In the Windows operating system, the right side of the taskbar will stand a few icons, such as input method switch icons, volume control icons, etc., we often encounter software with tray icon, such as Jinshan word, real-time monitoring function Anti-virus software, etc. These software run in the background, usually do not occupy too much screen resources, only put a small sign on the notification bar, if necessary, we can click on the icon with the mouse to play the menu or activate its main window. Sometimes our own procedures have also hoped to have similar results, this article will detail the method of designing tray icon programs with VC.

First, Notifyicondata structure

The Notifyicondata structure contains information for processing tray icons, which includes selection icons, callback messages, prompt messages, and icons corresponding to windows. It is defined as:

Typedef struct-notifyicondata {

DWORD CBSIZE;

// The size of this structure in bytes

Hwnd hwnd;

/ / Receive the window handle of the tray icon notification message

UINT UID;

// The ID number of the icon defined by the application

UINT UFLAGS;

/ / Set the attribute of this icon

Uint ucallbackmessage;

// Application defined message ID number, this message is passed to hwnd

Hicon Hicon; // Icon handle

Char sztip [64]; // The mouse is displayed on the icon.

NOTIFYICONDATA, PNOTIFYICONDATA;

In this structure, member uflags can be one of the following combinations or one:

Nif_icon: Setting members hicon valid

Nif_Message: Setting members uCallbackMessage is valid

Nif_tip: Setting member SZTIP is valid

Second, the shell_notifyicon function

Global Functions Shell_Notifyicon () is used to add, delete, or modify icons on the tray. Its prototype is:

Winshellapi Bool WinApi Shell_Notify, PNOTIFYICIONDATA PNID;

PNID is the pointer to the NOTIFYICONDATA structure above.

DwMessage is a message that is passed, which can be one of the following messages:

NIM_ADD: Add icon

NIM_DELETE: Remove Icon

NIM_MODIFY: Modify Icon

Third, pallet icon program design example

First we create an application TRAY that is not based on documentation and view structures with AppWizard. We don't want to display the main window while the application is started, so you need to delete the following two codes of member functions in the application class CTRAYAPP:

Pframe-> ActivateFrame ();

Pframe-> showwindow (sw_show);

Add the CMAINFRAME to the protection member variable m_tnid of the Notifyicondata structure, and add the code for generating the tray icon before the RETURN statement in its oncreate function:

m_tnid.cbsize = sizeof (notifyicondata);

m_tnid.hwnd = this-> m_hwnd;

M_tnid.uflags = nif_message | NIF_ICON | NIF_TIP;

M_Tnid.ucallbackMessage = mywm_notifyicon;

// User-defined callback message

Cstring sztooltip;

Sztooltip = _t ("Tray Icon Speed";

_TCSCPY (m_tnid.sztip, sztooltip);

m_tnid.uid = idR_mainframe;

Hicon Hicon;

Hicon = AFXGETAPP () -> loadicon (idR_mainframe); m_tnid.hicon = HICON;

:: shell_notifyicon (nim_add, && m_tnid);

IF (Hicon) :: Destroyicon (HICON);

The ID of the return message should be defined in the header function of the main framework:

#define myWM_notifyicon WM_USER 1

In order to process the icon return message, such as the left mouse button, right-click the message, we reload the WindowProc () function. In addition, we also hope that the icon does not appear at the blank area of ​​the taskbar when the main frame window is minimized, and it works simultaneously in this function.

Lresult CMAINFRAME :: WindowProc (Uint Message, WPARAM WPARAM, LPARAM LPARAM)

{

Switch (Message) {

Case MyWM_Notifyicon:

// If it is a user-defined message

IF (lparam == wm_lbuttondblclk) {

/ / The main window appears when the mouse doubles

AFXGetApp () -> m_pmainwnd-> showwindow;

}

Else if (lparam == wm_rbuttondown) {// mouse button Click the pop-up menu

CMenu Menu;

Menu.LoadMenu (idR_right_menu); // Load predetermined menu

CMenupmenu = menu.getsubmenu (0);

Cpoint Pos;

GetCursorpos (&& POS);

Pmenu-> TRACKPOPUPMENU (TPM_LEFTALIGN | TPM_RightButton, Pos.x, POS.Y, AFXGETMAINWND ());

}

Break;

Case WM_SYSCOMMAND:

// If it is a system message

IF (wPARAM = = SC_MINIMIMIZE) {

/ / Hide hidden when receiving minimization messages

AFXGetApp () -> m_pmainwnd-> showwindow (sw_hide);

Return 0;

}

Break;

}

Return CFrameWnd :: WindowProc (Message, WPARAM, LPARAM);

}

In order to make the app exit, the icon disappears, maps the WM_DESTROY message, and adds in the onDestroy () function:

:: shell_notifyicon (nim_delete, && m_tnid);

At this point, we have implemented the routine features of the tray icon program. We can also achieve changes in different states through the call of the shell_notifyicon () function.

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

New Post(0)