其實寫這篇主要是為了新增一個"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`
沒有留言:
張貼留言