Tray programming full contact

zhaozj2021-02-11  225

Tray programming

First, the tray introduction

The so-called "tray", in the Windows system interface, refers to the part of the right side of the following task bars, system time, etc. When the program minimizes or hangs, but if you don't want to occupy the task bar, you can put the program in the tray area. In fact, the programming of the tray is very simple, and the following is a brief explanation ^ _ ^

Second, pallet programming related functions

In fact, the essence of the program is placed on the tray is first drawing an icon in the tray area, then hide the program, and then performs message processing on the icon of the tray.

Draw icons and determine the function of the message sent by the icon, that is, ------

Winshellapi Bool WinApi Shell_notifyicon

DWORD DWMESSAGE,

PNOTIFYICONDATA PNID

);

This function is responsible for delivering messages to the system to add, modify or delete the icon of the tray area.

Her return value is a Boolean type. That is to say, if it is returned 0, it is Cheng Ren, not 0 is successful.

The parameter dwMessage is which aspect indicating the application function of this function is added, deleted, or modify the icon. If it is added, its value is nim_add; delete is nim_delete; and the modification is NIM_MODIFY.

The parameter PNID is the structure of the specific and program in the tray area. It is defined as follows:

Typedef struct _notifyicondata {

DWORD CBSIZE;

Hwnd hwnd;

UINT UID;

UINT UFLAGS;

Uint ucallbackmessage;

Hicon Hicon;

Char sztip [64];

Notifyicondata, * PNOTIFYICIONDATA;

Below, each parameter of this structure is planed:

CBSIZE: The length of the structure, uses "bit" to do units. Generally in the program, we assign it with (DWORD) SIZEOF (Notifyicondata).

HWnd: A handle, if you operate on the icon in the tray, the corresponding message is passed to the window represented by this handle. Naturally, most case is this-> m_hwnd.

UID: Icon ID defined in the project

UFLAGS: This member marks the data of which members of which are valid, nif_icon, nif_message, nif_tip, representing a member of the data, respectively, is Hicon, UcallbackMessage, SZTIP. Of course, three values ​​can be used with "|". The following is an elaboration of members involved.

Hicon: To increase, delete or modify the icon handle. If you only know a UID, you may usually use a function loadicon to get your handle. For example, LoadCon (AFXGetInstanceHandle (), MakeintResource (iDR_mainframe)).

UcallbackMessage: This is a more important data member in the operation of the tray area. This is a message logo. When you operate on the tray area accordingly, you will pass the window represented by HWND. So, in UFLAGS, it is generally marked it is effective. It is generally customized here.

Sztip: The prompt of the mouse moves to the tray icon.

Third, pallet programming example

The basics of tray programming is also above. Below, we entered the specific practical exercise stage, and we will take a few pallet programming examples, deepen understanding.

1. Minimize the program to the function totray () of the system tray area.

Void ctimewakedlg :: Totray () {

Notifyicondata NID;

Nid.cbsize = (dword) SizeOf (Notifyicondata);

Nid.hwnd = this-> m_hwnd;

Nid.uid = idR_mainframe;

Nid.uflags = nif_icon | NIF_MESSAGE | NIF_TIP;

Nid.ucallbackMessage = WM_SHOWTASK; // Custom message name

Nid.hicon = loading (AfxGetInstanceHandle (), makeintResource (iDR_mainframe);

STRCPY (Nid.sztip, "Plan Task Reminder"); // Information Prompt Strip is "Scheduled Task Reminder"

Shell_notifyicon (NIM_ADD, & NID); // Add icon in the tray area

Showwindow (sw_hide); // Hide the main window

}

This is a very simple function, first assignments for Notifyicondata, then call shell_notifyicon, and one argument is NIM_ADD, indicates that it is added. Then use the function showwindow to hide the main window so that the program minimizes the program to the system tray area.

2, the program has been minimized to the tray area, but how about the operation of the pallet icon? This reflects the role of members UcallbackMessage for structural NOTIFYICONDATA. The role it provides is that when the user clicks the icon of the tray area (whether the left button or right is key), send a message to the window represented by the HWnd. If it is an example, the name of the message is WM_SHOWTASK. Add a message response function to a custom message based on the message mechanism of the VC.

Declaring message response function between // {AFX_MSG and //}} on header file: AFX_MSG:

AFX_MSG LRESULT ONSHOWTASK (WPARAM WPARAM, LPARAM LPARAM);

Then add a message mapping in the CPP file. Join between Begin_Message_Map and End_Message_MAP:

ON_MESSAGE (WM_SHOWTASK, ONSHOWTASK) maps the message and message response function.

Then add the implementation of the function onshowtask in the CPP file:

Lresult ctimewakedlg :: OnShowTask (WPARAM WPARAM, LPARAM LPARAM)

// WPARAM receives the ID of the icon, and LPARAM receives the behavior of the mouse.

{

IF (WPARAM! = IDR_MAINFRAME)

Return 1;

Switch (lparam)

{

Case WM_RBUTTONUP: / / Right-click the shortcut menu, only one "close"

{

LPPOINT LPOINT = New tagpoint;

:: getCursorpos (LPOINT); // Get the mouse position

CMenu Menu;

Menu.createPopupmentu (); // Declare a pop-up menu

// Add menu item "Close", click, send a message WM_DESTROY to the main window (already

// Hide, end the program.

Menu.Appendmenu (MF_String, WM_DESTROY, "Off");

/ / Determine the location of the pop-up menu

Menu.TrackPopupmenu (TPM_LEFTALIGN, LPOINT-> X, LPOINT-> Y, this);

//Recycle

HMENU HMENU = Menu.detach ();

Menu.destroymenu (); delete lpoint;

}

Break;

Case WM_LBUTTONDBLCLK: / / Double-click Left button

{

This-> showwindow (sw_show); // Simple display main window finished

}

Break;

}

Return 0;

}

After it, it is over, there is nothing to say anymore.

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

New Post(0)