服务器之家

服务器之家 > 正文

wxWidgets自定义按钮的方法

时间:2021-07-22 17:00     来源/作者:infoworld

场景:

1.现在的软件上的按钮都不是标准的按钮了,因为基本上是贴图上去的,正常情况下一种图片,鼠标移上去之后按钮显示另一种效果,按下去之后又是另一种效果。

2.wx的做法其实和mfc的按钮原理是一样的,就是给按钮贴图和重绘背景。

以下是源文件.

dh_bitmap_button.h

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * File:  dh_bitmap_button.h
 * Author: Sai
 *
 * Created on 2009年12月29日, 下午4:08
 */
 
#ifndef _DH_BITMAP_BUTTON_H
#define _DH_BITMAP_BUTTON_H
 
#include "wx/wx.h"
 
enum DhBitmapButtonStatus
{
  kDhBitmapButtonNormal,
  kDhBitmapButtonEnter,
  kDhBitmapButtonDown,
  kDhBitmapButtonUp,
  kDhBitmapButtonLeave,
  kDhBitmapButtonDClick,
  kDhBitmapButtonDisable
};
 
class DhBitmapButton : public wxControl
{
  DECLARE_DYNAMIC_CLASS(DhBitmapButton)
  DECLARE_EVENT_TABLE()
public:
  DhBitmapButton();
  virtual ~DhBitmapButton();
 
  DhBitmapButton(wxWindow* parent, wxWindowID id,
      const wxPoint& pos = wxDefaultPosition,
      const wxSize& size = wxDefaultSize,
      long style = wxBORDER_NONE,
      const wxValidator& validator = wxDefaultValidator);
  bool Create(wxWindow* parent, wxWindowID id,
      const wxPoint& pos = wxDefaultPosition,
      const wxSize& size = wxDefaultSize,
      long style = wxSUNKEN_BORDER,
      const wxValidator& validator = wxDefaultValidator);
 
  wxSize DoGetBestSize() const;
  void OnPaint(wxPaintEvent& event);
  virtual void OnEnter(wxMouseEvent& event);
  virtual void OnLeave(wxMouseEvent& event);
  virtual void OnDown(wxMouseEvent& event);
  virtual void OnDClick(wxMouseEvent& event);
  virtual void OnUp(wxMouseEvent& event);
  virtual bool Enable(bool enable = true);
  virtual bool Disable();
  /**
   * 设置正常图片
   *
   * @param bitmap
   */
  DhBitmapButton* set_normal_bitmap(wxBitmap* bitmap);
 
  /**
   * 1.设置按钮按下时的切换图片
   */
  DhBitmapButton* set_down_bitmap(wxBitmap* bitmap);
 
  /**
   * 1.设置按钮按经过时的切换图片
   */
  DhBitmapButton* set_enter_bitmap(wxBitmap* bitmap);
 
  /**
   * 1.设置Disable图片.
   *
   * @param bitmap
   * @return this
   */
  DhBitmapButton* set_disable_bitmap(wxBitmap* bitmap);
 
  DhBitmapButton* set_background(const wxBitmap& bitmap);
 
  bool SetBackgroundColour(const wxColour& colour);
 
protected:
  void DrawExistBitmap(wxDC* dc,wxBitmap* image1,wxBitmap* exist_image);
 
private:
  wxBitmap background_;
  bool is_used_bg_;
 
  wxBitmap* normal_bitmap_;
  wxBitmap* down_bitmap_;
  wxBitmap* enter_bitmap_;
  wxBitmap* disable_bitmap_;
 
  int button_status_;
  wxString text_;
  wxFont text_font_;
 
  void DrawBackground(wxDC* dc);
 
};
#endif /* _DH_BITMAP_BUTTON_H */

dh_bitmap_button.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * File:  DhBitmapButton.cpp
 * Author: Sai
 *
 * Created on 2009年12月29日, 下午4:08
 */
 
#include "dh_bitmap_button.h"
 
BEGIN_EVENT_TABLE(DhBitmapButton, wxControl)
EVT_PAINT(DhBitmapButton::OnPaint)
EVT_ENTER_WINDOW(DhBitmapButton::OnEnter)
EVT_LEAVE_WINDOW(DhBitmapButton::OnLeave)
EVT_LEFT_DOWN(DhBitmapButton::OnDown)
EVT_LEFT_DCLICK(DhBitmapButton::OnDClick)
EVT_LEFT_UP(DhBitmapButton::OnUp)
END_EVENT_TABLE()
 
IMPLEMENT_DYNAMIC_CLASS(DhBitmapButton, wxControl)
 
DhBitmapButton::DhBitmapButton()
{
}
 
DhBitmapButton::DhBitmapButton(wxWindow* parent, wxWindowID id,
    const wxPoint& pos,
    const wxSize& size,
    long style,
    const wxValidator& validator)
: normal_bitmap_(NULL), down_bitmap_(NULL), enter_bitmap_(NULL)
{
  Create(parent, id, pos, size, style, validator);
}
 
DhBitmapButton::~DhBitmapButton()
{
  wxDELETE(normal_bitmap_);
  wxDELETE(enter_bitmap_);
  wxDELETE(down_bitmap_);
}
 
bool DhBitmapButton::Create(wxWindow* parent, wxWindowID id,
    const wxPoint& pos,
    const wxSize& size,
    long style,
    const wxValidator& validator)
{
  normal_bitmap_ = NULL;
  down_bitmap_ = NULL;
  enter_bitmap_ = NULL;
  disable_bitmap_ = NULL;
 
  if (!wxControl::Create(parent, id, pos, size, style, validator))
  {
    return false;
  }
  SetBackgroundStyle(wxBG_STYLE_PAINT);
  is_used_bg_ = false;
  return true;
}
 
wxSize DhBitmapButton::DoGetBestSize() const
{
  return GetSize();
}
 
void DhBitmapButton::DrawExistBitmap(wxDC* dc, wxBitmap* image1,
    wxBitmap* exist_image)
{
  if (image1)
  {
    dc->DrawBitmap(*image1, 0, 0, true);
 
  } else
  {
    dc->DrawBitmap(*exist_image, 0, 0, true);
  }
}
 
void DhBitmapButton::OnPaint(wxPaintEvent& event)
{
  wxPaintDC dc(this);
  DrawBackground(&dc);
  //1.状态控制绘画,好处就是可以调用Refresh连背景一起刷新.
  switch (button_status_)
  {
    case kDhBitmapButtonNormal:
      dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
      break;
    case kDhBitmapButtonEnter:
      if (!enter_bitmap_)
      {
        int width = DoGetBestSize().GetWidth();
        int height = DoGetBestSize().GetHeight();
        wxClientDC dc(this);
        dc.SetPen(*wxRED_PEN);
        dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
        dc.DrawRectangle(0, 0, width, height);
        break;
      }
      dc.DrawBitmap(*enter_bitmap_, 0, 0, true);
      break;
    case kDhBitmapButtonDown:
      DrawExistBitmap(&dc,down_bitmap_,normal_bitmap_);
      break;
    case kDhBitmapButtonUp:
     dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
      break;
    case kDhBitmapButtonLeave:
      dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
      break;
    case kDhBitmapButtonDClick:
      DrawExistBitmap(&dc,down_bitmap_,normal_bitmap_);
      break;
    case kDhBitmapButtonDisable:
      DrawExistBitmap(&dc,disable_bitmap_,normal_bitmap_);
      break;
    default:
      dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
      break;
  }
 
}
 
void DhBitmapButton::DrawBackground(wxDC* dc)
{
  if (is_used_bg_)
  {
    dc->DrawBitmap(background_, 0, 0, true);
  } else
  {
    wxBrush brush(GetBackgroundColour());
    wxPen pen(GetBackgroundColour());
    dc->SetBrush(brush);
    dc->SetPen(pen);
    dc->DrawRectangle(0, 0, GetSize().x, GetSize().y);
  }
}
 
void DhBitmapButton::OnEnter(wxMouseEvent& event)
{
  button_status_ = kDhBitmapButtonEnter;
  Refresh();
  Update();
}
 
void DhBitmapButton::OnLeave(wxMouseEvent& event)
{
  if (!IsEnabled())
  {
    return;
  }
  button_status_ = kDhBitmapButtonLeave;
  Refresh();
  Update();
}
 
void DhBitmapButton::OnDClick(wxMouseEvent& event)
{
  button_status_ = kDhBitmapButtonDown;
  Refresh(false);
  Update();
}
 
void DhBitmapButton::OnDown(wxMouseEvent& event)
{
  button_status_ = kDhBitmapButtonDown;
  Refresh();
  Update();
}
 
void DhBitmapButton::OnUp(wxMouseEvent& event)
{
  if (kDhBitmapButtonDown != button_status_)
  {
    return;
  }
  button_status_ = kDhBitmapButtonUp;
  Refresh();
  Update();
  wxCommandEvent myEvent(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
  myEvent.SetEventObject(this);
  GetEventHandler()->ProcessEvent(myEvent);
}
 
DhBitmapButton* DhBitmapButton::set_normal_bitmap(wxBitmap* bitmap)
{
  normal_bitmap_ = bitmap;
  return this;
}
 
DhBitmapButton* DhBitmapButton::set_down_bitmap(wxBitmap* bitmap)
{
  down_bitmap_ = bitmap;
  return this;
}
 
DhBitmapButton* DhBitmapButton::set_enter_bitmap(wxBitmap* bitmap)
{
  enter_bitmap_ = bitmap;
  return this;
}
 
bool DhBitmapButton::Enable(bool enable)
{
  if (enable)
  {
    button_status_ = kDhBitmapButtonNormal;
  } else
  {
    button_status_ = kDhBitmapButtonDisable;
  }
 
  Refresh(false);
  Update();
  return wxControl::Enable(enable);
}
 
bool DhBitmapButton::Disable()
{
  return Enable(false);
}
 
DhBitmapButton* DhBitmapButton::set_disable_bitmap(wxBitmap* bitmap)
{
  disable_bitmap_ = bitmap;
  return this;
}
 
DhBitmapButton* DhBitmapButton::set_background(const wxBitmap& bitmap)
{
  is_used_bg_ = true;
  background_ = bitmap;
  return this;
}
 
bool DhBitmapButton::SetBackgroundColour(const wxColour& colour)
{
  is_used_bg_ = false;
  return wxControl::SetBackgroundColour(colour);
}

调用方式和wxButton一样.

1.先注册事件映射宏.

?
1
EVT_BUTTON(Minimal_StartSimplePopup, MyFrame::OnStartSimplePopup)

2.调用代码.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
wxBitmap* normal = new wxBitmap("./resources/start/start_normal.png",
    wxBITMAP_TYPE_PNG);
  wxBitmap* down = new wxBitmap("./resources/start/start_pressed.png",
    wxBITMAP_TYPE_PNG);
  wxBitmap* enter = new wxBitmap("./resources/start/start_current.png",
    wxBITMAP_TYPE_PNG);
  wxBitmap* disable = new wxBitmap("./resources/start/stop_normal.png",
    wxBITMAP_TYPE_PNG);
  wxBitmap bg = GetPositionBackgroundBitmap(0,0,normal->GetWidth(),normal->GetHeight());
  start = new DhBitmapButton(page,Minimal_StartSimplePopup,wxPoint(0,0),
    normal->GetSize());
  start->set_normal_bitmap(normal)->set_down_bitmap(down)->set_enter_bitmap(enter);
  start->set_disable_bitmap(disable);
  start->set_background(bg);
 
 
wxBitmap MyFrame::GetPositionBackgroundBitmap(int x, int y,
    int width,int height)
{
  wxRect rect;
  rect.x = x;
  rect.y = y;
  rect.width = width;
  rect.height = height;
  wxBitmap temp = this->bg.GetSubBitmap(rect);
  return temp;
}

3.当然我觉得有更好的方式.

比如重载这个类.wxBit

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/infoworld/article/details/10830237

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部