Membres inscrits :2359
Membres en ligne : 0
Invités en ligne : 3


|
Conversation (Résolue) : wxTaskBarIcon, comment efficer et cacher le Frame? |
ajm (Membre)
Inscrit le : 16-03-2011
Messages: 82
Snippets: 0
Tutoriels: 0
Hors ligne |
Bonjour,
Ça fait presque une semaine que j'ai essayé d'utiliser le wxTaskBarIcon et je n'arrive pas à cacher et afficher le Frame à partir du menu du wxTaskBarIcon, jusqu'à maintenant je ne trouve pas de solution. Voici les code dans le projet crée avec CodeBlocks:
Fichier TaskBarIconApp.h
Code wxWidgets: #ifndef TASKBARICONAPP_H #define TASKBARICONAPP_H #include <wx/app.h> class TaskBarIconApp : public wxApp { public: virtual bool OnInit(); }; #endif // TASKBARICONAPP_H
Fichier TaskBarIconApp.cpp
Code wxWidgets: #include "TaskBarIconApp.h" //(*AppHeaders #include "TaskBarIconMain.h" #include <wx/image.h> //*) IMPLEMENT_APP(TaskBarIconApp); bool TaskBarIconApp::OnInit() { bool wxsOK = true; wxInitAllImageHandlers(); if ( wxsOK ) { TaskBarIconFrame* Frame = new TaskBarIconFrame(0); Frame->Show(); SetTopWindow(Frame); } return wxsOK; }
Fichier TaskBarIconMain.h
Code wxWidgets: #ifndef TASKBARICONMAIN_H #define TASKBARICONMAIN_H //(*Headers(TaskBarIconFrame) #include <wx/menu.h> #include <wx/textctrl.h> #include <wx/button.h> #include <wx/frame.h> #include <wx/statusbr.h> //*) #include <wx/taskbar.h> class MyTaskBarIcon: public wxTaskBarIcon { public: #if defined(__WXCOCOA__) MyTaskBarIcon(wxTaskBarIconType iconType = DEFAULT_TYPE) : wxTaskBarIcon(iconType) #else MyTaskBarIcon() #endif {} void OnLeftButtonDClick(wxTaskBarIconEvent&); void OnMenuRestore(wxCommandEvent&); void OnMenuExit(wxCommandEvent&); void OnMenuSetNewIcon(wxCommandEvent&); void OnMenuSetOldIcon(wxCommandEvent&); void OnMenuCheckmark(wxCommandEvent&); void OnMenuUICheckmark(wxUpdateUIEvent&); void OnMenuSub(wxCommandEvent&); virtual wxMenu *CreatePopupMenu(); DECLARE_EVENT_TABLE() }; class TaskBarIconFrame: public wxFrame { public: TaskBarIconFrame(wxWindow* parent,wxWindowID id = -1); virtual ~TaskBarIconFrame(); private: //(*Handlers(TaskBarIconFrame) void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); void OnButton1Click(wxCommandEvent& event); void OnButton1Click1(wxCommandEvent& event); void OnButton2Click(wxCommandEvent& event); //*) //(*Identifiers(TaskBarIconFrame) static const long ID_BUTTON1; static const long ID_BUTTON2; static const long ID_TEXTCTRL1; static const long idMenuQuit; static const long idMenuAbout; static const long ID_STATUSBAR1; //*) //(*Declarations(TaskBarIconFrame) wxButton* Button1; wxButton* Button2; wxStatusBar* StatusBar1; wxTextCtrl* TextCtrl1; //*) protected: MyTaskBarIcon *m_taskBarIcon; #if defined(__WXCOCOA__) MyTaskBarIcon *m_dockIcon; #endif DECLARE_EVENT_TABLE() }; #endif // TASKBARICONMAIN_H
Fichier TaskBarIconMain.cpp
Code wxWidgets: #include "TaskBarIconMain.h" #include <wx/msgdlg.h> #include <wx/artprov.h> #include <wx/msgdlg.h> //(*InternalHeaders(TaskBarIconFrame) #include <wx/intl.h> #include <wx/string.h> //*) #include <wx/taskbar.h> //helper functions enum wxbuildinfoformat { short_f, long_f }; wxString wxbuildinfo(wxbuildinfoformat format) { wxString wxbuild(wxVERSION_STRING); if (format == long_f ) { #if defined(__WXMSW__) wxbuild << _T("-Windows"); #elif defined(__UNIX__) wxbuild << _T("-Linux"); #endif #if wxUSE_UNICODE wxbuild << _T("-Unicode build"); #else wxbuild << _T("-ANSI build"); #endif // wxUSE_UNICODE } return wxbuild; } //(*IdInit(TaskBarIconFrame) const long TaskBarIconFrame::ID_BUTTON1 = wxNewId(); const long TaskBarIconFrame::ID_BUTTON2 = wxNewId(); const long TaskBarIconFrame::ID_TEXTCTRL1 = wxNewId(); const long TaskBarIconFrame::idMenuQuit = wxNewId(); const long TaskBarIconFrame::idMenuAbout = wxNewId(); const long TaskBarIconFrame::ID_STATUSBAR1 = wxNewId(); //*) BEGIN_EVENT_TABLE(TaskBarIconFrame,wxFrame) //(*EventTable(TaskBarIconFrame) //*) END_EVENT_TABLE() TaskBarIconFrame::TaskBarIconFrame(wxWindow* parent,wxWindowID id) { //(*Initialize(TaskBarIconFrame) wxMenuItem* MenuItem2; wxMenuItem* MenuItem1; wxMenu* Menu1; wxMenuBar* MenuBar1; wxMenu* Menu2; Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id")); SetClientSize(wxSize(400,215)); Button1 = new wxButton(this, ID_BUTTON1, _("Ok"), wxPoint(32,88), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1")); Button2 = new wxButton(this, ID_BUTTON2, _("DLG"), wxPoint(152,88), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2")); TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("Text"), wxPoint(32,40), wxSize(264,21), 0, wxDefaultValidator, _T("ID_TEXTCTRL1")); MenuBar1 = new wxMenuBar(); Menu1 = new wxMenu(); MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL); Menu1->Append(MenuItem1); MenuBar1->Append(Menu1, _("&File")); Menu2 = new wxMenu(); MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL); Menu2->Append(MenuItem2); MenuBar1->Append(Menu2, _("Help")); SetMenuBar(MenuBar1); StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1")); int __wxStatusBarWidths_1[1] = { -1 }; int __wxStatusBarStyles_1[1] = { wxSB_NORMAL }; StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1); StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1); SetStatusBar(StatusBar1); Center(); Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TaskBarIconFrame::OnButton1Click1); Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TaskBarIconFrame::OnButton2Click); Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TaskBarIconFrame::OnQuit); Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TaskBarIconFrame::OnAbout); //*) m_taskBarIcon = new MyTaskBarIcon(); #if defined(__WXCOCOA__) m_dockIcon = new MyTaskBarIcon(wxTaskBarIcon::DOCK); #endif wxBitmap BM(wxArtProvider::GetBitmap(wxART_NORMAL_FILE ,wxART_OTHER,wxSize(16,16))); wxIcon IC; IC.CopyFromBitmap(BM); if (!m_taskBarIcon->SetIcon(IC, wxT("wxTaskBarIcon Sample")))//wxICON(sample) wxMessageBox(wxT("Could not set icon.")); } TaskBarIconFrame::~TaskBarIconFrame() { //(*Destroy(TaskBarIconFrame) //*) delete m_taskBarIcon; #if defined(__WXCOCOA__) delete m_dockIcon; #endif } void TaskBarIconFrame::OnQuit(wxCommandEvent& event) { Close(); } void TaskBarIconFrame::OnAbout(wxCommandEvent& event) { wxString msg = wxbuildinfo(long_f); wxMessageBox(msg, _("Welcome to...")); } enum { PU_RESTORE = 10001, PU_NEW_ICON, PU_OLD_ICON, PU_EXIT, PU_CHECKMARK, PU_SUB1, PU_SUB2, PU_SUBMAIN }; BEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon) EVT_MENU(PU_RESTORE, MyTaskBarIcon::OnMenuRestore) EVT_MENU(PU_EXIT, MyTaskBarIcon::OnMenuExit) EVT_MENU(PU_NEW_ICON,MyTaskBarIcon::OnMenuSetNewIcon) EVT_MENU(PU_OLD_ICON,MyTaskBarIcon::OnMenuSetOldIcon) EVT_MENU(PU_CHECKMARK,MyTaskBarIcon::OnMenuCheckmark) EVT_UPDATE_UI(PU_CHECKMARK,MyTaskBarIcon::OnMenuUICheckmark) EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick) EVT_MENU(PU_SUB1, MyTaskBarIcon::OnMenuSub) EVT_MENU(PU_SUB2, MyTaskBarIcon::OnMenuSub) END_EVENT_TABLE() void MyTaskBarIcon::OnMenuRestore(wxCommandEvent& ) { // Frame->Show(true); } void MyTaskBarIcon::OnMenuExit(wxCommandEvent& ) { // Frame->Close(true); } static bool check = true; void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent& ) { check =!check; } void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent &event) { event.Check( check ); } void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent&) { wxBitmap BM(wxArtProvider::GetBitmap(wxART_FILE_SAVE ,wxART_OTHER,wxSize(16,16))); wxIcon IC; IC.CopyFromBitmap(BM); SetIcon(IC, wxT("wxTaskBarIcon Sample")); } void MyTaskBarIcon::OnMenuSetOldIcon(wxCommandEvent&) { if (IsIconInstalled()) { RemoveIcon(); } else { wxMessageBox(wxT("wxTaskBarIcon Sample - icon already is the old version")); } } void MyTaskBarIcon::OnMenuSub(wxCommandEvent&) { wxMessageBox(wxT("You clicked on a submenu!")); } // Overridables wxMenu *MyTaskBarIcon::CreatePopupMenu() { // Try creating menus different ways // TODO: Probably try calling SetBitmap with some XPMs here wxMenu *menu = new wxMenu; menu->Append(PU_RESTORE, _T("&Restore TBTest")); menu->AppendSeparator(); menu->Append(PU_OLD_ICON, _T("&Restore Old Icon")); menu->Append(PU_NEW_ICON, _T("&Set New Icon")); menu->AppendSeparator(); menu->Append(PU_CHECKMARK, _T("Checkmark"),wxT(""), wxITEM_CHECK); menu->AppendSeparator(); wxMenu *submenu = new wxMenu; submenu->Append(PU_SUB1, _T("One submenu")); submenu->AppendSeparator(); submenu->Append(PU_SUB2, _T("Another submenu")); menu->Append(PU_SUBMAIN, _T("Submenu"), submenu); #ifndef __WXMAC_OSX__ /*Mac has built-in quit menu*/ menu->AppendSeparator(); menu->Append(PU_EXIT, _T("E&xit")); #endif return menu; } void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&) { // Frame->Show(true); } void TaskBarIconFrame::OnButton1Click(wxCommandEvent& event) { } void TaskBarIconFrame::OnButton1Click1(wxCommandEvent& event) { TextCtrl1->SetValue(_("Hi!")); } void TaskBarIconFrame::OnButton2Click(wxCommandEvent& event) { wxMessageBox(_("C'est une test de wxMessageBox"),_("Test dlg"),wxYES_NO); }
Est-ce que qlq peut me dire où est le problème.
|
|
Xaviou (Administrateur)
Lieu: Annecy (74)
Inscrit le : 27-08-2007
Messages: 1387
Snippets: 25
Tutoriels: 6
Site web
Hors ligne |
Salut.
En fait, le principe est simple : Il faut que ta classe dérivée de wxTaskBarIcon possède un pointeur vers la wxFrame à gérer. Pour ma part, je le lui passe en paramètre au constructeur et je le stocke dans une variable privée.
voici un petit exemple :
Code wxWidgets:#ifndef TASKBARICON_H_INCLUDED #define TASKBARICON_H_INCLUDED #include <wx/wx.h> #include <wx/taskbar.h> class TaskBarIcon : public wxTaskBarIcon { public: TaskBarIcon(wxFrame* frame); virtual ~TaskBarIcon(); virtual wxMenu *CreatePopupMenu(); protected: private: void OnLeftButtonDClick(wxTaskBarIconEvent&); void OnMenuRestore(wxCommandEvent &event); void OnMenuExit(wxCommandEvent &event); void OnMenuAbout(wxCommandEvent &event); wxFrame* m_frmMain; DECLARE_EVENT_TABLE(); }; #endif // TASKBARICON_H_INCLUDED
Code wxWidgets:#include "taskbaricon.h" #include <wx/aboutdlg.h> BEGIN_EVENT_TABLE(TaskBarIcon, wxTaskBarIcon) EVT_MENU(wxID_HOME, TaskBarIcon::OnMenuRestore) EVT_MENU(wxID_EXIT, TaskBarIcon::OnMenuExit) EVT_MENU(wxID_ABOUT, TaskBarIcon::OnMenuAbout) EVT_TASKBAR_LEFT_DCLICK (TaskBarIcon::OnLeftButtonDClick) END_EVENT_TABLE() TaskBarIcon::TaskBarIcon(wxFrame* frame) : wxTaskBarIcon() { m_frmMain=frame; } TaskBarIcon::~TaskBarIcon() { //dtor } wxMenu *TaskBarIcon::CreatePopupMenu() { wxMenu *menu = new wxMenu; wxMenuItem *mnuItm; if (m_frmMain->IsShown()) { mnuItm=new wxMenuItem(menu, wxID_HOME, _T("Cacher la fenêtre")); } else { mnuItm=new wxMenuItem(menu, wxID_HOME, _T("Afficher la fenêtre")); } menu->Append(mnuItm); menu->AppendSeparator(); mnuItm=new wxMenuItem(menu, wxID_ABOUT, _T("A propos de")); menu->Append(mnuItm); menu->AppendSeparator(); mnuItm=new wxMenuItem(menu, wxID_EXIT, _T("&Quitter")); menu->Append(mnuItm); return menu; } void TaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&) { // Si la fenêtre principale est cachée, on la restore if (!m_frmMain->IsShown()) { // Double-clic sur l'icône = restaurer l'application m_frmMain->Show(); m_frmMain->Iconize(false); } else { // Sinon, on la cache m_frmMain->Hide(); } } void TaskBarIcon::OnMenuRestore(wxCommandEvent &event) { if (!m_frmMain->IsShown()) { // Idem que la méthode ci-dessus : on réaffiche la fenêtre m_frmMain->Show(); m_frmMain->Iconize(false); } else { // Si la fenêtre n'est pas cachée, alors on la cache m_frmMain->Hide(); } } void TaskBarIcon::OnMenuExit(wxCommandEvent &event) { // On passe l'événement directement à la frame m_frmMain->GetEventHandler()->AddPendingEvent(event); } void TaskBarIcon::OnMenuAbout(wxCommandEvent &event) { // Code identique à celui de MainFrame::OnAbout wxAboutDialogInfo info; info.SetName(_T("Mon Application")); info.SetVersion(_T("1.0.0"); info.SetDescription(_T("Description de l'application")); info.SetCopyright(_T("Copyright...")); info.SetWebSite(_T("http://www.wxdev.fr")); wxAboutBox(info); }
Code wxWidgets:#ifndef MAINFRAME_H_INCLUDED #define MAINFRAME_H_INCLUDED #include <wx/wx.h> #include "taskbaricon.h" class MainFrame: public wxFrame { public: MainFrame(wxWindow *parent, const wxString &title); ~MainFrame(); ...... private: TaskBarIcon* m_tbIcon; }; #endif //MAINFRAME_H_INCLUDED
Code wxWidgets:#include "mainframe.h" MainFrame::MainFrame(wxWindow *parent, const wxString& title) : wxFrame(parent, wxID_ANY, title) { m_tbIcon=new TaskBarIcon(this); m_tbIcon->SetIcon(wxICON(appicon16x16), GetTitle()); } MainFrame::~MainFrame() { delete m_tbIcon; } ...
J'ai copié/collé ce code (en le simplifiant) depuis l'application "wxToDoList" présente dans la rubrique "projets" de ce site. Ne t'étonnes donc pas trop s'il reste quelques lignes incompréhensibles...
@+ Xav'
|
Le nouveau portail wxWidgets francophone : www.wxdev.fr Ben en fait, vous y êtes déjà... et effectivement, depuis le temps, ce n'est plus tellement nouveau....
|
ajm (Membre)
Inscrit le : 16-03-2011
Messages: 82
Snippets: 0
Tutoriels: 0
Hors ligne |
Merci Xav, je vais essayer de comprendre les codes.
|
|
|