Make the dialog application with system tray function

xiaoxiao2021-03-06  40

Make the dialog application with system tray function

Neweroica2005-2-27 12:23

1. Copy systemtray.h and systemtray.cpp to the project work directory, add it to the project. Add #include "systemtray.h" in xxxdlg.h 2. Add protected variable for XXXDLG classes: Csystemtray M_Trayicon

3. Establishing a tray menu is that when the program is hidden on the taskbar, you use the right-click on the pallet icon to display the right-click menu. Newly built a Menu resource, named idR_thetray. Add the submenu you want to this menu. For example: About IDC_SHOW IDC_ABOUT show / hide ID_APP_EXIT exit OnAbout response function is added IDC_ABOUT with ClassWizard (): CAboutDlg aboutdlg; aboutdlg.DoModal (); adding the OnShow response function () is used IDC_SHOW ClassWizard: (! IsWindowVisible ()) if {ShowWindow (Sw_show);} else {showwindow (sw_hide);} 4. Add: #define wm_user_tray_notification (wm_user 0x101) in the XXDLG header file

Add: in OnInitDialog () in the CPP file:

m_trayicon.create (this, wm_user_tray_notification, "blhaaa", m_hicon, idr_thetray); // A sentence to the mouse when the mouse is placed in the tray //, you will see the task bar after compiling it. The system tray has appeared, but the right button does not respond. This is because we haven't added messages and messages. 5. Add the following functions in the XXXDLG class:

Disclaimer: AFX_MSG Long CsystrayDemodlg :: OntrayNotification (WParam WPARAM, LPARAM LPARAM);

Function definition:

Long csystrayDemodlg :: ontraynotification (wparam wparam, lparam lparam) {switch (lparam) {// the TRAY ICON SENT US A Message. // Let's See What IS

/ * Case WM_ContextMenu: Trace ("WM_ContextMenu / N"); * / Case WM_RBUTTONDOWN: {// User Right-click on the tray icon and pops up the context menu hide / display dialog. CMenu Omenu; IF (omenu.loadmenu) {cmenu * ppopup = Omenu.getsubmenu (0); assert (ppopup! = Null); cpoint opoint; if (iswindowVisible ()) // According to the dialog window / Hidden Status / / Modify Menu Name Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Hide (& H)"); Else Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Display (& S)"); // Determine the mouse location for The menu getCursorpos (& opoint); setForegroundWindow (); ppopup-> TRACKPOPMENU (TPM_LEFTALIGN | TPM_RightButton, Opoint.x, Opoint.Y, this);}} Break; // Click / Double click the left mouse left mouse button Dialog box case wm_lbuttondblclk: Case WM_LButtondown: onshow (); Break;}

Return 0;}

6. Add message mapping:

Long csystrayDemodlg :: ontraynotification (wparam wparam, lparam lparam) {switch (lparam) {// the TRAY ICON SENT US A Message. // Let's See What IS

/ * Case WM_ContextMenu: Trace ("WM_ContextMenu / N"); * / Case WM_RBUTTONDOWN: {// User Right-click on the tray icon and pops up the context menu hide / display dialog. CMenu Omenu; IF (omenu.loadmenu) {cmenu * ppopup = Omenu.getsubmenu (0); assert (ppopup! = Null); cpoint opoint; if (iswindowVisible ()) // According to the dialog window / Hidden Status / / Modify Menu Name Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Hide (& H)"); Else Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Display (& S)"); // Determine the mouse location for The menu getCursorpos (& opoint); setForegroundWindow (); ppopup-> TRACKPOPMENU (TPM_LEFTALIGN | TPM_RightButton, Opoint.x, Opoint.Y, this);}} Break; // Click / Double click the left mouse left mouse button Dialog box case wm_lbuttondblclk: Case WM_LButtondown: onshow (); Break;}

Return 0;}

6. Add message mapping:

Long csystrayDemodlg :: ontraynotification (wparam wparam, lparam lparam) {switch (lparam) {// the TRAY ICON SENT US A Message. // Let's See What IS

/ * Case WM_ContextMenu: Trace ("WM_ContextMenu / N"); * / Case WM_RBUTTONDOWN: {// User Right-click on the tray icon and pops up the context menu hide / display dialog. CMenu Omenu; IF (omenu.loadmenu) {cmenu * ppopup = Omenu.getsubmenu (0); assert (ppopup! = Null); cpoint opoint; if (iswindowVisible ()) // According to the dialog window / Hidden Status / / Modify Menu Name Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Hide (& H)"); Else Omenu.ModifyMenu (IDC_SHOW, MF_STRING, IDC_SHOW, "Display (& S)"); // Determine the mouse location for The menu getCursorpos (& opoint); setForegroundWindow (); ppopup-> TRACKPOPMENU (TPM_LEFTALIGN | TPM_RightButton, Opoint.x, Opoint.Y, this);}} Break; // Click / Double click the left mouse left mouse button Dialog box case wm_lbuttondblclk: Case WM_LButtondown: onshow (); Break;}

Return 0;}

6. Add message mapping:

BEGIN_MESSAGE_MAP ... // {{AFX_MSG_MAP (CSysTrayDemoDlg) ... ... //}} AFX_MSG_MAP ON_MESSAGE (WM_USER_TRAY_NOTIFICATION, OnTrayNotification) END_MESSAGE_MAP () then compile and run after us, right-click the tray icon menu will appear. 7. Modify the system menu so that we are not exiting the program when you click the "Close" button, but a hidden window. To do this, we can modify the OnsysCommand ((NID & 0xFFF0) == IDM_ABOUTBOX) {Caboutdlg Dlgabout; Dlgabout.Domodal ();} else}} else} == SC_CLOSE) {// onclose (); this is the closure here to hide. AnimateWindow (GetSafehWnd (), 1000, aw_hide | aw_blend; killtimer (0); // The two sentences implement gradient transition hidden window showWindow (sw_hide); // The shutdown of the system menu is also hidden. } Else {cdialog :: OnSysCommand (Nid, LPARAM); If you want to implement this function, in addition to not commenting out of these two lines, you need to add the following code in the beginning of the stdafx.h file: #undef Winver #define Winver 0x500, a dialog application has a system tray Function. IF ((NID & 0xFFF0) == IDM_ABOUTBOX) {Caboutdlg Dlgabout; dlgabout.domodal ();} else if ((NID & 0xFFF0) == sc_close) {// onclose (); Original this is closed here hide. AnimateWindow (GetSafehWnd (), 1000, aw_hide | aw_blend; killtimer (0); // The two sentences implement gradient transition hidden window showWindow (sw_hide); // The shutdown of the system menu is also hidden. } Else {cdialog :: OnSysCommand (Nid, LPARAM); If you want to implement this function, in addition to not commenting out of these two lines, you need to add the following code in the beginning of the stdafx.h file: #undef Winver #define Winver 0x500, a dialog application has a system tray Function.

At this time, the menu will appear after the compilation is running. 7. Modify the system menu so that we are not exiting the program when you click the "Close" button, but a hidden window. To do this, we can modify the OnsysCommand ((NID & 0xFFF0) == IDM_ABOUTBOX) {Caboutdlg Dlgabout; Dlgabout.Domodal ();} else}} else} == SC_CLOSE) {// onclose (); this is the closure here to hide. AnimateWindow (GetSafehWnd (), 1000, aw_hide | aw_blend; killtimer (0); // The two sentences implement gradient transition hidden window showWindow (sw_hide); // The shutdown of the system menu is also hidden. } Else {cdialog :: OnSysCommand (Nid, LPARAM); If you want to implement this function, in addition to not commenting out of these two lines, you need to add the following code in the beginning of the stdafx.h file: #undef Winver #define Winver 0x500, a dialog application has a system tray Function. Note, ANIMATEWINDOW (GetSafehWnd (), 1000, aw_hide | aw_blend; and killtimer (0); two sentences are to implement gradient transition hidden windows. If you want to implement this function, in addition to not commenting out of these two lines, you need to add the following code in the beginning of the stdafx.h file: #undef Winver #define Winver 0x500, a dialog application has a system tray Function.

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

New Post(0)