VC programming production system tray program

xiaoxiao2021-04-10  344

Some of the Windows operating systems do not display the run window, only displaying an icon on the taskbar, indicating that the program is running, the user can interact with the application, such as the application of the Jinshan drug tyrants, we sometimes need to be prepared Some similar programs running only in the background, in order not to interfere with the running interface of the front program and the unnecessary window, the main window when the program is run is invisible. At the same time, an icon is displayed in the right end of the taskbar and responds to the user's mouse action. Here is the design method of Visual C development such a program.

First, the main window of the hidden program

First, to make the program's main window is invisible, and do not appear on the taskbar, do these two points, you need to set the style and extended style of the main Border window, respectively:

Bool CMAINFRAME :: PrecreateWindow (CreateStruct & Cs)

{

cs.style = ws_popup; // Make the main window

cs.dwexstyle | = WS_EX_TOOLWINDOW; / / Do not display the task button

Return CFrameWnd :: PrecreateWindow (CS);

}

Second, join the icon that represents the program to join the taskbar

The above functions are called in the CMAINFRAME :: oncreate () function of the main frame window, you can display the icon on the task bar to use the system API function shell_notifyicon () to display an icon in the notification area of ​​the taskbar. The prototype of this function is: Before calling the function, you need to determine the value of its parameters. Where the first parameter of the shell_notifyicon () function is a predefined message, you can take one of the values: NIM_ADD, NIM_DELETE, or NIM_MODIFY, indicate the add icon, delete icon, or modify icon. Another parameter is a pointer to the Notifyicondata type. Its prototype is:

Typedef struct _notifyicondata {

DWORD CBSIZE;

Hwnd hwnd;

UINT UID;

UINT UFLAGS;

Uint ucallbackmessage;

Hicon Hicon;

Charsztip [64];

Notifyicondata

In a member of this structure, CBSIZE is the number of bytes of this structure, and hWnd is a handle of the window that accepts the message issued by the icon (the mouse will make a message when the icon is on the taskbar icon, this message user To define itself), the UID is the ID of the displayed icon, UFlags indicates whether the value of the remaining members (Hicon, UcallbackMessage, and Sztip) is valid, uCallbackMessage is a user-defined message, when the user acts on the icon Some mouse When the action, the icon will send the message to the main frame window of the application (window specified in the HWND member),. Hicon is a string that will be displayed on the task bar, and the SZTIP mouse is displayed on the icon.

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

Notifyicondata TND;

Tnd.cbsize = sizeof (notifyicondata);

Tnd.hwnd = this-> m_hwnd;

TND.UID = IDR_MAINFRAME;

Tnd.uflags = nif_message | NIF_ICON | NIF_TIP;

TND.UCALLBACKMESSAGE = WM_MYMESSAGE;

File: // User-defined message, ie message sent by the mouse on the taskbar on the program icon on the program Tnd.hicon = loadicon (AfxGetInstanceHandle (), makeintResource (iDR_mainframe);

STRCPY (TND.SZTIP, Test Program "); // Icon prompt to" Test Programs "

Shell_Notifyicon (NIM_ADD, & TND); / / Add icons to the taskbar

}

Third, the implementation of users and procedures

The user interacts, that is, when the user is clicked or double-click the left mouse button or right-click, at least the willingness to respond to the user's will. It has been mentioned that when the user performs a mouse action on the icon, a custom message will be issued to the window specified in the HWND member, which is the WM_Myessage specified by the UCALLBACKMESSAGE, which is WM_USER 101 (how to customize the message) ,I will not say much). We have to achieve the task is to respond to custom messages in the HWND window:

Void CMAINFRAME :: ONMYMESSAG (WPARAM WPARAM, LPARAM LPARAM)

{

UINT UID; / / Issue ID of the icon of the message

Uint umousemsg; // mouse action

Point pt;

UID = (uint) wparam;

Umousemsg = (uint) LPARAM;

IF (umousemsg == wm_rbuttondown) // If you are right-click

{

Switch (UID)

{

CASE IDR_MAINFRAME: // If it is our icon

GetCursorpos (& PT); // Get the mouse position

AFXGetApp () -> m_pmainwnd-> showwindow (sw_shownormal); // Display Program Window

Break;

DEFAULT:

}

}

Return;

}

Fourth, delete the program icon at the end of the program

When the program ends, you need to delete the icon in the notification area. At this time, the shell_notifyicon function should be called, but the first parameter is Nim_Delete that deletes the icon:

Void CMAINFRAME :: ~ cmainframe ()

{

Notifyicondata TNID;

Tnid.cbsize = sizeof (notifyicondata);

TNID.hwnd = this-> m_hwnd;

TNID.UID = idR_mainframe; / / Guaranteed to delete our icon

Shell_Notifyicon (Nim_Delete, & TnID);

}

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

New Post(0)