在屏幕上画简单图形和显示图片、处理简单鼠标键盘事件

/***************************************************************
* Name: MyApp.h
* Purpose: Defines Application Class
* Author: PingGe ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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...\tF2"), wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("&Exit...\tAlt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //建立一个菜单条
menuBar->Append(fileMenu, wxT("&File")); //将"文件"菜单加入到菜单条
menuBar->Append(helpMenu, wxT("&Help")); //将“帮助”菜单加入到菜单条 SetMenuBar(menuBar); //将菜单条放到窗体上 CreateStatusBar(); //创建两个状态栏,并写上字符串
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
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(, , )/*wxRED_BRUSH*/); mydc.SetBackground(wxBrush(*wxWHITE, wxSOLID)); wxSize sz = GetClientSize(); //获取窗口大小 wxCoord w = , h = ; //要绘制的矩形大小 int x = wxMax(, sz.x / ) - w / ; //将矩形放置在窗口的正中间,但不为负
int y = wxMax(, sz.y / ) - h / ; 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 > ) && (pt.y > ) && (event.LeftDown()))
{
wxPen pen(*wxWHITE, ); //白色画笔
dc.SetPen(pen);
dc.DrawLine(, , , ); //当鼠标左键点击位置在(100, 100)之后时用白色画笔画线
}
if(event.LeftUp())
{
wxPen pen(*wxRED, ); //红色画笔
dc.SetPen(pen);
dc.DrawLine(, , , ); //当鼠标左键弹起时,用红色画笔画线
}
} void MyFrame::OnKeyPrc(wxKeyEvent & event) //处理按键按下的消息
{
wxClientDC dc(this); //设置一个设备上下文 wxPen pen(*wxWHITE, );
dc.SetPen(pen); //白色画笔 if(event.ShiftDown()) //按下shift键画一条直线
{
dc.DrawLine(, , , );
} int code = event.GetKeyCode(); //获取按下按键的编码
if(code == )
{
dc.DrawLine(, , , ); //当按下A键时画一条直线
}
}
/***************************************************************
* Name: wxPingGe.h
* Purpose: Code for Application Frame
* Author: PingGe ([email protected])
* 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 = ); //画一些图形
void DrawSimpleShape(wxDC & dc); //画图片
void DrawMap(wxDC & dc); #endif //_WXPINGGE_H_
/***************************************************************
* Name: wxPingGe.cpp
* Purpose: Code for Application Frame
* Author: PingGe ([email protected])
* 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(, 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(, 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, , wxSOLID)); dc.SetBrush(wxBrush(*wxGREEN, wxSOLID)); //画抛物线
dc.DrawSpline(, , , , , ); //画点
dc.DrawPoint(, ); //画线:(注意,最后一个点不会画)
dc.DrawLine(, , , ); //画矩形:(用画笔画边框,画刷填充内部)
dc.DrawRectangle(, , , ); //设置画刷为红色
dc.SetBrush(*wxRED_BRUSH); //画圆角矩形
dc.DrawRoundedRectangle(, , , , ); //设置颜色
dc.SetBrush(wxColor(, , )); //画圆
dc.DrawCircle(, , );
} #include "1.xpm"
void DrawMap(wxDC & dc)
{
wxBitmap bitmap(logo_xpm);
dc.DrawBitmap(bitmap, , , wxBITMAP_TYPE_XPM); dc.SetTextBackground(*wxBLACK);
dc.SetTextForeground(*wxWHITE); wxString msg = wxT("Some text is mixed in bmp shade."); for(int i = , step = ; i < ; i++, step += )
{
dc.DrawText(msg, , step);
}
}

在屏幕上画普通文字和旋转的文字

/***************************************************************
* Name: MyApp.h
* Purpose: Defines Application Class
* Author: PingGe ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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...\tF2"), wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("&Exit...\tAlt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //建立一个菜单条
menuBar->Append(fileMenu, wxT("&File")); //将"文件"菜单加入到菜单条
menuBar->Append(helpMenu, wxT("&Help")); //将“帮助”菜单加入到菜单条 SetMenuBar(menuBar); //将菜单条放到窗体上 CreateStatusBar(); //创建两个状态栏,并写上字符串
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
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 = , h = ; //要绘制的矩形大小 int x = wxMax(, sz.x / ) - w / ; //将矩形放置在窗口的正中间,但不为负
int y = wxMax(, sz.y / ) - h / ; wxRect rectToDraw(x, y, w, h); if(IsExposed(rectToDraw)) //只有在需要的时候才重新画以便提高效率
{
mydc.DrawRectangle(rectToDraw); DrawTextString(mydc, wxT("test"), wxPoint(, )); //画字符串 DrawRotatedTest(mydc, wxT("PingGe"), wxPoint(, ), ); //旋转字符串
}
/*
窗口最大化后出现两个矩形,原因是窗口变大时原先部分没有刷新,
只把新增部分刷新出来;
*/
}
/***************************************************************
* Name: wxPingGe.h
* Purpose: Code for Application Frame
* Author: PingGe ([email protected])
* 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 = ); #endif //_WXPINGGE_H_
/***************************************************************
* Name: wxPingGe.cpp
* Purpose: Code for Application Frame
* Author: PingGe ([email protected])
* 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(, 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(, 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 ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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 ([email protected])
* 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...\tF2"), wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("&Exit...\tAlt-X"), wxT("Quit this programe")); wxMenuBar * menuBar = new wxMenuBar(); //定义菜单条
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help")); SetMenuBar(menuBar); CreateStatusBar(); //创建两个状态栏,并写上字符串
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
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 = , h = ; //要绘制的矩形大小 int x = wxMax(, sz.x / ) - w / ; //将矩形放置在窗口的正中间,但不为负
int y = wxMax(, sz.y / ) - h / ; wxRect rectToDraw(x, y, w, h); if(IsExposed(rectToDraw)) //只有在需要的时候才重新画以便提高效率
{
mydc.DrawRectangle(rectToDraw);
}
/*
窗口最大化后出现两个矩形,原因是窗口变大时原先部分没有刷新,
只把新增部分刷新出来;
*/
}
05-02 11:03