如何在wxStatusBar里面添加进度条-分享
wxwidgets吧
全部回复
仅看楼主
level 3
sssky307 楼主
默认wxStatusBar只能显示一点文字信息,不能放置button,进度条之类的小控件。
本人尝试实现了一把。
方法是:
1,继承wxStatusBar。
2,在构造函数定义进度条。
3,设定OnSize event。
实现Frame如下,手动放到wxApp下实例化就能运行。
核心代码在第二楼
2016年04月20日 01点04分 1
level 3
sssky307 楼主
//simple.h
#pragma once
#include<wx/wx.h>
class Simple:public wxFrame
{
public:
Simple(const wxString& title);
};
//simple.cpp
#include "Simple.h"
#include"MyStatusBar.h"
Simple::Simple(const wxString& title) :wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(450,300))
{
MyStatusBar* statusBar = new MyStatusBar(this, wxID_ANY);
statusBar->setGaugeValue(10);
this->SetStatusBar(statusBar);
this->Centre();
}
//myStatusBar.h
#pragma once
#include<wx/wx.h>
class MyStatusBar:public wxStatusBar
{
public:
MyStatusBar(wxWindow *parent,wxWindowID id = wxID_ANY,long style = wxSTB_DEFAULT_STYLE,const wxString& name = wxStatusBarNameStr);
void setGaugeValue(int n);
int getGaugeValue();
private:
void OnSize(wxSizeEvent& event);
wxGauge* gauge;
int gaugeRange = 100;
};
//myStatusBar.cpp
#include "MyStatusBar.h"
MyStatusBar::MyStatusBar(wxWindow *parent, wxWindowID id /*= wxID_ANY*/, long style /*= wxSTB_DEFAULT_STYLE*/, const wxString& name /*= wxStatusBarNameStr*/)
:wxStatusBar(parent,id,style,name)
{
this->SetFieldsCount(3);
int patch[3] = { -1, -1,100 };//窗口将分成三段:其中第3段是固定宽度100pix,第1段和第2段动态1:1比例分配宽度。
this->SetStatusWidths(3, patch);
this->gauge = new wxGauge(this, -1, gaugeRange);
this->Bind(wxEVT_SIZE, &MyStatusBar::OnSize, this);
}
void MyStatusBar::setGaugeValue(int n)
{
if ((n >= 0 && n <= gaugeRange))
{
this->gauge->SetValue(n);
}
}
int MyStatusBar::getGaugeValue()
{
return this->gauge->GetValue();
}
void MyStatusBar::OnSize(wxSizeEvent& event)
{
wxRect rect;
this->GetFieldRect(2, rect);//获取第3段的位置信息;
this->gauge->SetPosition(wxPoint(rect.x + 1, rect.y + 1));
this->gauge->SetSize(wxSize(rect.width - 4, rect.height - 4));
}
2016年04月20日 01点04分 2
level 3
sssky307 楼主
有一点不放心gauge是不是new在对象树上,最后还是保险一点补充析构函数,防止内存leak
MyStatusBar::~MyStatusBar()
{
if (this->gauge)
{
delete gauge;
gauge = 0;
}
}
2016年04月20日 01点04分 3
level 2
我粘到VS试试~
2016年04月21日 12点04分 4
level 2
int gaugeRange = 100;为毛能直接初始化?编译器直接提示你不能直接这么做,你得放到构造函数里面!
2016年04月21日 12点04分 5
哈哈哈,你孤陋寡闻啦,c++11新规范有说明。这种类中的普通变量可以就地初始化,你用vs2013以上版本就没问题。
2016年04月21日 14点04分
@sssky307 优先级是:构造初始化列表->构造函数的函数体->最后是你表示惊讶的用法。
2016年04月21日 14点04分
@sssky307 规则越多,越容易出bug[汗]
2016年04月22日 14点04分
level 2
百度什么时候要能提供粘代码的地方就好了
2016年04月21日 14点04分 6
1