当前位置:网站首页>BCG 使用之NOTIFYICONDATA托盘
BCG 使用之NOTIFYICONDATA托盘
2022-07-20 16:57:00 【春蕾夏荷_728297725】
(1)头文件
// TryMenuDemoDlg.h : header file
//
#pragma once
// CTryMenuDemoDlg dialog
class CTryMenuDemoDlg : public CBCGPDialog
{
// Construction
public:
CTryMenuDemoDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_TRYMENUDEMO_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
void OnTrayContextMenu();
afx_msg LRESULT OnTrayNotify(WPARAM wp, LPARAM lp);
virtual void SetActiveMenu(CBCGPPopupMenu* pMenu);
afx_msg void OnAppExit();
afx_msg void OnAppOpen();
afx_msg void OnClose();
NOTIFYICONDATA m_nid; // struct for Shell_NotifyIcon args
};
添加的内容是:
void OnTrayContextMenu();
afx_msg LRESULT OnTrayNotify(WPARAM wp, LPARAM lp);
virtual void SetActiveMenu(CBCGPPopupMenu* pMenu);
afx_msg void OnAppExit();
afx_msg void OnAppOpen();
afx_msg void OnClose();
NOTIFYICONDATA m_nid; // struct for Shell_NotifyIcon args
(2)实现文件中
// TryMenuDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TryMenuDemo.h"
#include "TryMenuDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CTryMenuDemoDlg::CTryMenuDemoDlg(CWnd* pParent /*=NULL*/)
: CBCGPDialog(CTryMenuDemoDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//...需要添加 初始化托盘
memset(&m_nid, 0, sizeof(m_nid));
m_nid.cbSize = sizeof(m_nid);
m_nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
EnableVisualManagerStyle(TRUE, TRUE);
}
void CTryMenuDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CBCGPDialog::DoDataExchange(pDX);
}
#define UM_TRAYNOTIFY (WM_USER + 1)
BEGIN_MESSAGE_MAP(CTryMenuDemoDlg, CBCGPDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CTryMenuDemoDlg::OnBnClickedOk)
ON_MESSAGE(UM_TRAYNOTIFY, OnTrayNotify)
ON_COMMAND(ID_APP_EXIT, OnAppExit) //托盘中 退出
ON_COMMAND(ID_APP_OPEN, OnAppOpen)//托盘中 打开
ON_WM_CLOSE() //点击对话框关闭 按钮 隐藏
END_MESSAGE_MAP()
// CTryMenuDemoDlg message handlers
void CTryMenuDemoDlg::OnAppExit()
{
CBCGPDialog::OnCancel();
}
void CTryMenuDemoDlg::OnAppOpen()
{
ShowWindow(SW_SHOWNORMAL);
}
void CTryMenuDemoDlg::OnClose()
{
ShowWindow(SW_HIDE);
}
LRESULT CTryMenuDemoDlg::OnTrayNotify(WPARAM /*wp*/, LPARAM lp)
{
UINT uiMsg = (UINT)lp;
switch (uiMsg)
{
case WM_RBUTTONUP:
OnTrayContextMenu();
return 1;
case WM_LBUTTONDBLCLK:
ShowWindow(SW_SHOWNORMAL);
return 1;
}
return 0;
}
void CTryMenuDemoDlg::OnTrayContextMenu()
{
CPoint point;
::GetCursorPos(&point);
CMenu menu;
menu.LoadMenu(IDR_MENU1);
if (menu.GetSafeHmenu() == NULL)
{
return;
}
CBCGPPopupMenu::SetForceShadow(TRUE);
HMENU hMenu = menu.GetSubMenu(0)->Detach();
CBCGPPopupMenu* pMenu = GetWorkspace()->GetContextMenuManager()->ShowPopupMenu(
hMenu, point.x, point.y, this, TRUE);
pMenu->SetWindowPos(&wndTopMost, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
pMenu->SetForegroundWindow();
}
void CTryMenuDemoDlg::SetActiveMenu(CBCGPPopupMenu* pMenu)
{
CBCGPDialog::SetActiveMenu(pMenu);
if (pMenu != NULL)
{
pMenu->SetWindowPos(&wndTopMost, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
pMenu->SetForegroundWindow();
}
}
BOOL CTryMenuDemoDlg::OnInitDialog()
{
CBCGPDialog::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);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
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
{
m_nid.hWnd = GetSafeHwnd();
m_nid.uCallbackMessage = UM_TRAYNOTIFY;
// Set tray icon and tooltip:
m_nid.hIcon = m_hIcon;
CString strToolTip = _T("BCGPTrayDemo");
_tcsncpy(m_nid.szTip, strToolTip, strToolTip.GetLength());
Shell_NotifyIcon(NIM_ADD, &m_nid);
//这一句可以不要,如果不要,那么托盘中没有图片
CBCGPToolBar::AddToolBarForImageCollection(IDR_MENUIMAGES, IDB_MENU_HC);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTryMenuDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
CBCGPDialog::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 CTryMenuDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CBCGPDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTryMenuDemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CTryMenuDemoDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
}
资源中:
resource.h
#define IDR_MENU1 129
#define ID_APP_OPEN 130
#define IDR_MENUIMAGES 131
#define IDB_MENU_HC 132
rc文件:
/
//
// Toolbar
//
IDR_MENUIMAGES TOOLBAR DISCARDABLE 16, 16
BEGIN
BUTTON ID_APP_OPEN
BUTTON ID_APP_EXIT
END
/
//
// Bitmap
//
IDR_MENUIMAGES BITMAP DISCARDABLE "res\\menuimag.bmp"
/
//
// PNG
//
IDB_MENU_HC PNG DISCARDABLE "res\\menuimag.png"
/
//
// Menu
//
IDR_MENU1 MENU DISCARDABLE
BEGIN
POPUP "Tray"
BEGIN
MENUITEM "&Open....", ID_APP_OPEN
MENUITEM SEPARATOR
MENUITEM "E&xit", ID_APP_EXIT
END
END
以上就是实现托盘的代码
边栏推荐
- Could not resolve all dependencies for configuration的解决方案
- 1. Mx6u system migration-4-uboot migration
- Leetcode 展望2021
- Mongodb replication (replica set)
- Intelligent science innovation lecture hall | Institute of automation he Huiguang: Research on deep learning brain mechanism based on visual information encoding and decoding
- HTB-Lame
- 支持同花顺的证券公司怎么选择?手机上开户安不安全
- 2021-09-16
- 《程序设计基础》 第十章 函数与程序结构 6-9 十进制转换二进制 (15 分)
- I.MX6U-系统移植-4-uboot移植
猜你喜欢
随机推荐
山东大学、北邮、哈工大| Multimodal Dialog Systems with Dual Knowledge-enhanced Generative Pretrained Language Model(具有双重知识增强生成预训练语言模型的多模态对话系统)
Gbase competition | icdis database parameter tuning competition starts registration
力扣每日一题-第40天-643. 字数组最大平均数Ⅰ
chrome插件开发入门实战——CSDN免登陆拷贝、免关注查看
lotus-bench 1.17.0-rc3 Benchmarks
2021-09-17
分布式高并发高可用FastDFS文件服务器集群部署
2021-9-21
Why is kubernetes a new application server
Intelligent science innovation lecture hall | Institute of automation he Huiguang: Research on deep learning brain mechanism based on visual information encoding and decoding
Help the AI industry landing, yunzhisheng shows the full stack of AI hard core technology
Mongodb replication (replica set)
HTB-Lame
线性回归模型笔记(2)
OREPA:阿里提出训练也很快的重参数策略,内存减半,速度加倍 | CVPR 2022
关于PCDet的问题:Can not find file kitti_dbinfos_train.pkl
《程序设计基础》 第十章 函数与程序结构 6-11 递归计算P函数 (15 分)
Low code that democratizes software development
"Fundamentals of program design" Chapter 10 function and program structure 6-6 recursive implementation sequential output integer (15 points)
Lenovo exposed new UEFI vulnerabilities, affecting 70 models and millions of laptops