Q_Jizi(七之子)的专栏

计算机视觉相关博客:http://cvart.blog.163.com/

ubuntu下codeblocks起步(七)-中

打开drawingMain.h文件,向drawingFrame类中添加如下成员变量:

private:

wxString m_UserColor; //记录所选择的画笔和画刷的颜色

wxString m_UserShape; //记录所选择的要画的图形


/*下面四个变量,在画直线时:m_SX和m_SY分别用于保存直线的起始坐标的x,y值,m_EX和m_EY用于保存直线的结束坐标值;在画椭圆时: m_SX和 m_SY用于保存椭圆的中心点坐标, m_EX和 m_EY分别用于保存椭圆的长轴和短轴;在画圆时: m_SX和 m_SY用于保存圆心坐标, m_EX保存半径。*/

int m_EY;

int m_EX;

int m_SY;

int m_SX;


wxPen pen; //画笔

wxBrush brush; //画刷


//下面两个变量用于将输入的wxString型数据转换为int型的

wxString str;

double temp;

头文件要#include <wx/brush.h>


打开MyDialog.h文件,修改MyDialog类如下(红色为修改的内容):

class MyDialog: public wxDialog

{

public:


MyDialog(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);

virtual ~MyDialog();


//(*Identifiers(MyDialog)

static const long ID_STATICTEXT1;

static const long ID_TEXTCTRL1;

static const long ID_TEXTCTRL2;

static const long ID_STATICTEXT2;

static const long ID_TEXTCTRL3;

static const long ID_TEXTCTRL4;

static const long ID_BUTTON1;

static const long ID_BUTTON2;

//*)


protected:


//(*Handlers(MyDialog)

void OnChanlClick(wxCommandEvent& event);

void OnOkClick(wxCommandEvent& event);

//*)

public:

//(*Declarations(MyDialog)

wxStaticText* StaticText1;

wxTextCtrl* m_StartX;

wxTextCtrl* m_StartY;

wxStaticText* StaticText2;

wxTextCtrl* m_EndX;

wxTextCtrl* m_EndY;

wxButton* Button1;

wxButton* Button2;

//*)

public:

bool IfOk;


DECLARE_EVENT_TABLE()

};

将wxTextCtrl设置为public类型,便于Frame访问;变量IfOk用于记录是否选择了此窗体,即是否选择画这种图形。


打开RangeDialog.h和CircleDialog.h,设置如上面的MyDialog.h

打开MyDialog窗体,为“退出”和“确定”按钮在MyDialog.cpp文件中添加响应事件:

void MyDialog::OnChanlClick(wxCommandEvent& event)

{

Close();

}


void MyDialog::OnOkClick(wxCommandEvent& event)

{

IfOk = TRUE;

Close();

}


RangeDialog和CircleDialog窗体做同样的操作。


打开drawingMain.cpp,在Frame类的构造函数中对drawingFrame类的新加成员变量进行初始化:

m_UserColor = _T("");

m_UserShape = _T("");

str = _T("");

temp = 0.0;


打开drawingFrame主窗体,为窗体的“颜色”和“画图”的子菜单选项添加响应函数:打开C::B左边management工作区的Resource选项卡,依次打开列表,找到“颜色”和“画图”菜单的子选项。

分别为子选项添加响应函数

为“颜色”菜单的子选项添加的函数如下:

void drawingFrame::OnRedSelected(wxCommandEvent& event)

{

m_UserColor = _T("红色"); //记录选中的为“红色”

pen = *wxRED_PEN; //定义红色画笔

brush = *wxRED_BRUSH; //定义红色画刷


Refresh(); //刷新窗体,实际是一个重构窗体的过程,在

Update(); //重构过程中重新调用OnPaint函数。

}


void drawingFrame::OnYelloSelected(wxCommandEvent& event)

{

m_UserColor = _T("灰色");

pen = *wxGREY_PEN;

brush = *wxGREY_BRUSH;


Refresh();

Update();

}


void drawingFrame::OnGreenSelected(wxCommandEvent& event)

{

m_UserColor = _T("绿色");

pen = *wxGREEN_PEN;

brush = *wxGREEN_BRUSH;


Refresh();

Update();

}


void drawingFrame::OnBlueSelected(wxCommandEvent& event)

{

m_UserColor = _T("黑色");

pen = *wxBLACK_PEN;

brush = *wxBLACK_BRUSH;


Refresh();

Update();

}

为“颜色”菜单的子选项添加的函数如下:

void drawingFrame::OnLineSelected(wxCommandEvent& event)

{

m_UserShape = _T("直线"); //记录选择的图形为直线

MyDialog LineDialog(this); //创建MyDialog类的对象

LineDialog.ShowModal(); //显示子窗体

//将子窗体中输入的内容转换为int型,具体方法在文章最后有一个讲解

str = (LineDialog.m_StartX)->GetValue();

str.ToDouble(&temp);

m_SX = int(temp);

str = (LineDialog.m_StartY)->GetValue();

str.ToDouble(&temp);

m_SY = int(temp);

str = (LineDialog.m_EndX)->GetValue();

str.ToDouble(&temp);

m_EX = int(temp);

str = (LineDialog.m_EndY)->GetValue();

str.ToDouble(&temp);

m_EY = int(temp);

if(LineDialog.IfOk == TRUE) //判断是否选择了直线

{

Refresh();

Update();

}

}


void drawingFrame::OnCircleSelected(wxCommandEvent& event)

{

m_UserShape = _T("圆");

CircleDialog MyCircleDialog(this);

MyCircleDialog.ShowModal();


str = (MyCircleDialog.TextCtrl1)->GetValue();

str.ToDouble(&temp);

m_SX = int(temp);

str = (MyCircleDialog.TextCtrl2)->GetValue();

str.ToDouble(&temp);

m_SY = int(temp);

str = (MyCircleDialog.TextCtrl3)->GetValue();

str.ToDouble(&temp);

m_EX = int(temp);

if(MyCircleDialog.IfOk == TRUE)

{

Refresh();

Update();

}

}


void drawingFrame::OnRangeSelected(wxCommandEvent& event)

{

m_UserShape = _T("椭圆");

RangeDialog MyRangeDialog(this);

MyRangeDialog.ShowModal();


str = (MyRangeDialog.TextCtrl1)->GetValue();

str.ToDouble(&temp);

m_SX = int(temp);

str = (MyRangeDialog.TextCtrl2)->GetValue();

str.ToDouble(&temp);

m_SY = int(temp);

str = (MyRangeDialog.TextCtrl3)->GetValue();

str.ToDouble(&temp);

m_EX = int(temp);

str = (MyRangeDialog.TextCtrl4)->GetValue();

str.ToDouble(&temp);

m_EY = int(temp);


if(MyRangeDialog.IfOk == TRUE)

{

Refresh();

Update();

}

}


void drawingFrame::OnSempleSelected(wxCommandEvent& event)

{

m_UserShape = _T("图形实例");

Refresh();

Update();

}

 
阅读更多
想对作者说点什么? 我来说一句

codeblocks下图形编程起步

2013年05月07日 3.35MB 下载

没有更多推荐了,返回首页

不良信息举报

ubuntu下codeblocks起步(七)-中

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭