Display icon on the taskbar

zhaozj2021-02-11  216

Display icon on the taskbar

Allen Studio Qiu Ji

The right side of the Windows 95 / Windows NT taskbar is a status area, Windows95 / Wi

NDOWSNT displays time and some small icons, which represent a specific function or

Program, click, right click or double-click these icons, will pop up function menu or program window

Port, etc. You may want to put your own program icon on the status area of ​​the taskbar, very simple,

VC 5.0 provides a function that makes it easy to implement this feature.

Shell_notifyicon () function

This function sends a message to the system in the state area, increase, delete, or modify the system in the taskbar.

Sign. Shell_notifyi con () has two parameters:

DWORD DWMESSAGE

The message logo value is one of the following three:

NIM_ADD

Add icons in the taskbar status area

NIM_DELETE deletes icons from the taskbar status area

NIM_MODITY Modify the icon of the taskbar status area

2. PNOTIFYICONDATA PNID

Pointer to the NOTIFYICONDATA structure. The NOTIFYICONDATA structure is as follows:

Typedef struct _notifyicondata {// NID

DWORD CBSIZE; // Notifyicondata Size

HWND HWND; // Receive the window handle of the callback message

UINT UID; / / Custom Icon Sign Value

UINT UFLAGS; / / The three data members behind // are valid,

// can be made from nif_icon, nif_message and

Nif_tip combination.

Uint ucallbackMessage; // callback message

Hicon Hicon; // Icon Handle

Char sztip [64]; // prompts characters. When the mouse is moved to the taskbar

// When the status area is on the icon, the system displays the character.

Notifyicondata, * PNOTIFYICIONDATA;

How to implement the program icon and its functions in the taskbar status area

1. Firstly create a single document project file.

2. Add a custom message in stdafx.h:

// Tune message

#define myWM_notifyicon WM_USER 1

/ / Show icon in the taskbar status area

#define mywm_showappAppiconic WM_USER 2

3. Add a definition message processing function in Mainfrm.h, which looks like

under:

protected:

// {{AFX_MSG (CMAINFRAME)

AFX_MSG Void OnsysCommand (uint nid, lparam lparam); //

Industrial plus

AFX_MSG Void Onmyiconnotify (WPARAM WPARAM, LPARAM LPARAM

);

AFX_MSG Void OnshowappAppiconic (WPARAM WPARAM, LPARAM LPARA

m); // Handmade

AFX_MSG void onclose (); // System generated

//}} AFX_MSG

Declare_message_map ()

In addition, you need to define a switch value in Mainfrm.h, and the recorder window is turned on or off.

status:

Bool BoolWndhadshow;

4. Implement message mapping and message processing functions in Mainfrm.cpp. Message mapping department

The points look as follows:

Begin_MESSAGE_MAP (CMAINFRAME, CFRAMEWND)

// {{AFX_MSG_MAP (CMAINFRAME)

ON_MESSAGE (MyWM_Notifyicon, Onmyiconnotify)

ON_MESSAGE (MyWM_SHOWAPPICONIC, ONSHOWAPPICONIC)

ON_WM_SYSCOMMAND ()

ON_WM_CLOSE ()

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

Message processing and related functions are as follows:

// Add icon in the taskbar status area

Bool MyTaskbaraddCdon (HCON HICON, LPSTR LPSZTIP)

{

BOOL RES;

Notifyicondata TNID;

Tnid.cbsize = sizeof (notifyicondata);

Tnid.hwnd = hwnd;

TNID.UID = UID;

Tnid.uflags = nif_icon | NIF_TIP | NIF_MESSAGE;

TNID.UCALLBACKMESSAGE = MYWM_NOTIFYICON;

TNID.HICON = HICON;

IF (LPSZTIP)

LSTRCPYN (TNID.SZTIP, LPSZTIP, SIZEOF (TNID.SZTIP));

Else

TNID.SZTIP [0] = '/ 0';

Res = shell_notifyico n (nim_add, & tnid);

IF (Hicon)

DESTROYICI (HICON);

Return res;

}

// Remove the icon from the task bar status area

BOOL MyTaskbardeleteicon (HWND HWND, UINT UID)

{

BOOL RES;

Notifyicondata TNID;

Tnid.cbsize = sizeof (notifyicondata);

Tnid.hwnd = hwnd;

TNID.UID = UID;

RES = shell_notifyicon (Nim_Delete, & TnID);

Return res;

}

// Process icon callback message MYWM_NOTIFYICON

Void CMAINFRAME :: Onmyiconnotify (WPARAM WPARAM, LPARAM L

PARAM)

{

UINT NID;

Uint umousemsg;

NID = (uint) WPARAM;

Umousemsg = (uint) LPARAM;

IF (umousemsg == wm_lbuttondown)

// click icon

{

IF (BoolWndhadShow) // If the window is opened, hide the window

ShowWindow (SW_HIDE);

Else

/ / Otherwise, the window is displayed

Showwindow (sw_shownormal);

BoolWndhadShow = ~ BoolWndhadShow;

}

}

// Message MYWM_SHOWAPPICONIC Processing function, display icon in the taskbar and hide

window

Void CMAINFRAME :: OnshowAppiconic (WPARAM WPARAM, LPARAM L

PARAM)

{

Hicon theicon = loading (AFXGETITINSTANCE-HANDLE (), Makeint

Resource (IDR_MAI NFRAME);

MytaskbaraddCon (GetsafehWnd (), 100, Theicon, _T ("Model

sequence"));

ShowWindow (SW_HIDE);

BoolWndhadShow = false;

}

When the program is closed, you need to delete the icon from the task-like state area, you can add a place

The function of the WM_CLOSE message is to delete the icon. Add Processing WM_ via ClassWizard

CLOSE's function onclose (), the function is as follows:

Void cmainframe :: onClose ()

{

// Todo: add your message handler code here and / or call

DEFAULT

MyTaskBardeleteicon (GetsafehWnd (), 100);

CframeWnd :: onClose ();

When the user minimizes the window, you should hide the entire window, so to process ON_SYSCO

Mmand message. First add a function description in Mainfrm.h, as shown in the third step; then in Ma

Message mapping and implementation functions in INFRM.CPP. Message mapping as shown in front, functions

Now the following:

Void CMAINFRAME :: OnSysCommand (uint nid, lparam lparam)

{

IF (NID == SC_MINIMIZE)

{

ShowWindow (SW_HIDE);

BoolWndhadShow = false;

}

Else

CWnd :: OnSysCommand (NID, LPARAM);

}

5. Adding a statement before the InitInstance () function returns

m_pmainwnd-> postmessage (MyWM_SHOWAP- Piconic);

It is sending a custom message MYWM_SHOWAP-Piconic to make the program start

Hide Program Window and displays a small icon in the taskbar status area.

6. Now you can compile and run the program. After the program is running, click on the taskbar

The icon of the program, the system will pop up the program window, then click it and hide it.

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

New Post(0)