사용자 이벤트


사용자 이벤트 사용법
1) 이벤트 클래스 생성
2) 한 개 이상의 이벤트 타입 등록, wxEventTypeTag 템플릿 클래스 사용, 정적 이벤트 테이블 지원용 매크로 추가
3) 이벤트 테이블 생성(동적 이벤트 테이블 또는 정적 이벤트 테이블)
4) 이벤트 발생시킴

MyApp.h
#pragma  once

#include  <wx/wx.h>
class MyApp:public wxApp
{
public:
    MyApp(void);
    ~MyApp(void);

   virtual  bool OnInit();
};

DECLARE_APP(MyApp);

MyApp.cpp
#include  "MyApp.h"
#include  "MyFrame.h"

IMPLEMENT_APP(MyApp);

MyApp::MyApp(void)
{
}


MyApp::~MyApp(void)
{
}


bool
MyApp::OnInit(){
   wxFrame* m = new  MyFrame("My Hello Event");
    m->Show(true);
   return  true;
}

HelloEvent.h
#pragma  once

#include  <wx/wx.h>

class  HelloEvent:public  wxEvent
{
private:
   wxString data_;

public:
    HelloEvent(int winid = 0, wxEventType commandType = wxEVT_NULL);
    ~HelloEvent(void);

   void data(wxString data);
   wxString data();

   virtual  wxEvent *Clone() const;
};

wxDECLARE_EVENT(EVT_HELLO1,HelloEvent);
wxDECLARE_EVENT(EVT_HELLO2,HelloEvent);
wxDECLARE_EVENT(EVT_HELLO3,wxCommandEvent);

/// 정적  이벤트  테이블  지원
typedef  void (wxEvtHandler::*wxHelloEventFunction)(HelloEvent&);

#define  wxHelloEventHandler(func) \
    wxEVENT_HANDLER_CAST(wxHelloEventFunction, func)

#  define  MEVT_HELLO1(func) \
   wx__DECLARE_EVT0(EVT_HELLO1,wxHelloEventHandler(func))

#  define  MEVT_HELLO2(func) \
   wx__DECLARE_EVT0(EVT_HELLO2,wxHelloEventHandler(func))

#  define  MEVT_HELLO3(func) \
   wx__DECLARE_EVT0(EVT_HELLO3,wxCommandEventHandler(func))


HelloEvent.cpp
#include  "HelloEvent.h"

wxDEFINE_EVENT(EVT_HELLO1,HelloEvent);
wxDEFINE_EVENT(EVT_HELLO2,HelloEvent);
wxDEFINE_EVENT(EVT_HELLO3,wxCommandEvent);


HelloEvent::HelloEvent(int  winid, wxEventType  commandType)
    :wxEvent(winid, commandType)
{
}


HelloEvent::~HelloEvent(void)
{
}


wxEvent *
HelloEvent::Clone() const{
   return  new  HelloEvent( *this);
}

void
HelloEvent::data(wxString  data){
   this->data_= data;
}

wxString
HelloEvent::data(){
   return  this->data_;
}

MyFrame.h
#pragma  once
#include  <wx/wx.h>
#include  "HelloEvent.h"

class  MyFrame:public  wxFrame
{
private:
   wxTextCtrl* text_;

public:
    MyFrame(const  wxString& title);
    ~MyFrame(void);

private:
   void OnText(wxCommandEvent& event);
   void OnEvt1(HelloEvent& event);
   void OnEvt2(HelloEvent& event);
   void OnEvt3(wxCommandEvent& event);
   void OnStaticEvt(wxCommandEvent& event);

   DECLARE_EVENT_TABLE()
};

MyFrame.cpp
#include  "MyFrame.h"

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
   MEVT_HELLO3(MyFrame::OnStaticEvt)
END_EVENT_TABLE()


MyFrame::MyFrame(const  wxString& title)
    :wxFrame(NULL, wxID_ANY, title)
{
   wxPanel* panel = new  wxPanel(this);
    text_ = new  wxTextCtrl(panel,wxID_ANY,"",wxPoint(100,50));

    text_->Bind(wxEVT_TEXT,&MyFrame::OnText, this);
    text_->Bind(EVT_HELLO1,&MyFrame::OnEvt1, this);
    text_->Bind(EVT_HELLO2,&MyFrame::OnEvt2, this);
    text_->Bind(EVT_HELLO3, &MyFrame::OnEvt3, this);

}

void 
MyFrame::OnStaticEvt(wxCommandEvent& event){
   wxString data = wxString::Format("static event %s", event.GetString());
    wxMessageBox(data);
}

void
MyFrame::OnEvt3(wxCommandEvent& event){
    wxMessageBox(event.GetString());
   event.Skip(true);
}

void
MyFrame::OnEvt1(HelloEvent& event){
    wxMessageBox(event.data());
}

void
MyFrame::OnEvt2(HelloEvent& event){
    wxMessageBox(event.data());
}

MyFrame::~MyFrame(void)
{
}

void
MyFrame::OnText(wxCommandEvent& event){
   wxString data = this->text_->GetValue();
   if( "evt1"==data){
      HelloEvent evt(this->text_->GetId(),EVT_HELLO1);
       evt.SetEventObject( this->text_);
       evt.data("Hello Event Type 1");
      this->text_->ProcessWindowEvent(evt);
   }

   if( "evt2"==data){
      HelloEvent evt(this->text_->GetId(),EVT_HELLO2);
       evt.SetEventObject( this->text_);
       evt.data("Hello Event Type 2");
      this->text_->ProcessWindowEvent(evt);
   }

   if( "evt3"==data){
      wxCommandEvent evt(EVT_HELLO3, this->text_->GetId());
       evt.SetEventObject(this->text_);
       evt.SetString("Hello Event Type 3");
      this->text_->ProcessWindowEvent(evt);
   }

}

- 목록:

3 thoughts on “wxPaintDC 이해하기

  1. 공유해주신 동영상에서 많은 것을 배우고 있습니다. 감사합니다.
    윈도우에서 GUI 프로그래밍을 가끔 하지만, RAD 툴에서 만들어주는 코드를 약간 변형하는 정도입니다.
    동영상을 보다보니, 툴이 생성해주는 코드들에 대해 좀 더 이해할 수 있을 것 같습니다.
    wxSmith 를 이용하여 wxWidget 를 사용하려 알아보는 중인데, 설명을 들으니 wxSmith 나 wxFormWBuilder 가 해주는 일을 약간은 이해할 수 있게 된 것 같습니다. 해당 툴을 사용하는데 많은 도움이 될 것 같습니다.
    좋은 강좌에 감사드립니다.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>