MFC System tray icon Implementation (reprint)

Source: Internet
Author: User

First of all, it must be known that a struct notifyicondata, which is MFC contains the system needs to pass the pallet area message of the information structure, with it, we today
The task of the day is completely easy to complete!

At this point, we can declare in our class that a NOTIFYICONDATA member variable is m_nid.

Next, we can assign our desired value to this struct variable, and remember that the assignment statement is written in the OnInitDialog initialization window function, not in the constructor
Number, otherwise it will be invalid.
m_nid.cbsize = sizeof (NOTIFYICONDATA);
M_nid.hwnd = m_hwnd;
M_nid.uid = Idr_mainframe;
M_nid.uflags = nif_message| nif_icon| Nif_tip;
M_nid.ucallbackmessage = Wm_systemtray; Custom messages
M_nid.hicon = M_hicon;
strcpy (M_nid.sztip, "Authentication system client");
:: Shell_NotifyIcon (Nim_add, &m_nid);
Then, let's parse this piece of code in a sentence,
m_nid.cbsize = sizeof (NOTIFYICONDATA);
Cbsize represents the size of the struct, in bytes not units, where the NOTIFYICONDATA structure standard size is assigned, and the sizeof function can be used to obtain its bytes.
Size
M_nid.hwnd = m_hwnd;
HWND is the window handle you want to give the Tray, MFC's window class because all inherit the CDialog class, so there will be a member variable is m_hwnd holds the current real
Example window's specific handle, we assign it to it on the line
M_nid.uid = Idr_mainframe;
The UID refers to the identifier of the application-defined taskbar icon, which is simply the ID of the image icon, where you can enter the ResourceView of the project.
Locate the icon under the Icon folder, where the ID number is the specific Id,shell_notifyicon function call we are assigning now, and the HWND and UID members are used to indicate
Specific to the operation of the icon, can be multiple calls, to implement different UID to link multiple icons to a window hwnd, so as to achieve the effect of tray icon switching
M_nid.uflags = nif_message| nif_icon| Nif_tip;
Uflags indicates which other members are legitimate data, simply said that only the variables mentioned in the Uflags, can play its role, the specific uflags can be
Some of the following member groups:
Nif_icon indicates that HICON members work.
Nif_message indicates that Ucallbackmessage members work
Nif_tip indicates that Sztip members work
Of course, there are many other, such as: Nif_state,nif_info,nif_guid, here on the specific first three, the other would like to know the details of the MSDN
M_nid.ucallbackmessage = Wm_systemtray;
Ucallbackmessage is an application-defined message indicating that when the mouse clicks on a tray mouse, the program's Wm_systemtray is sent out and we only
To be able to get the message in the WindowProc function, make an event response, as follows:
Switch (message)
{
Case Wm_systemtray://Custom message
Specific Response message
Break
}
Among them, you can see that Wm_systemtray is not a system message, but a custom message, to indicate that this particular event has occurred, as to how to define it
, it's using a macro.
#define Wm_systemtray wm_user+1
Explained here, why Wm_systemtray to macro definition into wm_user+1, as far as I know Wm_user is the last one in the system message, that is to say that the definition is
Does not conflict with the system's messages into the same value
M_nid.hicon = M_hicon;
Add, modify, or delete a picture handle to control the tray icon, M_hicon is a custom window icon handle
Hicon M_hicon;
M_hicon = AfxGetApp ()->loadicon (IDR_MAINFRAME);
Of course you can change M_nid.hicon = M_hicon directly to M_nid.hicon=afxgetapp ()->loadicon (IDR_MAINFRAME)
strcpy (M_nid.sztip, "Authentication system client");
Sztip is a pointer to a string that appears when the mouse is placed on the icon.
:: Shell_NotifyIcon (Nim_add, &m_nid);
The last one is a global function BOOL Shell_NotifyIcon (DWORD dwmessage,pnotifyicondata ipdata);
The parameter dwmessage represents the action to be performed, and the optional value is:
Nim_add to add an icon to the tray area
Nim_delete icon to delete the tray area
Nim_modify the icon that modifies the pallet area
Nim_setfocus indicates focus to tray icon
The second parameter is the pointer to the NOTIFYICONDATA structure we have described above.
If the operation succeeds the function returns True, otherwise false is returned.

///////////////////////////////////////////////////////////////////////////////////
OK, the implementation of the pallet is so simple, but if you want to know more specifically, you can check the MSDN specific explanations, you can also communicate with me
The following introduction of the application, the specific application situation is many.
Let's just say, how to implement the window hiding, double-click the tray to make the window appear again, and right-click the Tray appears exit window implementation
The above is called a custom message, in the WindowProc function, we can specifically implement
Switch (message)
{
Case Wm_systemtray://Custom message
if (LParam = = WM_LBUTTONDBLCLK)
{
ShowWindow (SW_SHOWNORMAL);
}
Break
}
The above code is implemented, double-click the tray to implement the display window code, very simple, we do not specifically analyzed.
To achieve a right-click Tray Display Popup, we must first create a menu in the resource and then add the following code
Case Wm_systemtray://Custom message
if (LParam = = Wm_rbuttondown)
{
Right-click to eject Tray menu
CMenu menu;
Menu. LoadMenu (IDR_MENU1);
CMenu *ppopup=menu. GetSubMenu (0);
CPoint pt;
GetCursorPos (&PT);

SetForegroundWindow ();
Ppopup->trackpopupmenu (Tpm_rightbutton,pt.x,pt.y,this);
PostMessage (wm_null,0,0);
}
Break
In the code, we just started to define a CMenu object, then loaded the menu ID we created earlier, and then got the current position of the mouse, and in this position
The menu is displayed.

Finally exit to delete icon: Shell_NotifyIcon (Nim_delete,&m_traydata);

MFC System tray icon Implementation (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.