最近要用codeblocks+wxWidgets做一个实验的结果图,采用的版本是codeblocksSVN完美配置绿色版,SVN6852,下载地址http://bt.neu6.edu.cn/viewthread.php?tid=692508&highlight=codeblocks
之所以采用这个版本是因为不需配置,方便快速做完现在手头上的东西。
新建工程,选择wxWidgets Project:
第一步之后是一个欢迎界面,无需理会,next;
弹出窗口提示选择wxWidgets的版本,我下的版本集成了2.8.x,选好后点击next;
设置路径和工程名:
输入作者信息,不理会,next;
选择使用的GUI类型,我做的功能比较简单,直接选None,next
输入wxWidgets的路径,由于利用了绿色版,也无需设置,保留$(#wx)这个全局符号,继续一路next;
最后工程建好,工程列表如下:
替换wxDrawApp.cpp中的内容:
#include "wxDrawApp.h" //(*AppHeaders #include "wxDrawMain.h" #include <wx/image.h> IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Line *line = new Line(wxT("Line")); line->Show(true); return true; }
更改wxDrawMain.cpp中的内容:
#include "wxDrawMain.h" Line::Line(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180)) { this->Connect(wxEVT_PAINT, wxPaintEventHandler(Line::OnPaint)); this->Centre(); } void Line::OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); // 绘制线段 wxCoord x1 = 50, y1 = 60; wxCoord x2 = 190, y2 = 60; dc.DrawLine(x1, y1, x2, y2); }
更改wxDrawApp.h中的内容:
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
更改wxDrawMain.h中的内容:
#include <wx/wx.h> class Line : public wxFrame { public: Line(const wxString& title); void OnPaint(wxPaintEvent& event); };
这些代码任何有过一点MFC、C++经验的人都可以看懂,新建了一个Line类,重载了库中的OnPaint函数,完成绘制一条线段的功能;
效果如下:
OnPaint响应函数中的内容可以进行修改,完成各种复杂的绘制,下面粘贴一点有用的代码实例:
void Line::OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); // 绘制线段 wxCoord x1 = 50, y1 = 60; wxCoord x2 = 190, y2 = 60; dc.DrawLine(x1, y1, x2, y2); // 添加文字 dc.DrawText(wxT("fjsadk"), 40, 60); dc.DrawText(wxT("fwefdecd"), 70, 80); // 点 dc.DrawPoint(10,10); dc.DrawPoint(10,20); dc.DrawPoint(20,10); dc.DrawPoint(20,20); // 画笔 wxColour col1, col2; col1.Set(wxT("#0c0c0c")); col2.Set(wxT("#000000")); wxBrush brush(wxColour(255, 255, 255), wxTRANSPARENT); dc.SetBrush(brush); dc.SetPen(wxPen(col1, 1, wxSOLID)); dc.DrawRectangle(10, 15, 90, 60); dc.SetPen(wxPen(col1, 1, wxDOT)); dc.DrawRectangle(130, 15, 90, 60); dc.SetPen(wxPen(col1, 1, wxLONG_DASH)); dc.DrawRectangle(250, 15, 90, 60); dc.SetPen(wxPen(col1, 1, wxSHORT_DASH)); dc.DrawRectangle(10, 105, 90, 60); dc.SetPen(wxPen(col1, 1, wxDOT_DASH)); dc.DrawRectangle(130, 105, 90, 60); dc.SetPen(wxPen(col1, 1, wxTRANSPARENT)); dc.DrawRectangle(250, 105, 90, 60); // 绘制图形 wxColour gray, white, red, blue; wxColour orange, green, brown; gray.Set(wxT("#d4d4d4")); white.Set(wxT("#ffffff")); red.Set(wxT("#ff0000")); orange.Set(wxT("#fa8e00")); green.Set(wxT("#619e1b")); brown.Set(wxT("#715b33")); blue.Set(wxT("#0d0060")); dc.SetPen(wxPen(gray)); dc.DrawRectangle(20, 20, 50, 50); dc.DrawRectangle(30, 40, 50, 50); dc.SetBrush(wxBrush(white)); dc.DrawRectangle(100, 20, 50, 50); dc.DrawRectangle(110, 40, 50, 50); wxRegion region1(100, 20, 50, 50); wxRegion region2(110, 40, 50, 50); region1.Intersect(region2); wxRect rect1 = region1.GetBox(); dc.SetClippingRegion(region1); dc.SetBrush(wxBrush(red)); dc.DrawRectangle(rect1); dc.DestroyClippingRegion(); dc.SetBrush(wxBrush(white)); dc.DrawRectangle(180, 20, 50, 50); dc.DrawRectangle(190, 40, 50, 50); wxRegion region3(180, 20, 50, 50); wxRegion region4(190, 40, 50, 50); region3.Union(region4); dc.SetClippingRegion(region3); wxRect rect2 = region3.GetBox(); dc.SetBrush(wxBrush(orange)); dc.DrawRectangle(rect2); dc.DestroyClippingRegion(); dc.SetBrush(wxBrush(white)); dc.DrawRectangle(20, 120, 50, 50); dc.DrawRectangle(30, 140, 50, 50); wxRegion region5(20, 120, 50, 50); wxRegion region6(30, 140, 50, 50); region5.Xor(region6); wxRect rect3 = region5.GetBox(); dc.SetClippingRegion(region5); dc.SetBrush(wxBrush(green)); dc.DrawRectangle(rect3); dc.DestroyClippingRegion(); dc.SetBrush(wxBrush(white)); dc.DrawRectangle(100, 120, 50, 50); dc.DrawRectangle(110, 140, 50, 50); wxRegion region7(100, 120, 50, 50); wxRegion region8(110, 140, 50, 50); region7.Subtract(region8); wxRect rect4 = region7.GetBox(); dc.SetClippingRegion(region7); dc.SetBrush(wxBrush(brown)); dc.DrawRectangle(rect4); dc.DestroyClippingRegion(); dc.SetBrush(white); dc.DrawRectangle(180, 120, 50, 50); dc.DrawRectangle(190, 140, 50, 50); wxRegion region9(180, 120, 50, 50); wxRegion region10(190, 140, 50, 50); region10.Subtract(region9); wxRect rect5 = region10.GetBox(); dc.SetClippingRegion(region10); dc.SetBrush(wxBrush(blue)); dc.DrawRectangle(rect5); dc.DestroyClippingRegion(); }
from:http://www.cnblogs.com/ziqiao/archive/2011/11/14/2248268.html