[wx] 系统托盘(Tray) ---- wxTaskBarIcon
lib: wxmsw28u_adv.lib
---------------------------- MyTray.h -----------------------------
#include <wx/wx.h>
#include <wx/taskbar.h>
class MyTray : public wxTaskBarIcon
{
public:
void OnAbout(wxCommandEvent& event);
protected:
virtual wxMenu *CreatePopupMenu();
private:
DECLARE_EVENT_TABLE();
};
-----------------------------------------------------------------------
#include "MyTray.h"
BEGIN_EVENT_TABLE(MyTray, wxTaskBarIcon)
EVT_MENU(wxID_ABOUT, MyTray::OnAbout)
END_EVENT_TABLE()
wxMenu *MyTray::CreatePopupMenu()
{
// 每次都要 new 一个新的 wxMenu
wxMenu *popupMenu = new wxMenu;
popupMenu->Append(wxID_ABOUT, _("About"));
popupMenu->AppendSeparator();
popupMenu->Append(wxID_EXIT, _("Exit"));
return popupMenu;
}
void MyTray::OnAbout(wxCommandEvent& event)
{
wxMessageBox(_T("hello!"), wxT("About Tray!"), wxOK | wxICON_INFORMATION);
}
------------------------ MyFrame.cpp --------------------------
MyFrame::MyFrame(const wxString &title) : wxFrame(NULL, wxID_ANY, title)
{
....
m_Tray = new MyTray();
m_Tray->SetIcon(wxIcon(cut_xpm), _("Hello Tray!"));
}
MyFrame::~MyFrame()
{
if ( m_Tray )
delete m_Tray;
}
-----------------------------------------------------------------------
使用 wxTaskBarIcon 的退出流程
1. 在 wxTaskBarIcon 上点'退出',调用 m_pMainFrame->Close()
2. MainFrame 收到消息,最后调用自己的 wxFrame::Destroy()
3. 进入析构函数,delete m_pTray,通过 wxTaskBarIcon::~wxTaskBarIcon() 完成退出流程
因为在 win32 下,wxTaskBarIcon 后面隐含着一个 TopLevelWindow,而此 TopLevelWindow 不退出,app 也是不会退出的。
http://kasicass.blog.163.com/blog/static/395619201071093547410/