Win32 Advanced Path: Program Tray Icon + right-click pop-up menu

Source: Internet
Author: User

This article introduces two very good and practical tips: The program tray icon and the right-click pop-up menu, the effect is as shown in.

The program Tray Icon uses the thunder icon. When you right-click the icon, three options are displayed.

I will describe it in a step that is as clear as possible. I assume that you have already written your own windows form. If you have just learned win32, you can read my previous blog, this section describes how to create your first windows window from scratch. You can also refer to the complete code posted at the end of this Article for reference. If you think other ways better or do not understand the story can comment or send to my mailbox believing_dan@hotmail.com, QQ382128698.

Step 1: declare the tray icon object and assign values to key members.

Step 2: Notify windows to add an icon in the lower right corner.

Step 3: process the left-click and right-click messages of the tray icon.

Code in step 1/2:

 

 

{If (! Strncmp (operation, TEXT ("MIN"), 3) {// declare the variable and initialize the associated Tray Icon resource, the icon must be a 16*16 or 32*32 pixel nID. hIcon = (HICON) LoadImage (NULL, TEXT ("notify. ico "), IMAGE_ICON, 0, 0, LR_LOADFROMFILE); // prompt for the tray icon, that is, the prompt strncpy_s (nID. szTip, TEXT ("Homemade screen lock software selling cute"), sizeof (TEXT ("Homemade screen lock software selling cute"); // The Window nID associated with the tray icon. hWnd = hWND; // if the application only has one tray icon, you can set nID as needed. uID = 1; // The type of the tray icon. For details, see MSDN nID. uFlags = NIF_GUID | NIF_ICON | NIF_MESSAGE | NIF_TIP; // the ID of the message associated with the tray icon. The message ID of the left-click and right-click messages of the tray is its nID. uCallbackMessage = WM_MINMAXNotify; // notify windows to add a tray icon. Check the parameters to see shell_policyicon (NIM_ADD, & nID );}}

The code in step 3 is how to process the messages associated with the pallets.

Assume that you already have your own windows form. For the complete code, refer to the Code posted at the end of this article.

 

 

Case WM_MINMAXNotify: switch (lparam) {case WM_LBUTTONDOWN: MessageBox (NULL, TEXT ("Recv every y icon message"), TEXT ("Every y"), MB_ICONHAND); break; case wm_rbtondown: POINT pt; GetCursorPos (& pt); HMENU hMenu; hMenu = CreatePopupMenu (); AppendMenu (hMenu, MF_STRING, IDM_FIR, ""); AppendMenu (hMenu, MF_SEPARATOR, 0, NULL); AppendMenu (hMenu, MF_STRING, IDM_SEC, TEXT ("strange"); AppendMenu (hMenu, MF_SEPARATOR, 0, NULL); AppendMenu (hMenu, MF_STRING, IDM_THI, TEXT ("replenishment"); AppendMenu (hMenu, MF_SEPARATOR, 0, NULL); TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt. x, pt. y, NULL, hwnd, NULL); break;} break;

Is it too easy! Haha, that's simple. Add a case in the form processing function of windows to process the messages associated with the tray. You can use the lparam parameter to obtain the actions of the mouse on the tray, here, I processed the left-click and right-click messages, and left-click to bring up a prompt box, right-click the tray ...... well, I have missed the question. Right-click to add a pop-up menu.

Step 1: Create a PopMenu object.

Step 2: Add a menu item for the newly added pop-up menu.

Step 3: display the newly added menu.

Step 4: process the message of the menu item.

I used four more steps to deal with such practical skills. Isn't that exciting? Although the previous question has been missed, let's talk about the code.

Create an HMENU handle for a pop-up menu. There are many menu types and you need to create an API

HMENU hMenu = CreatePopupMenu ();

Insert a menu item and give it a character name. Here, you must set the menu item type to MF_STRING and associate it with an ID.

AppendMenu (hMenu, MF_STRING, IDM_FIR, "secret ");

Insert a separator to make the menu look better. What? Yes, it looks better.

AppendMenu (hMenu, MF_SEPARATOR, 0, NULL );

Compare the previous one. The difference is that the separator is playing tricks.

Here is a tip for getting the current mouse position and displaying the newly created pop-up menu. Why not associate the pop-up menu with the current window, in this way, the menu is automatically destroyed after you click the menu.

POINT pt;

GetCursorPos (& pt );

TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt. x, pt. y, NULL, hwnd, NULL );

The Response Message of the pop-up menu belongs to the WM_COMMAND command series. You can use wparam to distinguish the clicked menu items.

Case WM_COMMAND:

Switch (wparam)

{

Case IDM_FIR:

MessageBox (NULL, TEXT ("Recv popup menu idm_fir message"), TEXT ("Popup"), MB_ICONINFORMATION );

Break;

Case IDM_SEC:

MessageBox (NULL, TEXT ("Recv popup menu idm_sec message"), TEXT ("Popup"), MB_ICONASTERISK );

Break;

Case IDM_THI:

MessageBox (NULL, TEXT ("Recv popup menu idm_thi message"), TEXT ("Popup"), MB_ICONEXCLAMATION );

Break;

Default:

Break;

}

Break;

Source code Link Description: The complete code is a well-developed lock screen software code. It may take some effort to find the functions described in this article, of course, there may also be some extra help for everyone. below is the screen lock software. The middle sister will flash and sell cute, and the sub-window will gradually drift, when the screen edge is reached, it will bounce back.

The lock screen software is continuously updated. Welcome to my blog.

Code and sample http://download.csdn.net/detail/hd770c/6920757

Related Article

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.