wxWidgets自定义的窗口事件--转帖

    技术2022-05-19  22

    经常会碰到自定义窗口需要自己处理OnKeyDownOnKeyUpOnChar等事件,单有些时候接受不正常,这个时候就需要在自定义窗口中使用wxWANTS_CHARS 这个style,附上自己的例子代码:

    #include <wx/wx.h>

    #include <wx/splitter.h>

     

    class MyWin: public wxWindow

    {

    public:

           MyWin(wxWindow* parent);

           virtual ~MyWin(){};

     

    private:

           void OnKeyDown(wxKeyEvent& event);

           void OnKeyUp(wxKeyEvent& event);

           void OnChar(wxKeyEvent& event);

     

    private:

           DECLARE_EVENT_TABLE()

    };

     

    class MyFrame : public wxFrame

    {

    public:

           MyFrame(wxWindow *pParent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style = wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX | wxCAPTION | wxSYSTEM_MENU | wxRESIZE_BORDER | wxCLIP_CHILDREN, const wxString& name = wxT("Main Frame"));

           virtual ~MyFrame(void);

     

    private:

           void OnClose(wxCloseEvent &event);

           void OnQuit(wxCommandEvent& WXUNUSED(event));

     

    private:

           void CreateGUIControls(void);

    private:

           DECLARE_EVENT_TABLE()

    };

     

     

    class MyApp : public wxApp

    {

    private:

           MyFrame *m_pMainFrame;

     

    public:

           MyApp(void);

           virtual ~MyApp(void);

     

    protected:

           virtual bool OnInit();

    };

     

    IMPLEMENT_APP(MyApp)

     

    MyApp::MyApp(void)

    : m_pMainFrame(NULL)

    {

    }

     

    MyApp::~MyApp(void)

    {

    }

     

    bool MyApp::OnInit()

    {

           wxInitAllImageHandlers();

           m_pMainFrame = new MyFrame(NULL, wxID_ANY, wxT("MyApp"), wxDefaultPosition, wxDefaultSize);

     

           m_pMainFrame->Centre();

           m_pMainFrame->Show(TRUE);

     

           SetTopWindow(m_pMainFrame);

     

           return TRUE;

     

    BEGIN_EVENT_TABLE(MyFrame, wxFrame)

           EVT_CLOSE(MyFrame::OnClose)

           EVT_MENU(wxID_CLOSE, MyFrame::OnQuit)

    END_EVENT_TABLE()

     

    MyFrame::MyFrame(wxWindow *pParent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name)

    : wxFrame(pParent, id, title, pos, size, style, name)

    {

           CreateGUIControls();

    }

     

    MyFrame::~MyFrame(void)

    {

    }

     

    void MyFrame::CreateGUIControls(void)

    {

           wxMenu *menuFile = new wxMenu;

           menuFile->Append( wxID_CLOSE, wxT("E&xit") );

     

           wxMenu *menuHelp = new wxMenu;

           menuHelp->Append( wxID_ABOUT, wxT("&About...") );

     

           wxMenuBar *menuBar = new wxMenuBar;

           menuBar->Append( menuFile, wxT("&File") );

           menuBar->Append( menuHelp, wxT("&Help") );

     

           SetMenuBar( menuBar );

     

           CreateStatusBar();

           SetStatusText(wxT("Ready"));

           SetMinSize( wxSize( 800, 600 ) );

     

           wxSplitterWindow* m_pSashSplitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_3D | wxCLIP_CHILDREN | wxWANTS_CHARS  );

     

           MyWin* pWin = new MyWin(m_pSashSplitter);

           pWin->SetBackgroundColour(*wxGREEN);

           wxPanel* panel = new wxPanel( m_pSashSplitter);

           m_pSashSplitter->SplitVertically( pWin,panel,565);

     

    }

     

    void MyFrame::OnClose(wxCloseEvent& event)

    {

           event.Skip();

    }

     

    void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))

    {

           Close();

    }

     

    BEGIN_EVENT_TABLE(MyWin, wxWindow)

           EVT_KEY_UP( MyWin::OnKeyUp )

           EVT_KEY_DOWN( MyWin::OnKeyDown )

           EVT_CHAR( MyWin::OnChar )

    END_EVENT_TABLE()

     

    MyWin::MyWin(wxWindow* parent)

    : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,wxWANTS_CHARS )

    {

    }

     

    void MyWin::OnChar(wxKeyEvent& event)

    {

           wxLogDebug( wxString::Format(wxT("bbbb %d"), event.GetKeyCode()) );

           event.Skip();

    }

     

    void MyWin::OnKeyDown(wxKeyEvent& event)

    {

           switch(event.GetKeyCode())

           {

           case WXK_RIGHT:

                  wxLogDebug( wxT("right down") );

                  break;

           case WXK_LEFT:

                  wxLogDebug( wxT("left down") );

                  break;

           case WXK_UP:

                  wxLogDebug( wxT("up down") );

                  break;

           case WXK_DOWN:

                  wxLogDebug( wxT("down down") );

                  break;

           default:

                  break;

           }    

           event.Skip();

    }

     

    void MyWin::OnKeyUp(wxKeyEvent& event)

    {

           switch(event.GetKeyCode())

           {

           case WXK_RIGHT:

                  wxLogDebug( wxT("right up") );

                  break;

           case WXK_LEFT:

                  wxLogDebug( wxT("left up") );

                  break;

           case WXK_UP:

                  wxLogDebug( wxT("up up") );

                  break;

           case WXK_DOWN:

                  wxLogDebug( wxT("down up") );

                  break;

           default:

                  break;

           }    

           event.Skip();

    }


    最新回复(0)