Preparations:
Before we start, we need to figure out Dongdong. Of course, you can also know it, but you don't know why it will work.
1. MFC custom message process (see the following article)
2. BOOL shell_policyicon (
DWORD dwMessage,
PNOTIFYICONDATA pnid
);
API functions (see the following document)
3. NOTIFYICONDATA struct (see the following document)
Concepts: (you can skip this step)
1. MFC custom message process
Step 1: Define a message.
# Define WM_JarvisChu (WM_USER + 1)
Add
Step 2: Describe the message processing function in the AFX_MSG block of the Class header file:
Afx_msg LRESULT OnJarvisChu (WPARAM wParam, LPARAM lParam );
Note: Here OnJarvisChu can be any name you get, but remember, this is our processing function for custom messages.
Step 3: Use the ON_MESSAGE macro command in the message block of the user class to map the message to the message processing function.
ON_MESSAGE (WM_JarvisChu, OnJarvisChu)
Note: The message MFC OnJarvisChu is the processing function of the custom message WM_JarvisChu.
Step 4: Implement Message processing functions.
Implement the LRESULT OnJarvisChu (WPARAM wParam, LPARAM lParam) function in the. cpp file );
2. BOOL shell_policyicon (
DWORD dwMessage,
PNOTIFYICONDATA pnid
);
This API function is used to store pnid information.
See MSDN
3. NOTIFYICONDATA struct
Let's take a look at MSDN. It's more specific than my major! Haha
Example:
Step 1: Create an MFC dialog box project ToTray
Add a Button in the Main Dialog Box and change the ID to IDC_BTN_TO_TRAY.
Function void CToTrayDlg: OnBtnToTary (){}
This button is used, but when we click it, the program will be minimized to the system tray area.
Step 2: Implement OnBtnToTary ()
Void CToTaryDlg: OnBtnToTray () <br/>{< br/> policyicondata nid; <br/> nid. cbSize = (DWORD) sizeof (policyicondata); <br/> nid. hWnd = this-> m_hWnd; <br/> nid. uID = IDR_MAINFRAME; <br/> nid. uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; <br/> nid. uCallbackMessage = WM_SHOWTASK; // custom message name <br/> nid. hIcon = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDR_MAINFRAME); <br/> strcpy (nid. szTip, "Jarvis Chu"); // message tip <br/> shell_policyicon (NIM_ADD, & nid ); // Add the icon in the tray area <br/> ShowWindow (SW_HIDE); // hide the main window <br/>}
In this program, we specify a custom message WM_SHOWTASK to the policyicondata struct. The following describes how to implement this message.
Step 3: Add a custom message WM_SHOWTASK
Do you still remember the content mentioned above? We can apply what we have learned now! Place the following code in a proper position.
# Define WM_SHOWTASK (WM_USER + 1) // custom message
Afx_msg LRESULT onShowTask (WPARAM wParam, LPARAM lParam); // message response function declaration
ON_MESSAGE (WM_SHOWTASK, onShowTask) // message ing
Step 4: Implement LRESULT onShowTask (WPARAM wParam, LPARAM lParam) for the corresponding message function );LRESULT CToTaryDlg: onShowTask (WPARAM wParam, LPARAM lParam) <br/> // wParam receives the icon ID, while lParam receives the mouse behavior <br/>{< br/> if (wParam! = IDR_MAINFRAME) <br/> return 1; <br/> switch (lParam) <br/>{< br/> case WM_RBUTTONUP: // right-click the shortcut menu to bring up <br/>{</p> <p> LPPOINT lpoint = new tagPOINT; <br/>: GetCursorPos (lpoint ); // obtain the mouse position <br/> CMenu menu; <br/> menu. createPopupMenu (); // declare a pop-up menu <br/> // Add the menu item "close ", click to send the message WM_DESTROY to the main window (<br/> // hide) and end the program. <Br/> menu. appendMenu (MF_STRING, WM_DESTROY, "close"); <br/> menu. appendMenu (MF_STRING, WM_SHOWWINDOW, "open"); <br/> // determine the position of the pop-up menu <br/> menu. trackPopupMenu (TPM_LEFTALIGN, lpoint-> x, lpoint-> y, this); <br/> // reclaim resources <br/> HMENU hmenu = menu. detach (); <br/> menu. destroyMenu (); <br/> delete lpoint; <br/>}< br/> break; <br/> case WM_LBUTTONDBLCLK: // double-click the left button for processing <br/>{< br/> this-> ShowWindow (SW_SHOW ); // simple display of the main window to complete <br/>}< br/> break; <br/>}< br/> return 0; <br/>}
Step 5: run the program
If the code is correct, you can see the effect.
It should be noted that some codes have been modified by referring to the online search results. Support originality!