(Currently, it can only be used in a dialog box-based program. If the SDI test fails and is to be resolved, you can respond to the onsize message in SDI)
1. Principle of minimization: First hide the window, and then draw the icon in the lower right corner.
2. Restoration principle: display the window and delete the images in the tray.
(2) Program Implementation 1. Custom message wm_showtask: # define wm_showtask (wm_user + 1)
2. Add a command response in the onsyscommand (uint NID, lparam) function body of MFC (in the DLG class ).
If (nid = SC _minimize)
Totray (); // the function that minimizes data to the tray.
3. Add on_message (wm_showtask, onshowtask) to message ing. Where wm_showtask is the message name, And onshowtask is the message response function defined by myself.
(3) specific function content
1. minimize to the tray Function
Void cmydlg: totray (){
Notifyicondata NID;
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip;
Nid. ucallbackmessage = wm_showtask; // custom message name
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Strcpy (NID. sztip, "program name"); // message tip
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
Showwindow (sw_hide); // hide the Main Window
}
2. Restore interface functions
Define the message response function afx_msg lresult onshowtask (wparam, lparam) in the header file; // wparam receives the icon ID, while lparam receives the mouse action.
Lresult ctimedlg: onshowtask (wparam, lparam) <br/>{< br/> If (wparam! = Idr_mainframe) <br/> return 1; <br/> switch (lparam) <br/> {<br/> case wm_rbuttonup: // The shortcut menu is displayed when you right-click it, there is only one "close" <br/>{< br/> 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_destroy, "set... "); <br/> // determine the position of the pop-up menu <br/> menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); <br/> // reclaim resources <br/> hmenu = menu. detach (); <br/> menu. destroymenu (); <br/> Delete lpoint; <br/>}< br/> break; <br/> case wm_lbuttondblclk: // double-click the left button for processing <br/>{</P> <p> showwindow (sw_shownormal ); // It is incorrect to simply display sw_show in the main window. </P> <p> // This-> setforegroundwindow (); // display it on top <br/> deletetray (); <br/>}< br/> break; <br/> default: <br/> break; <br/>}< br/> return 0; <br/>}< br/>
3. Delete the tray icon Function
Void ctimedlg: deletetray () <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, "program name"); // The message prompt bar is "scheduled task reminder" <br/> shell_policyicon (nim_delete, & nid ); // Delete the icon in the tray area <br/>}
In a single document program:
Void cmainframe: onsize (uint ntype, int CX, int CY)
{
Cframewnd: onsize (ntype, CX, CY );
// Todo: add your message handler code here
If (ntype = size_minimized)
{
Totray ();
}
// Else
//{////
// Shell_policyicon (nim_delete, & nid );
//}
}
Others are similar
This is a classic problem, because you may not want users to always face a lot of dialogs or zoom in to the toolbar to occupy an interface. I have seen many solutions to similar problems, however, what the fuck is anti-bot is a fucking Dialog Box program. In the face of a Single-document program, I once got such a plausible answer: it should be similar to the dialog box !.......... When I hear this sentence, I guess 90% of the brothers will be very depressed. How much is the difference? 108,000 miles !! Most of the comrades do not know the VC message processing process. In a word, the brothers are new to each other.
I don't want to say anything about the source code. You can use the Code if you can't understand it, note :!!!!!!!!!!! Leave a message.
..
.
.
Endif
Protected: // control bar embedded members
Cstatusbar m_wndstatusbar;
Ctoolbar m_wndtoolbar;
// Generated message map Functions
Protected:
// {Afx_msg (cmainframe)
Afx_msg int oncreate (maid );
Afx_msg void onopenmainfrm ();
//} Afx_msg
Policyicondata m_tnid;
Declare_message_map ()
};
Int cmainframe: oncreate (maid)
{
If (cframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible | cbrs_top
| Cbrs_gripper | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) |
! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create toolbar/N ");
Return-1; // fail to create
}
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.setindicators (indicators,
Sizeof (indicators)/sizeof (uint )))
{
Trace0 ("failed to create status bar/N ");
Return-1; // fail to create
}
M_tnid.cbsize = sizeof (policyicondata );
M_tnid.hwnd = This-> m_hwnd;
M_tnid.uflags = nif_message | nif_icon | nif_tip;
M_tnid.ucallbackmessage = wm_user_policyicon;
Cstring sztooltip;
Sztooltip = _ T ("System Tray ");
_ Tcscpy (m_tnid.sztip, sztooltip );
M_tnid.uid = idr_mainframe;
M_tnid.hicon = afxgetapp ()-> loadicon (idr_mainframe );
: Shell_policyicon (nim_add, & m_tnid );
// Todo: delete these three lines if you don't want the toolbar
// Be dockable
M_wndtoolbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndtoolbar );
Return 0;
}
Lresult cmainframe: windowproc (uint message, wparam, lparam)
{
// Todo: add your specialized code here and/or call the base class
Switch (Message) // determine the Message Type
{
Case wm_user_policyicon:
// For user-defined messages
If (lparam = wm_lbuttondblclk)
{
// The main window appears when you double-click the mouse
If (afxgetapp ()-> m_pmainwnd-> iswindowvisible () // you can determine the current window status.
{
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_hide); // hide the window
}
Else
{
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_show); // display window
}
}
Else if (lparam = wm_rbuttondown)
{// Right-click the menu
Cmenu menu;
Menu. loadmenu (idr_menu); // load the previously defined menu
Cmenu * pmenu = menu. getsubmenu (0 );
Cpoint Pos;
Getcursorpos (& Pos );
Pmenu-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, POS. X, POS. Y, afxgetmainwnd ());
}
Break;
Case wm_syscommand:
// If it is a system message
If (wparam = SC _minimize)
{
// The main window is hidden when the minimum message is received
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_hide );
//: Shell_policyicon (nim_add, & m_tnid );
Return 0;
}
If (wparam = SC _close)
{
: Shell_policyicon (nim_delete, & m_tnid); // Delete the system tray icon when it is disabled
}
Break;
}
Return cframewnd: windowproc (message, wparam, lparam );
}
Void cmainframe: onopenmainfrm ()
{
// Todo: add your command handler code here
If (! Afxgetapp ()-> m_pmainwnd-> iswindowvisible () // you can specify the current window status.
{
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_show); // display window
}
}
Note: The red part is the code that must be added. Some may say that we cannot find lresult cmainframe: windowproc (uint message, wparam, lparam) in the Class View, you don't have this function. Check the prototype and parameters of this function. You will find that this is not a Windows Message Processing Function !!! Add Windows message virtual function by yourself. As for how to add windows message virtual function, I don't need to say that everyone knows it.
///// The following are not verified,
Minimize single-document programs to pallets + custom messages
1. Custom message method
# Define wm_mymessage wm_user + 1
Afx_msg lresult onmymessage (wparam, lparam );
On_message (wm_mymessage, onmymessage) // This sentence must be in the middle of Begin message map and end
Lresult cxx: onmymessage (wparam wparma, lparam)
{
...
}
2. minimize to the tray Function
Void cmainframe: totray (void)
{
Notifyicondata NID;
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = This-> m_hwnd;
Nid. uid = idr_mainframe;
Nid. uflags = nif_icon | nif_message | nif_tip;
Nid. ucallbackmessage = wm_showtask; // custom message name
Nid. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
// Cstring STR = l "scheduled task reminder ";
// NID. sztip = {L "scheduled task reminder"}; // The message is scheduled task reminder"
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
Showwindow (sw_hide); // hide the Main Window
}
3. Pallet processing functions
Lresult cmainframe: onshowtask (wparam, lparam)
{
// Todo: add the message processing program code here
If (wparam! = Idr_mainframe)
Return 1;
Switch (lparam)
{
Case wm_rbuttonup: // The shortcut menu is displayed when you right-click it. There is only one "off"
{
Lppoint lpoint = new tagpoint;
: Getcursorpos (lpoint); // get the cursor position
Cmenu menu;
Menu. createpopupmenu (); // declare a pop-up menu
// Add the menu item "close" and click it to send the message wm_destroy to the main window (already
// Hide) to end the program.
Menu. appendmenu (mf_string, wm_destroy, l "close ");
// Determine the position of the pop-up menu
Menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this );
// Reclaim Resources
Hmenu = menu. Detach ();
Menu. destroymenu ();
Delete lpoint;
}
Break;
Case wm_lbuttondblclk: // double-click the left button.
{
This-> showwindow (sw_show); // a simple display of the main window is complete.
}
Break;
}
Return 0;
}
4. The above is the processing of the MFC tray. The following is a complete pure SDK program.
# Include <windows. h>
# Include <atlimage. h>
# Include "resource. H"
// # Include <afxwin. h>
# Define wm_showtask wm_user + 10
// # DEFINE _ afxdll;
Hwnd; // handle the main window of the program
Hinstance mhinstance; // instance handle
Notifyicondata NID; // The structure of the tray.
Lresult callback wndproc (hwnd, uint, wparam, lparam );
Void totray (void );
Lresult onshowtask (wparam, lparam );
Cimage image;
Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow)
{
Static tchar szappname [] = text ("hellowin ");
Hwnd;
MSG;
Wndclass;
Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = wndproc;
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance;
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hbrbackground = (hbrush) getstockobject (black_brush );
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname;
If (! Registerclass (& wndclass ))
{
MessageBox (null, text ("this program requires Windows NT! "),
Szappname, mb_iconerror );
Return 0;
}
Hwnd = createwindow (szappname, // window class name
Text ("full screen display"), // window caption
// Ws_overlappedwindow, // window style
Ws_popup,
/* Cw_usedefault, // initial X position
Cw_usedefault, // initial y position
Cw_usedefault,
Cw_usedefault, // initial X size
*/100,100,100,100,
Null, // parent window handle
Null, // Window menu handle
Hinstance, // program instance handle
Null); // creation Parameters
Hwnd = hwnd;
Mhinstance = hinstance;
Showwindow (hwnd, sw_show );
Updatewindow (hwnd );
Setwindowpos (hwnd, hwnd_topmost, 540,260,200,200, swp_showwindow | swp_noactivate | swp_nosize );
Totray ();
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return msg. wparam;
}
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case wm_create:
Return 0;
Case wm_paint:
{
HDC = getdc (hwnd );
Cimage image;
Image. Load (L "C: // bitmap // 002.bmp ");
Int x = getsystemmetrics (sm_cxscreen), Y = getsystemmetrics (sm_cyscreen );
// Image. bitblt (HDC, X-100, y-100, X, Y );
Setstretchbltmode (HDC, coloroncolor );
Rect;
Getclientrect (hwnd, & rect );
// Image. stretchblt (HDC, x-500, y-500, x, y );
Image. stretchblt (HDC, rect );
Image. Destroy ();
Releasedc (hwnd, HDC );
// Delete rect;
Return 0;
}
Case wm_destroy:
Shell_policyicon (nim_delete, & nid );
Postquitmessage (0 );
Return 0;
Case wm_showtask:
Onshowtask (wparam, lparam );
Return 0;
Case wm_command:
Switch (loword (wparam ))
{
Case idr_menu1:
Shell_policyicon (nim_delete, & nid );
Postquitmessage (0 );
Return 0;
Case idr_menu2:
Setwindowpos (hwnd, hwnd_topmost, 540,260,200,200, swp_showwindow | swp_noactivate | swp_nomove | swp_nosize );
Return 0;
Case idr_menu3:
Setwindowpos (hwnd, hwnd_topmost, 540,260,200,200, swp_hidewindow );
Return 0;
}
}
Return defwindowproc (hwnd, message, wparam, lparam );
}
Void totray (void)
{
Nid. cbsize = (DWORD) sizeof (policyicondata );
Nid. hwnd = hwnd;
Nid. uid = idi_icon1;
Nid. uflags = nif_icon | nif_message | nif_tip;
Nid. ucallbackmessage = wm_showtask; // custom message name
Nid. hicon = loadicon (mhinstance, makeintresource (idi_icon1 ));
// The message is scheduled task reminder"
Wchar SZ [] = l "scheduled task reminder ";
Memcpy (NID. sztip, SZ, sizeof (sz ));
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
// Showwindow (hwnd, sw_hide); // hide the Main Window
}
Lresult onshowtask (wparam, lparam)
{
If (wparam! = Idi_icon1)
Return 1;
Switch (lparam)
{
Case wm_rbuttonup: // The shortcut menu is displayed when you right-click it. There is only one "off"
{
Lppoint lpoint = new tagpoint;
: Getcursorpos (lpoint); // get the cursor position
Hmenu = createpopupmenu ();
Appendmenu (hmenu, mf_string, idr_menu1, l "off ");
Appendmenu (hmenu, mf_string, idr_menu2, l "");
Appendmenu (hmenu, mf_string, idr_menu3, l "hidden ");
Trackpopupmenu (hmenu, tpm_leftalign, lpoint-> X, lpoint-> Y, 0, hwnd, null );
Destroymenu (hmenu );
Delete lpoint;
}
Break;
Case wm_lbuttondblclk: // double-click the left button.
{
Showwindow (hwnd, sw_show); // a simple display of the main window is complete.
}
Break;
}
Return 0;
}