wxWidget入门(二)

接着上一期的内容。如何在窗口上打出"helloworld"呢?

引入一个wxStaticText,顾名思义静态文本框。并且把文本框“附着”当前窗口上。

MyFrame::MyFrame(const wxString & title)
	:wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250,150))
{
	//Centre();//将窗口放置在显示屏正中心
	wxStaticText *text = new wxStaticText(this,wxID_ANY,wxT("HelloWorld!!!"));
}

设置窗口图标需要xpm格式文件(不知道的可自行百度)

操作步骤:

  1. 图片转成xpm格式文件
  2. #include "icon.xpm"
    MyFrame::MyFrame(const wxString & title)
    	:wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250,150))
    {
    	SetIcon(wxIcon(icon_xpm));
    }
  3. icon_xpm 是静态数组名可以在导入的icon.xpm文件中找到(修改为指定名)

          

接着今天的任务

讲一下按钮控件wxButton

同理都是在MyFrame类中添加上你想要的控件,他的名字叫"hit me"

MyFrame::MyFrame(const wxString & title)
	:wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250,150))
{
    wxButton *button = new wxButton(this,wxID_ANY, wxT("hit me"));
	    button->SetPosition(wxPoint(20,20));
}

这里讨论问题,不对按钮进行初始化

wxButton *button = new wxButton();

之后要怎么显示出来。

如果原先有学过qt的可能会这样写

wxButton *button = new wxButton();
button->SetParent(this);
button->SetId(wxID_ANY);
button->SetLabelText(wxT("hit me"));
button->SetPosition(wxPoint(20,20));
button->Show(true);

但是你会发现根本没有按钮。我也试了挺久可能他的机制问题不是这样写。

这里附上正确的写法

wxButton *button = new wxButton();
button->Create(this,wxID_ANY);
button->SetPosition(wxPoint(20,20));

注意点是setPosition要放在create后面否则效果会被覆盖

好了言归正传。。。

你会发现点击没有任何反应。

这里引入一个事件处理机制,其实有挺多种方法的,这里我就讲一种我自己喜欢的

与QT信号和槽函数类似

class MyButton : public wxButton
{
public:
	MyButton(wxWindow *parent, wxWindowID id, const wxString & title);
	void MyHandle(wxCommandEvent &event);

};
MyButton * mybutton = new MyButton(this,wxID_ANY,wxT("hit me"));
mybutton->SetSize(50,30);
mybutton->SetPosition(wxPoint(50,50));
mybutton->Connect(mybutton->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(MyButton::MyHandle));

上图中自定义了一个函数MyHandle,当我点击按钮(触发wxEVT_COMMAND_BUTTON_CLICKED)

值得注意两个点:

  1. Connect函数(与QT不同,不需要制定谁Connect),但是wx中要说明是谁连接的
  2. 连接者是有编号的,用mybutton->GetId()获得由系统自动分配的Id(wxID_ANY的作用)。

嗯贴出完整代码了

#include <wx/wx.h>
#include "icon.xpm"


class MyFrame : public wxFrame
{
public:
	MyFrame(const wxString & title);
};

class MyApp : public wxApp
{
public:
	virtual bool OnInit();
};

class MyButton : public wxButton
{
public:
	MyButton(wxWindow	*parent,wxWindowID	id, const wxString & title);
	void MyHandle(wxCommandEvent &event);

};
MyButton::MyButton(wxWindow	*parent,wxWindowID	id,const wxString & title):
	wxButton(parent,id,title)
{}

void MyButton::MyHandle(wxCommandEvent & event)
{
	//wxMessageBox(wxT("自定义消息"));    // 弹出提示消息框
	static int count = 1;
	if(count % 2)//基数显示"hit me" 偶数显示 "god"
		this->SetLabelText(wxT("god"));
	else
		this->SetLabelText(wxT("hit me"));
	count++;
}

MyFrame::MyFrame(const wxString & title)
	:wxFrame(NULL, wxID_ANY, title, wxDefaultPosition/*, wxSize(250,150)*/)
{
	//Centre();//将窗口放置在显示屏正中心
	SetIcon(wxIcon(icon_xpm));
	SetSize(250,150);
	wxStaticText *text = new wxStaticText(this,wxID_ANY,wxT("HelloWorld!!!"));
	

	MyButton * mybutton = new MyButton(this,wxID_ANY,wxT("hit me"));
	mybutton->SetSize(50,30);
	mybutton->SetPosition(wxPoint(50,50));
	mybutton->Connect(mybutton->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(MyButton::MyHandle));
}

bool MyApp::OnInit()
{
	MyFrame *frame = new MyFrame(wxT("MyApp"));
	frame->Show(true);
	return true;
}

// 核心代码
DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp);

这次讲的还是依旧很简单哈。大家可以去试一试,想一想有什么有趣的按钮动作,自定义函数实现把

猜你喜欢

转载自blog.csdn.net/u012198575/article/details/82462864