A little summary of the pallet

Source: Internet
Author: User
Tags define bool exit

Many software such as: KV3000 and so on after the shutdown will have a small tray icon in the tray area, which means that the program does not really stop and run in the background. When we click the mouse, we can make it appear on the desktop a maximized window; and if we click х in the upper-right corner of the program, or if we click the exit item in the File menu, the program still runs on the tray; we just right-click the tray icon and choose to exit from the menu that appears to be able

Now we do this by using a class Csystemtray and modifying the System menu.

Let's change the system menu first.

1, when we click the right mouse button on the title bar of the program, only appear as the figure of several items.

And through MFC generated will also appear about an item, we have to remove it.

We call the function GetSystemMenu () to fetch the system menu, and then call DeleteMenu () to remove the System menu item, where we remove the divider and "about" code below "close":

CMenu *pSystemMenu=GetSystemMenu(FALSE);
pSystemMenu->DeleteMenu(8,MF_BYPOSITION);
pSystemMenu->DeleteMenu(7,MF_BYPOSITION);

The numbers 7 and 8 represent the 7th and 8th items of the System menu, noting that the divider is also considered a VC. (This is the same as the previous method of modifying the System menu). When we remove the "about", we should also remove the corresponding code in the system command. We find the function OnSysCommand (UINT NID, LPARAM LPARAM) to remove the code that intercepts the About dialog box:

if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
     CAboutDlg dlgAbout;
     dlgAbout.DoModal();
}

It is of no use to us, put here can only bring trouble to our debugging. Of course, if you don't remove it, there's nothing wrong with the program. So why do we get rid of these two because we're going to do the same thing in the pallet? 1, we change the function of the program's closing sign "X" to hide it instead of closing it completely. Let's intercept the shutdown command and change it to hidden.

if ((nID & 0xFFF0)==SC_CLOSE){
     //OnClose();本来这个是关闭的这里也改为隐藏。
     AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_BLEND);
     KillTimer(0);
     ShowWindow(SW_HIDE);//系统菜单的关闭也改为隐藏。
}

The two sentences in front of ShowWindow (Sw_hide) are designed to achieve a special effect, which is slowly and gently hidden, but only those two sentences still fail should be preceded by the following two lines of code in the stdafx.h:

#undef WINVER
#define WINVER 0X500

2, we create a pallet menu, ID number for Idr_tuopan when we right-click on the tray, there will be a menu, in the menu we have: About, show (hide), change songs, quit four. Let's first customize a message in the header file VioletPlayDlg.h:

#define WM_USER_TRAY_NOTIFICATION (WM_USER+0x101)

Notice the format of it. Define a pallet variable in the file Csystemtray M_trayicon, with these we can first make a prototype of the tray. Create a pallet in the main file VioletPlayDlg.cpp. We use the creat () function to define the following:

m_trayIcon.Create(this, WM_USER_TRAY_NOTIFICATION, "VioletPlay", m_hIcon, IDR_TUOPAN);

One of the second parameters for our custom function is that the tray icon appears, the third parameter is the descriptive text that appears when we put the mouse over the icon, and the fifth parameter is the menu that appears when we click the right button, so this menu cannot appear here because we are not able to respond to the right button through this function. When our mouse leaves the icon, the description text violetplay will automatically disappear, we use a conditional statement to achieve. First we define a bool variable, bool start_minimized, to determine its value:

if (start_minimized)
  PostMessage(WM_CLOSE);

When we move the mouse, we send a close command, which closes the window. When we add a code to the file VioletPlay.cpp, it can be displayed in the tray and displayed on the desktop when the program is running:

dlg.start_minimized=GetProfileInt(_T(""),_T("StartMinimized"),FALSE);

We're here to add this code before the dialog box that shows the main program, to play an "intercept" function. This allows us to achieve the original function, that is, an icon appears in the tray, but this is not enough, we are in step to implement other functions.

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.