Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / How to declare event in the Class?

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. How to declare event in the Class?
#4602
Posted by: mooncake 2004-06-24 19:50:12
I have a class, for ex:

class myclass {
    public:        
        myclass(wxWindow* parent, wxWindowID id);

        void MoveMouse(wxMouseEvent & event);
};

myclass::myclass(wxWindow* parent, wxWindowID id) {
     
     ....
     
}

I wish to declare mouse event in the class, could somebody guide me?

Thank you.
Meow~
Message2. Re: How to declare event in the Class?
#4604
Posted by: upCASE 2004-06-24 21:16:03
Hi!
Basically this class can't handle an event, because it's not derived from wxEventHandler in any way.
Derive your class from a wxFrame for example (there is another way, like deriving it from wxEventHandler and set it dynamically as an event handling class for a frame, but this is way more complicated). Be sure to use the DECLARE_EVENT_TABLE() macro in the class definition.
In the implementation file use
BEGIN_EVENT_TABLE(myClass, wxFrame)
...
END_EVENT_TABLE()

Between these to macros use either on of the following:
EVT_LEFT_DOWN(func)  Process a wxEVT_LEFT_DOWN event. 
EVT_LEFT_UP(func)  Process a wxEVT_LEFT_UP event. 
EVT_LEFT_DCLICK(func)  Process a wxEVT_LEFT_DCLICK event. 
EVT_MIDDLE_DOWN(func)  Process a wxEVT_MIDDLE_DOWN event. 
EVT_MIDDLE_UP(func)  Process a wxEVT_MIDDLE_UP event. 
EVT_MIDDLE_DCLICK(func)  Process a wxEVT_MIDDLE_DCLICK event. 
EVT_RIGHT_DOWN(func)  Process a wxEVT_RIGHT_DOWN event. 
EVT_RIGHT_UP(func)  Process a wxEVT_RIGHT_UP event. 
EVT_RIGHT_DCLICK(func)  Process a wxEVT_RIGHT_DCLICK event. 
EVT_MOTION(func)  Process a wxEVT_MOTION event. 
EVT_ENTER_WINDOW(func)  Process a wxEVT_ENTER_WINDOW event. 
EVT_LEAVE_WINDOW(func)  Process a wxEVT_LEAVE_WINDOW event. 
EVT_MOUSEWHEEL(func)  Process a wxEVT_MOUSEWHEEL event.
or
EVT_MOUSE_EVENTS(func)  Process all mouse events.

Specify myClass::MoveMouse as "func" in the above.
Compile, run, be happy :)
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: How to declare event in the Class?
#4606
Posted by: mooncake 2004-06-24 21:37:45
Hi Upcase, btw, i wish this class can be reuse in many times..

for ex:

new myclass(this,id1);
new myclass(this,id2);
...

And.. as i follow your step, it prompt me the following error messages..

26 C:\base.cpp
`const wxEventTable myclass::sm_eventTable' is not a  static member of `class myclass'

156 C:\Dev-Cpp\include\wx\msw\frame.h
`const wxEventTable  wxFrame::sm_eventTable' is protected


Thank you...
Meow~
Message4. Re: How to declare event in the Class?
#4608
Posted by: upCASE 2004-06-24 22:54:03
Hi!
So, what EXACTLY do you want myClass to do/be??

"reuse in many times"
Like an event handling class in java?

Please, be more specific and maybe post an example of what you did...

BTW: The errors above are most likely from miss-use of the event table macros. Check if you really used BEGIN_EVENT_TABLE() and passed the correct parameters.
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message5. Re: How to declare event in the Class?
#4609
Posted by: mooncake 2004-06-24 23:09:43
Hi Upcase,
   I replace the code to class myclass: public wxFrame and it is free error already.

Meanwhile, what happen to the EVT_RIGHT_DOWN, when i right click the mouse on the frame, it is no happening...

void myclass::Move(wxMouseEvent & WXUNUSED(event)) {
    wxMessageBox("A");


Thank you
Meow~
Message6. Re: How to declare event in the Class?
#4612
Posted by: upCASE 2004-06-25 02:33:45
Hi!
Check out this example:

#ifndef __BASE_H
#define __BASE_H

class MainApp: public wxApp
{
  public:
      virtual bool OnInit();
};

class MainFrame: public wxFrame
{
  public:
      MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
      void OnMouse( wxMouseEvent &event);
      void OnQuit(wxCommandEvent &event);
  private:
      DECLARE_EVENT_TABLE()
};

enum
{
   ID_MAINWIN_QUIT = wxID_HIGHEST+1
   
};
#endif


#include <wx/wxprec.h>
#ifndef WX_PRECOMP
   #include <wx/wx.h>
#endif

#include "base.h"

IMPLEMENT_APP(MainApp)

bool MainApp::OnInit()
{
   MainFrame *win = new MainFrame("Frame", wxPoint (100, 100),
     wxSize(450, 340));
   win->Show(TRUE);
   SetTopWindow(win);

   return TRUE;
}


BEGIN_EVENT_TABLE(MainFrame, wxFrame)
   EVT_MENU(ID_MAINWIN_QUIT, MainFrame::OnQuit)
   EVT_RIGHT_DOWN(MainFrame::OnMouse) 
END_EVENT_TABLE()

MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
    : wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
    wxMenu *FileMenu = new wxMenu;
    wxMenuBar *MenuBar = new wxMenuBar;

    FileMenu->Append(ID_MAINWIN_QUIT, "&Quit");

    MenuBar->Append(FileMenu, "&File");
    SetMenuBar(MenuBar);

}

void MainFrame::OnMouse(wxMouseEvent &event)
{
    wxMessageBox("You clicked the frame!");
}

void MainFrame::OnQuit(wxCommandEvent & WXUNUSED(event))
{
    Close(TRUE);
}
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
¥287.00 ·
 
 
¥2,600.00 ·
 
 
¥289.00 ·
 
 
¥1,370.00 ·
 
 
¥289.00 ·
 
 
¥1,480.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0