0
点赞
收藏
分享

微信扫一扫

BCG 使用之NOTIFYICONDATA托盘


(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
#endifCTryMenuDemoDlg::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 132rc文件:
/
//
// 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

以上就是实现托盘的代码

举报

相关推荐

0 条评论