About minimizing windows to pallets in MFC implementation principles and procedures _c language

Source: Internet
Author: User
Steps/Methods
(i) principle
1, the principle of minimizing: first to hide the window, and then in the lower right corner to draw the icon.
2, the principle of restoration: The window will be displayed, and then the picture in the tray deleted.

(ii) Procedural realization
1, custom message wm_showtask:
#define WM_SHOWTASK (Wm_user + 1)
2, in the MFC
:: OnSysCommand (UINT NID, LPARAM LPARAM) function body to add a command response
if (nid==sc_minimize)
Totray (); Functions that minimize to the pallet
3, in the message map to add
On_message (Wm_showtask,onshowtask); Where Wm_showtask is the name of the message,
Onshowtask is a message response function that you define, followed by a description.

(iii) content of specific functions
1, minimize to the tray function
Copy Code code as follows:

void Cmydlg::totray ()
{
Notifyicondata nid;
Nid.cbsize= (DWORD) sizeof (NOTIFYICONDATA);
nid.hwnd=this->m_hwnd;
Nid.uid=idr_mainframe;
nid.uflags=nif_icon| nif_message| Nif_tip;
nid.ucallbackmessage=wm_showtask;//a custom message name
Nid.hicon=loadicon (AfxGetInstanceHandle (), Makeintresource (Idr_mainframe));
strcpy (Nid.sztip, "program name"); Information Tip Bar
Shell_NotifyIcon (Nim_add,&nid); Add an icon to the pallet area
ShowWindow (Sw_hide); Hide main Window
}

2. Restore interface function
To define a message response function in a header file
Copy Code code as follows:

afx_msg lresult Onshowtask (WPARAM wparam,lparam LPARAM);
WPARAM receives the ID of the icon, and lparam receives the mouse behavior
Lresult Cmydlg::onshowtask (WPARAM wparam,lparam LPARAM)
{
if (wparam!=idr_mainframe)
return 1;
Switch (LParam)
{
Case wm_rbuttonup://right button up when pop-up shortcut menu, there is only one "off"
{
Lppoint lpoint=new Tagpoint;
:: GetCursorPos (Lpoint);//Get mouse position
CMenu menu;
Menu. CreatePopupMenu ()//Declaration of a pop-up menu

Menu. AppendMenu (Mf_string,wm_destroy, "close"); Add menu item "Off", click Send Message Wm_destroy to main window (hidden), end program.
Menu. TrackPopupMenu (Tpm_leftalign,lpoint->x,lpoint->y,this); Determine the location of the pop-up menu
Hmenu Hmenu=menu. Detach ();
Menu. DestroyMenu (); Resource Recycling
Delete Lpoint;
} break;
Case WM_LBUTTONDBLCLK://Double-click the left of the processing
{
This->showwindow (sw_show)//Simple display of main window finish
Deletetray ();
} break;
Default:break;
}
return 0;
}

Remove tray icon function
Copy Code code as follows:

void CMyDlg::D eletetray ()
{
Notifyicondata nid;
Nid.cbsize= (DWORD) sizeof (NOTIFYICONDATA);
nid.hwnd=this->m_hwnd;
Nid.uid=idr_mainframe;
nid.uflags=nif_icon| nif_message| Nif_tip;
Nid.ucallbackmessage=wm_showtask; A custom message name
Nid.hicon=loadicon (AfxGetInstanceHandle (), Makeintresource (Idr_mainframe));
strcpy (Nid.sztip, "program name"); Message tip Bar for scheduled task reminders
Shell_NotifyIcon (Nim_delete,&nid); Remove icon in pallet area
}
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.