Search

2011年6月7日 星期二

wxWidgets

自從去年暑假Plurk上某大大推薦使用wxWidgets後,跑去用了一下感覺真的是簡單易用,如果對MFC有一定的概念其實是很好上手的差異不會太大,有差異的不是wxApp衍生類別的實體化的形式不一樣就是子窗口或是選單創建的寫法不同這些都不過是雞毛蒜皮的微小差異,嚴格說起來除了去年腸病毒住院用兩天寫一個一千五百行的彈幕(wxWidgets + OpenGL)外沒寫過太大的project 都弄一些小工具XD,不過很有趣的是完全不用修改就可以在windows & linux 編譯過,而且效能不見得會比MFC差甚至更好!



其實寫這篇主要是為了新增一個"wxWidgets"的標籤好提醒我要作紀錄XD所以下面附上百合子還有GL簡單的Demo XD





#include <wx/wx.h>

class MyApp:public wxApp
{
   public:
      bool OnInit();
};

DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)

class MyFrame:public wxFrame
{
   public:
      MyFrame();

      void SetWindowShape();

      void OnLeftDown(wxMouseEvent &event);
      void OnLeftUp(wxMouseEvent &event);
      void OnMouseMove(wxMouseEvent &event);
      void OnExit(wxMouseEvent &event);
      void OnPaint(wxPaintEvent &event);
      void OnWindowCreate(wxWindowCreateEvent &event);

   private:
      wxBitmap bmp;
      wxPoint point;

      DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(MyFrame,wxFrame)
   EVT_LEFT_DOWN(MyFrame::OnLeftDown)
   EVT_LEFT_UP(MyFrame::OnLeftUp)
   EVT_MOTION(MyFrame::OnMouseMove)
   EVT_RIGHT_DOWN(MyFrame::OnExit)
   EVT_PAINT(MyFrame::OnPaint)
   EVT_WINDOW_CREATE(MyFrame::OnWindowCreate)
END_EVENT_TABLE()

bool MyApp::OnInit()
{
   wxInitAllImageHandlers();

   MyFrame *frame = new MyFrame();

   frame->Show(true);

   return true;
}

MyFrame::MyFrame():wxFrame(NULL,wxID_ANY,wxEmptyString,wxDefaultPosition,wxSize(100,100),0 | wxFRAME_SHAPED | wxSIMPLE_BORDER | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP)
{
   bmp = wxBitmap(wxT("s.png"),wxBITMAP_TYPE_PNG);
   SetSize(wxSize(bmp.GetWidth(),bmp.GetHeight()));
   SetToolTip(wxT("Right Exit"));
}

void MyFrame::SetWindowShape()
{
   wxRegion region(bmp,*wxWHITE);
   SetShape(region);
}

void MyFrame::OnLeftDown(wxMouseEvent &event)
{
   CaptureMouse();
   wxPoint pt = ClientToScreen(event.GetPosition());
   wxPoint origin = GetPosition();
   int dx = pt.x - origin.x;
   int dy = pt.y - origin.y;
   point = wxPoint(dx,dy);
}

void MyFrame::OnLeftUp(wxMouseEvent &event)
{
   if(HasCapture()){
      ReleaseMouse();
   }
}

void MyFrame::OnMouseMove(wxMouseEvent &event)
{
   wxPoint pt = event.GetPosition();
   if(event.Dragging() && event.LeftIsDown()){
      wxPoint pos = ClientToScreen(pt);
      Move(wxPoint(pos.x - point.x,pos.y - point.y));
   }
}

void MyFrame::OnExit(wxMouseEvent &event)
{
   Close();
}

void MyFrame::OnPaint(wxPaintEvent &event)
{
   wxPaintDC paint(this);
   paint.DrawBitmap(bmp,0,0,true);
}

void MyFrame::OnWindowCreate(wxWindowCreateEvent &event)
{
   SetWindowShape();
}



g++ -g -o name name.cpp `wx-config --cxxflags --libs --unicode`


#include <wx/wx.h>
#include <wx/glcanvas.h>


class TestGLCanvas;


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


class MyFrame:public wxFrame
{
   public:
      MyFrame(const wxString&);
      ~MyFrame();
      TestGLCanvas *m_canvas;
};


class TestGLCanvas: public wxGLCanvas
{
   friend class MyFrame;
   public:
   TestGLCanvas(wxWindow *parent,wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,const wxString& name = wxT("TestGLCanvas"));


   ~TestGLCanvas();


   void OnPaint(wxPaintEvent &event);
   void OnSize(wxSizeEvent& event);


   private:
   DECLARE_EVENT_TABLE()
};


DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)


BEGIN_EVENT_TABLE(TestGLCanvas,wxGLCanvas)
   EVT_PAINT( TestGLCanvas::OnPaint)
   EVT_SIZE( TestGLCanvas::OnSize)
END_EVENT_TABLE()


bool MyApp::OnInit()
{
   MyFrame *frame = new MyFrame(wxT("(〃∀〃)"));


   frame->m_canvas = new TestGLCanvas(frame);


   frame->Show(true);


   return true;
}


MyFrame::MyFrame(const wxString &title):wxFrame(NULL,wxID_ANY,title)
{
}


MyFrame::~MyFrame()
{
}


TestGLCanvas::TestGLCanvas(wxWindow *parent,wxWindowID id,
const wxPoint& pos,const wxSize& size,long style,const wxString& name)
: wxGLCanvas(parent,(wxGLCanvas*)NULL,id,pos,size,style|wxFULL_REPAINT_ON_RESIZE ,name )
{
}


TestGLCanvas::~TestGLCanvas()
{
}


void TestGLCanvas::OnPaint(wxPaintEvent &event)
{
   wxPaintDC dc(this);
   SetCurrent();
   float vertex[] = {0.0f, 0.0f, 0.0f};
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glBegin(GL_POINTS);
   glVertex3fv(vertex);
   glEnd();
   SwapBuffers();
}


void TestGLCanvas::OnSize(wxSizeEvent& event)
{
   wxGLCanvas::OnSize(event);


   int w, h;
   GetClientSize(&w, &h);


   if (GetContext()){
      SetCurrent();
      glViewport(0, 0, (GLint) w, (GLint) h);
   }
}


g++ -g -o name name.cpp `wx-config --cxxflags --libs --unicode --gl-libs`

沒有留言:

張貼留言