在屏幕上画简单图形和显示图片、处理简单鼠标键盘事件
/*************************************************************** * Name: MyApp.h * Purpose: Defines Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTAPP_H #define WXTESTAPP_H #include <wx/app.h> class MyApp : public wxApp //应用程序类,应用程序的入口点 { public: virtual bool OnInit(void); //在应用程序启动时调用,如果返回false,退出应用程序 }; #endif // WXTESTAPP_H
/*************************************************************** * Name: MyApp.cpp * Purpose: Code for Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include <wx/wx.h> //(*AppHeaders #include "MyApp.h" #include "MyFrame.h" //*) DECLARE_APP(MyApp); IMPLEMENT_APP(MyApp); bool MyApp::OnInit(void) { MyFrame * frame = new MyFrame(NULL, wxT("wxWidgets")); //为新窗口分配空间,设定标题 frame->Show(); return wxOK; }
/*************************************************************** * Name: MyFrame.h * Purpose: Defines Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTMAIN_H #define WXTESTMAIN_H #include <wx/frame.h> class MyFrame : public wxFrame //窗体类 { public: //Constructor MyFrame(wxWindow * parent, const wxString & title); //窗体的构造函数 //Destructor ~MyFrame() {}; //Event handlers void OnAbout (wxCommandEvent & event); void OnQuit (wxCommandEvent & event); void OnMotion (wxMouseEvent & event); void OnPaint (wxPaintEvent & event); void OnMousePrc (wxMouseEvent & event); void OnKeyPrc (wxKeyEvent & event); private: DECLARE_EVENT_TABLE(); }; #endif // WXTESTMAIN_H
/*************************************************************** * Name: MyFrame.cpp * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include <wx/wx.h> #include <wx/dcbuffer.h> //(*FrameHeaders #include "MyApp.h" #include "MyFrame.h" #include "wxPingGe.h" //*) //事件表:(类,基类) BEGIN_EVENT_TABLE(MyFrame,wxFrame) //(*EventTable(MyFrame) EVT_MENU (wxID_ABOUT, MyFrame::OnAbout) //把wxID_ABOUT和OnAbout关联 EVT_MENU (wxID_EXIT, MyFrame::OnQuit) //把wxID_EXIT和OnQuit关联 EVT_MOTION ( MyFrame::OnMotion) //鼠标事件 EVT_PAINT ( MyFrame::OnPaint) //刷屏消息 EVT_MOUSE_EVENTS( MyFrame::OnMousePrc) //鼠标事件 EVT_KEY_DOWN ( MyFrame::OnKeyPrc) //键盘事件 //*) END_EVENT_TABLE() #include "wx.xpm" MyFrame::MyFrame(wxWindow * parent, const wxString & title) : wxFrame(NULL, wxID_ANY, title) { SetIcon(wxIcon(wx_xpm)); wxMenu * fileMenu = new wxMenu; //建立“文件”菜单 wxMenu * helpMenu = new wxMenu; //建立“帮助”菜单 //增加菜单项(标识、文本、帮助字符) helpMenu->Append(wxID_ABOUT, wxT("&About... F2"), wxT("Show about dialog")); fileMenu->Append(wxID_EXIT, wxT("&Exit... Alt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //建立一个菜单条 menuBar->Append(fileMenu, wxT("&File")); //将"文件"菜单加入到菜单条 menuBar->Append(helpMenu, wxT("&Help")); //将“帮助”菜单加入到菜单条 SetMenuBar(menuBar); //将菜单条放到窗体上 CreateStatusBar(2); //创建两个状态栏,并写上字符串 SetStatusText(wxT("welcome to wxWidgets!"), 1); } void MyFrame::OnAbout(wxCommandEvent & event) { wxString msg; msg.Printf(wxT("hello and welcome to %s"), wxVERSION_STRING); //消息的字符串 //消息框:(消息内容、标题、窗口类型、父窗口) wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); } void MyFrame::OnQuit(wxCommandEvent & event) { Close(); } void MyFrame::OnMotion(wxMouseEvent & event) { if(event.Dragging()) //鼠标拖动时为真 { wxClientDC dc(this); //创建一个指向当前窗口的设备指针dc wxPen pen(*wxRED, 1); //创建画笔,颜色:红色,宽度:1 dc.SetPen(pen); dc.DrawPoint(event.GetPosition()); //画点 dc.SetPen(wxNullPen); } } void MyFrame::OnPaint(wxPaintEvent & event) //刷屏消息处理函数 { wxBufferedPaintDC mydc(this); mydc.SetPen(*wxWHITE_PEN); //mydc.SetBrush(*wxRED_BRUSH); mydc.SetBrush(wxColor(255, 0, 0)/*wxRED_BRUSH*/); mydc.SetBackground(wxBrush(*wxWHITE, wxSOLID)); wxSize sz = GetClientSize(); //获取窗口大小 wxCoord w = 100, h = 50; //要绘制的矩形大小 int x = wxMax(0, sz.x / 2) - w / 2; //将矩形放置在窗口的正中间,但不为负 int y = wxMax(0, sz.y / 2) - h / 2; wxRect rectToDraw(x, y, w, h); if(IsExposed(rectToDraw)) //只有在需要的时候才重新画以便提高效率 { //mydc.DrawRectangle(rectToDraw); //画矩形 //DrawTextString(mydc, wxT("test"), wxPoint(50, 50)); //画字符串 //DrawRotatedTest(mydc, wxT("PingGe"), wxPoint(100, 100), 60); //旋转字符串 DrawSimpleShape(mydc); DrawMap(mydc); } /* 窗口最大化后出现两个矩形,原因是窗口变大时原先部分没有刷新, 只把新增部分刷新出来; */ } void MyFrame::OnMousePrc(wxMouseEvent & event) { wxPoint pt(event.GetPosition()); //获取当前坐标pt wxClientDC dc(this); //设置一个设备上下文 if((pt.x > 100) && (pt.y > 100) && (event.LeftDown())) { wxPen pen(*wxWHITE, 1); //白色画笔 dc.SetPen(pen); dc.DrawLine(0, 0, 1280, 1024); //当鼠标左键点击位置在(100, 100)之后时用白色画笔画线 } if(event.LeftUp()) { wxPen pen(*wxRED, 1); //红色画笔 dc.SetPen(pen); dc.DrawLine(0, 100, 1024, 100); //当鼠标左键弹起时,用红色画笔画线 } } void MyFrame::OnKeyPrc(wxKeyEvent & event) //处理按键按下的消息 { wxClientDC dc(this); //设置一个设备上下文 wxPen pen(*wxWHITE, 1); dc.SetPen(pen); //白色画笔 if(event.ShiftDown()) //按下shift键画一条直线 { dc.DrawLine(0, 50, 1280, 50); } int code = event.GetKeyCode(); //获取按下按键的编码 if(code == 65) { dc.DrawLine(0, 40, 1280, 40); //当按下A键时画一条直线 } }
/*************************************************************** * Name: wxPingGe.h * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef _WXPINGGE_H_ #define _WXPINGGE_H_ #include <wx/wx.h> #include <wx/dcbuffer.h> //画字符函数:(DC,文本,位置) void DrawTextString(wxDC & dc, const wxString & text, const wxPoint & pt); //画旋转字符函数:(DC,文本,位置,旋转角度) void DrawRotatedTest(wxDC & dc, const wxString & text, const wxPoint & pt, int angle = 0); //画一些图形 void DrawSimpleShape(wxDC & dc); //画图片 void DrawMap(wxDC & dc); #endif //_WXPINGGE_H_
/*************************************************************** * Name: wxPingGe.cpp * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include "wxPingGe.h" void DrawTextString(wxDC & dc, const wxString & text, const wxPoint & pt) { //定义一个wxFont类的对象font //构造函数wxFont::wxFont(字体大小、字体类型(书法、艺术)、斜体) wxFont font(50, wxFONTFAMILY_SCRIPT, wxNORMAL, wxBOLD); //利用DC类的成员函数SetFont设置字体 dc.SetFont(font); //设置背景透或者不透 dc.SetBackgroundMode(wxSOLID/*wxTRANSPARENT*/); //设置前景颜色 dc.SetTextForeground(*wxRED); //设置背景颜色 dc.SetTextBackground(*wxWHITE); //写字,文本wxString,位置wxPoint dc.DrawText(text, pt); } void DrawRotatedTest(wxDC & dc, const wxString & text, const wxPoint & pt, int angle) { wxFont font(50, wxFONTFAMILY_SCRIPT, wxNORMAL, wxBOLD); dc.SetFont(font); dc.SetBackgroundMode(wxTRANSPARENT/*wxSOLID*/); dc.SetTextForeground(*wxWHITE); dc.SetTextBackground(*wxRED); dc.DrawRotatedText(text, pt, angle); } void DrawSimpleShape(wxDC & dc) { //设置画笔画刷 dc.SetPen(wxPen(*wxRED, 1, wxSOLID)); dc.SetBrush(wxBrush(*wxGREEN, wxSOLID)); //画抛物线 dc.DrawSpline(0, 100, 100, 0, 200, 100); //画点 dc.DrawPoint(5, 5); //画线:(注意,最后一个点不会画) dc.DrawLine(10, 10, 100, 100); //画矩形:(用画笔画边框,画刷填充内部) dc.DrawRectangle(50, 50, 150, 100); //设置画刷为红色 dc.SetBrush(*wxRED_BRUSH); //画圆角矩形 dc.DrawRoundedRectangle(150, 20, 100, 50, 10); //设置颜色 dc.SetBrush(wxColor(255, 255, 0)); //画圆 dc.DrawCircle(100, 150, 60); } #include "1.xpm" void DrawMap(wxDC & dc) { wxBitmap bitmap(logo_xpm); dc.DrawBitmap(bitmap, 200, 100, wxBITMAP_TYPE_XPM); dc.SetTextBackground(*wxBLACK); dc.SetTextForeground(*wxWHITE); wxString msg = wxT("Some text is mixed in bmp shade."); for(int i = 0, step = 75; i < 10; i++, step += 20) { dc.DrawText(msg, 200, step); } }
在屏幕上画普通文字和旋转的文字
/*************************************************************** * Name: MyApp.h * Purpose: Defines Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTAPP_H #define WXTESTAPP_H #include <wx/app.h> class MyApp : public wxApp //应用程序类,应用程序的入口点 { public: virtual bool OnInit(void); //在应用程序启动时调用,如果返回false,退出应用程序 }; #endif // WXTESTAPP_H
/*************************************************************** * Name: MyApp.cpp * Purpose: Code for Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include <wx/wx.h> //(*AppHeaders #include "MyApp.h" #include "MyFrame.h" //*) DECLARE_APP(MyApp); IMPLEMENT_APP(MyApp); bool MyApp::OnInit(void) { MyFrame * frame = new MyFrame(NULL, wxT("wxWidgets")); //为新窗口分配空间,设定标题 frame->Show(); return wxOK; }
/*************************************************************** * Name: MyFrame.h * Purpose: Defines Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTMAIN_H #define WXTESTMAIN_H #include <wx/frame.h> class MyFrame : public wxFrame //窗体类 { public: //Constructor MyFrame(wxWindow * parent, const wxString & title); //窗体的构造函数 //Destructor ~MyFrame() {}; //Event handlers void OnAbout (wxCommandEvent & event); void OnQuit (wxCommandEvent & event); void OnMotion (wxMouseEvent & event); void OnPaint (wxPaintEvent & event); private: DECLARE_EVENT_TABLE(); }; #endif // WXTESTMAIN_H
/*************************************************************** * Name: MyFrame.cpp * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include <wx/wx.h> #include <wx/dcbuffer.h> //(*FrameHeaders #include "MyApp.h" #include "MyFrame.h" #include "wxPingGe.h" //*) //事件表:(类,基类) BEGIN_EVENT_TABLE(MyFrame,wxFrame) //(*EventTable(MyFrame) EVT_MENU (wxID_ABOUT, MyFrame::OnAbout) EVT_MENU (wxID_EXIT, MyFrame::OnQuit) EVT_MOTION ( MyFrame::OnMotion) EVT_PAINT ( MyFrame::OnPaint) //*) END_EVENT_TABLE() #include "wx.xpm" MyFrame::MyFrame(wxWindow * parent, const wxString & title) : wxFrame(NULL, wxID_ANY, title) { SetIcon(wxIcon(wx_xpm)); wxMenu * fileMenu = new wxMenu; //建立“文件”菜单 wxMenu * helpMenu = new wxMenu; //建立“帮助”菜单 //增加菜单项(标识、文本、帮助字符) helpMenu->Append(wxID_ABOUT, wxT("&About... F2"), wxT("Show about dialog")); fileMenu->Append(wxID_EXIT, wxT("&Exit... Alt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //建立一个菜单条 menuBar->Append(fileMenu, wxT("&File")); //将"文件"菜单加入到菜单条 menuBar->Append(helpMenu, wxT("&Help")); //将“帮助”菜单加入到菜单条 SetMenuBar(menuBar); //将菜单条放到窗体上 CreateStatusBar(2); //创建两个状态栏,并写上字符串 SetStatusText(wxT("welcome to wxWidgets!")); } void MyFrame::OnAbout(wxCommandEvent & event) { wxString msg; msg.Printf(wxT("hello and welcome to %s"), wxVERSION_STRING); //消息的字符串 //消息框:(消息内容、标题、窗口类型、父窗口) wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); } void MyFrame::OnQuit(wxCommandEvent & event) { Close(); } void MyFrame::OnMotion(wxMouseEvent & event) { if(event.Dragging()) //鼠标拖动时为真 { wxClientDC dc(this); //创建一个指向当前窗口的设备指针dc wxPen pen(*wxRED, 1); //创建画笔,颜色:红色,宽度:1 dc.SetPen(pen); dc.DrawPoint(event.GetPosition()); //画点 dc.SetPen(wxNullPen); } } void MyFrame::OnPaint(wxPaintEvent & event) //刷屏消息处理函数 { wxBufferedPaintDC mydc(this); mydc.SetPen(*wxBLACK_PEN); mydc.SetBrush(*wxRED_BRUSH); wxSize sz = GetClientSize(); //获取窗口大小 wxCoord w = 100, h = 50; //要绘制的矩形大小 int x = wxMax(0, sz.x / 2) - w / 2; //将矩形放置在窗口的正中间,但不为负 int y = wxMax(0, sz.y / 2) - h / 2; wxRect rectToDraw(x, y, w, h); if(IsExposed(rectToDraw)) //只有在需要的时候才重新画以便提高效率 { mydc.DrawRectangle(rectToDraw); DrawTextString(mydc, wxT("test"), wxPoint(50, 50)); //画字符串 DrawRotatedTest(mydc, wxT("PingGe"), wxPoint(100, 100), 60); //旋转字符串 } /* 窗口最大化后出现两个矩形,原因是窗口变大时原先部分没有刷新, 只把新增部分刷新出来; */ }
/*************************************************************** * Name: wxPingGe.h * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef _WXPINGGE_H_ #define _WXPINGGE_H_ #include <wx/wx.h> #include <wx/dcbuffer.h> //画字符函数:(DC,文本,位置) void DrawTextString(wxDC & dc, const wxString & text, const wxPoint & pt); //画旋转字符函数:(DC,文本,位置,旋转角度) void DrawRotatedTest(wxDC & dc, const wxString & text, const wxPoint & pt, int angle = 0); #endif //_WXPINGGE_H_
/*************************************************************** * Name: wxPingGe.cpp * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include "wxPingGe.h" void DrawTextString(wxDC & dc, const wxString & text, const wxPoint & pt) { //定义一个wxFont类的对象font //构造函数wxFont::wxFont(字体大小、字体类型(书法、艺术)、斜体) wxFont font(50, wxFONTFAMILY_SCRIPT, wxNORMAL, wxBOLD); //利用DC类的成员函数SetFont设置字体 dc.SetFont(font); //设置背景透或者不透 dc.SetBackgroundMode(wxSOLID/*wxTRANSPARENT*/); //设置前景颜色 dc.SetTextForeground(*wxRED); //设置背景颜色 dc.SetTextBackground(*wxWHITE); //写字,文本wxString,位置wxPoint dc.DrawText(text, pt); } void DrawRotatedTest(wxDC & dc, const wxString & text, const wxPoint & pt, int angle) { wxFont font(50, wxFONTFAMILY_SCRIPT, wxNORMAL, wxBOLD); dc.SetFont(font); dc.SetBackgroundMode(wxTRANSPARENT/*wxSOLID*/); dc.SetTextForeground(*wxWHITE); dc.SetTextBackground(*wxRED); dc.DrawRotatedText(text, pt, angle); }
在屏幕上画点和矩形
/*************************************************************** * Name: MyApp.h * Purpose: Defines Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTAPP_H #define WXTESTAPP_H #include <wx/app.h> class MyApp : public wxApp { public: virtual bool OnInit(void); }; #endif // WXTESTAPP_H
/*************************************************************** * Name: MyApp.cpp * Purpose: Code for Application Class * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include "MyApp.h" //(*AppHeaders #include "wx/wx.h" #include "MyFrame.h" //*) DECLARE_APP(MyApp); IMPLEMENT_APP(MyApp); bool MyApp::OnInit(void) { MyFrame * frame = new MyFrame(NULL, wxT("wxWidgets")); //为新窗口分配空间,设定标题 frame->Show(); return wxOK; }
/*************************************************************** * Name: MyFrame.h * Purpose: Defines Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #ifndef WXTESTMAIN_H #define WXTESTMAIN_H #include <wx/frame.h> class MyFrame : public wxFrame { public: //Constructor MyFrame(wxWindow * parent, const wxString & title); //Destructor ~MyFrame() {}; //Event handlers void OnAbout (wxCommandEvent & event); void OnQuit (wxCommandEvent & event); void OnMotion (wxMouseEvent & event); void OnPaint (wxPaintEvent & event); private: DECLARE_EVENT_TABLE(); }; #endif // WXTESTMAIN_H
/*************************************************************** * Name: MyFrame.cpp * Purpose: Code for Application Frame * Author: PingGe (414236069@qq.com) * Created: 2013-10-14 * Copyright: PingGe (http://www.cnblogs.com/pingge/) * License: **************************************************************/ #include "MyFrame.h" //(*InternalHeaders(wxTestDialog) #include "wx/wx.h" #include "wx/dcbuffer.h" #include "MyApp.h" //*) BEGIN_EVENT_TABLE(MyFrame,wxFrame) //(*EventTable(wxTestDialog) EVT_MENU (wxID_ABOUT, MyFrame::OnAbout) EVT_MENU (wxID_EXIT, MyFrame::OnQuit) EVT_MOTION ( MyFrame::OnMotion) EVT_PAINT ( MyFrame::OnPaint) //*) END_EVENT_TABLE() #include "wx.xpm" MyFrame::MyFrame(wxWindow * parent, const wxString & title) : wxFrame(NULL, wxID_ANY, title) { SetIcon(wxIcon(wx_xpm)); wxMenu * fileMenu = new wxMenu; //定义两个菜单项 wxMenu * helpMenu = new wxMenu; //增加菜单项(标识、文本、帮助字符) helpMenu->Append(wxID_ABOUT, wxT("&About... F2"), wxT("Show about dialog")); fileMenu->Append(wxID_EXIT, wxT("&Exit... Alt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //定义菜单条 menuBar->Append(fileMenu, wxT("&File")); menuBar->Append(helpMenu, wxT("&Help")); SetMenuBar(menuBar); CreateStatusBar(2); //创建两个状态栏,并写上字符串 SetStatusText(wxT("welcome to wxWidgets!")); } void MyFrame::OnAbout(wxCommandEvent & event) { wxString msg; msg.Printf(wxT("hello and welcome to %s"), wxVERSION_STRING); //消息的字符串 //消息框:(消息内容、标题、窗口类型、父窗口) wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); } void MyFrame::OnQuit(wxCommandEvent & event) { Close(); } void MyFrame::OnMotion(wxMouseEvent & event) { if(event.Dragging()) //鼠标拖动时为真 { wxClientDC dc(this); //创建一个指向当前窗口的设备指针dc wxPen pen(*wxRED, 1); //创建画笔,颜色:红色,宽度:1 dc.SetPen(pen); dc.DrawPoint(event.GetPosition()); dc.SetPen(wxNullPen); } } void MyFrame::OnPaint(wxPaintEvent & event) { wxBufferedPaintDC mydc(this); mydc.SetPen(*wxBLACK_PEN); mydc.SetBrush(*wxRED_BRUSH); wxSize sz = GetClientSize(); //获取窗口大小 wxCoord w = 100, h = 50; //要绘制的矩形大小 int x = wxMax(0, sz.x / 2) - w / 2; //将矩形放置在窗口的正中间,但不为负 int y = wxMax(0, sz.y / 2) - h / 2; wxRect rectToDraw(x, y, w, h); if(IsExposed(rectToDraw)) //只有在需要的时候才重新画以便提高效率 { mydc.DrawRectangle(rectToDraw); } /* 窗口最大化后出现两个矩形,原因是窗口变大时原先部分没有刷新, 只把新增部分刷新出来; */ }