📄 modifysysmenudlgdlg.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Project:MODIFYSYSMENUDLG
// Author:NorthTibet
// Date:星期五, 五月 02, 2003
// Description:示范如何修改系统菜单以及如何利用系统托盘机制隐藏对话框。
//
/////////////////////////////////////////////////////////////////////////////
// ModifySysMenuDlgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ModifySysMenuDlg.h"
#include "ModifySysMenuDlgDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "about.h"
/////////////////////////////////////////////////////////////////////////////
// CModifySysMenuDlgDlg dialog
CModifySysMenuDlgDlg::CModifySysMenuDlgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModifySysMenuDlgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModifySysMenuDlgDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CModifySysMenuDlgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModifySysMenuDlgDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModifySysMenuDlgDlg, CDialog)
//{{AFX_MSG_MAP(CModifySysMenuDlgDlg)
ON_WM_SYSCOMMAND()
ON_BN_CLICKED(ID_APP_ABOUT,OnAbout)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_COMMAND(IDC_EXIT, OnExit)
ON_COMMAND(IDC_SHOW, OnShow)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_USER_TRAY_NOTIFICATION,OnTrayNotification)
ON_COMMAND(IDM_ABOUTBOX, OnAbout)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModifySysMenuDlgDlg message handlers
BOOL CModifySysMenuDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
ASSERT((IDM_EXIT & 0xFFF0) == IDM_EXIT);
ASSERT(IDM_EXIT < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu,strExitMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
strExitMenu.LoadString(IDS_EXIT);
if (!strAboutMenu.IsEmpty() && !strExitMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_STRING,IDM_EXIT, strExitMenu);
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//show tray icon
m_trayIcon.Create(this, WM_USER_TRAY_NOTIFICATION, "Blhaaa 示范例子",
m_hIcon, IDM_TRAYICONMENU);
if (start_minimized)
PostMessage(WM_CLOSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CModifySysMenuDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
// 截获定制的系统菜单消息
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
} else if ((nID & 0xFFF0)==SC_CLOSE){
OnClose();
} else if ((nID & 0xFFF0)==IDM_EXIT) {
::PostQuitMessage(0);
}
else {
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CModifySysMenuDlgDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CModifySysMenuDlgDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModifySysMenuDlgDlg::OnAbout()
{
CAboutDlg().DoModal();
}
void CModifySysMenuDlgDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
// 改写原来系统菜单的“关闭”功能。
ShowWindow(SW_HIDE);
}
void CModifySysMenuDlgDlg::OnExit()
{
// TODO: Add your command handler code here
::PostQuitMessage(0);// 程序退出的唯一方式
}
void CModifySysMenuDlgDlg::OnShow()
{
// TODO: Add your command handler code here
// 检查对话框窗口的可见状态
if (!IsWindowVisible()) {
ShowWindow(SW_SHOW);
}
else
ShowWindow(SW_HIDE);
}
LONG CModifySysMenuDlgDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{ // The tray icon sent us a message. Let's see what it is
switch ( lparam )
{
case WM_CONTEXTMENU:
TRACE( "WM_CONTEXTMENU\n" );
case WM_RBUTTONDOWN:
{// 用户在托盘图标上单击鼠标右键,弹出上下文菜单隐藏/显示对话框。
CMenu oMenu;
if (oMenu.LoadMenu(IDM_TRAYICONMENU))
{
CMenu* pPopup = oMenu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CPoint oPoint;
if (IsWindowVisible())// 根据对话框窗口的显示/隐藏状态修改菜单名称
oMenu.ModifyMenu(IDC_SHOW,MF_STRING,IDC_SHOW,"隐藏(&H)");
else
oMenu.ModifyMenu(IDC_SHOW,MF_STRING,IDC_SHOW,"显示(&S)");
// 确定鼠标位置以便在该位置附近显示菜单
GetCursorPos( &oPoint );
SetForegroundWindow();
pPopup->TrackPopupMenu(
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
oPoint.x, oPoint.y, this);
}
}
break;
// 单击/双击鼠标左键均显示出对话框
case WM_LBUTTONDBLCLK:
case WM_LBUTTONDOWN:
OnShow();
break;
}
return 0;
}
void CModifySysMenuDlgDlg::OnCancel()
{
// TODO: Add extra cleanup here
OnClose();
// CDialog::OnCancel();
}
void CModifySysMenuDlgDlg::OnOK()
{
// TODO: Add extra validation here
OnClose();
// CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -