服务器之家

服务器之家 > 正文

wxWidgets实现图片和文件按钮

时间:2021-07-22 16:59     来源/作者:hellokandy

在wxWidgets中,想要通过其本身的控件来实现图片+文件的按钮,貌似不太容易做到。但是可以通过重载wxControl来自绘图片+文件按钮。下面给出的是已经封装好的按钮类:

wxBitmapButtonEx.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
#ifndef _BITMAP_BUTTON_EX_H
#define _BITMAP_BUTTON_EX_H
 
#include "wx/wx.h"
 
enum eBitmapButtonStatus
{
 BitmapButtonNormal,
 BitmapButtonEnter,
 BitmapButtonDown,
 BitmapButtonUp,
 BitmapButtonLeave,
 BitmapButtonDClick,
 BitmapButtonDisable
};
 
class wxBitmapButtonEx : public wxControl
{
 DECLARE_DYNAMIC_CLASS(wxBitmapButtonEx)
 DECLARE_EVENT_TABLE()
public:
 wxBitmapButtonEx();
 virtual ~wxBitmapButtonEx();
 
 wxBitmapButtonEx(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();
 
 //设置正常图片
 wxBitmapButtonEx* set_normal_bitmap(wxBitmap* bitmap);
 
 //设置按钮按下时的切换图片
 wxBitmapButtonEx* set_down_bitmap(wxBitmap* bitmap);
 
 //设置按钮按经过时的切换图片
 wxBitmapButtonEx* set_enter_bitmap(wxBitmap* bitmap);
 
 //设置Disable图片.
 wxBitmapButtonEx* set_disable_bitmap(wxBitmap* bitmap);
 
 //设置背景图片
 wxBitmapButtonEx* set_background(const wxBitmap& bitmap);
 
 //设置背景颜色
 bool set_background_colour(const wxColour& colour);
 
 //设置按钮文字
 void set_button_text(wxString str);
 
 //设置文字字体
 void set_text_font(wxFont font);
 
protected:
 void DrawExistBitmap(wxDC* dc,wxBitmap* image1,wxBitmap* exist_image);
 
private:
 wxBitmap background_bitmap;
 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 /* _BITMAP_BUTTON_EX_H */

wxBitmapButtonEx.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "wxBitmapButtonEx.h"
#include <wx/settings.h>
 
BEGIN_EVENT_TABLE(wxBitmapButtonEx, wxControl)
EVT_PAINT(wxBitmapButtonEx::OnPaint)
EVT_ENTER_WINDOW(wxBitmapButtonEx::OnEnter)
EVT_LEAVE_WINDOW(wxBitmapButtonEx::OnLeave)
EVT_LEFT_DOWN(wxBitmapButtonEx::OnDown)
EVT_LEFT_DCLICK(wxBitmapButtonEx::OnDClick)
EVT_LEFT_UP(wxBitmapButtonEx::OnUp)
END_EVENT_TABLE()
 
IMPLEMENT_DYNAMIC_CLASS(wxBitmapButtonEx, wxControl)
 
wxBitmapButtonEx::wxBitmapButtonEx()
{
}
 
wxBitmapButtonEx::wxBitmapButtonEx(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);
}
 
wxBitmapButtonEx::~wxBitmapButtonEx()
{
 wxDELETE(normal_bitmap);
 wxDELETE(enter_bitmap);
 wxDELETE(down_bitmap);
}
 
bool wxBitmapButtonEx::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;
 button_status = BitmapButtonNormal;
 
 if (!wxControl::Create(parent, id, pos, size, style, validator))
 {
  return false;
 }
 SetBackgroundStyle(wxBG_STYLE_SYSTEM);
 is_used_bg = false;
 text_font.Create(12,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("System"),wxFONTENCODING_DEFAULT);
 
 return true;
}
 
wxSize wxBitmapButtonEx::DoGetBestSize() const
{
 return GetSize();
}
 
void wxBitmapButtonEx::DrawExistBitmap(wxDC* dc, wxBitmap* image1,
  wxBitmap* exist_image)
{
 int width = DoGetBestSize().GetWidth();
 int height = DoGetBestSize().GetHeight();
 //wxClientDC dc(this);
 dc->SetPen(*wxBLACK_PEN);
 wxColour clr(2,18,245);
 dc->SetTextForeground(*wxWHITE);
 //dc->SetBrush(*wxTRANSPARENT_BRUSH);
 //dc->SetBrush(*wxBLUE_BRUSH);
 //wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION)
 wxBrush brush(clr);//
 dc->SetBrush(brush);
 
 dc->DrawRectangle(0, 0, width, height);
 /*wxRect rc = wxRect(0, 0, width, height);
 dc->DrawRoundedRectangle(rc,5);*/
 
 int x = normal_bitmap->GetWidth();
 int y = (height-normal_bitmap->GetHeight())/2.0;
 
 if (image1)
 {
 dc->DrawBitmap(*image1, width-x-5, y, true);
  //dc->DrawBitmap(*image1, 0, 0, true);
 
 } else
 {
 dc->DrawBitmap(*exist_image, width-x-5, y, true);
  //dc->DrawBitmap(*exist_image, 0, 0, true);
 }
 
 if (text.IsEmpty())
 {
 return;
 }
 
 if (!text_font.IsNull())
 {
 dc->SetFont(text_font);
 }
 int pointsize = text_font.GetPointSize();
 
 //dc.SetLayoutDirection(wxLayout_Default);
 
 int bit = text.Find('\n');
 if (bit!=-1)
 {
 wxString str;
 str = text.Left(bit);
 dc->DrawText(str,5,height/2-pointsize-2);
 
 int length = text.Length();
 str = text.Mid(bit+1,length-bit-1);
 dc->DrawText(str,5,height/2);
 }
 else
 {
 dc->DrawText(text,5,height/2-7);
 }
}
 
void wxBitmapButtonEx::OnPaint(wxPaintEvent& event)
{
 wxPaintDC dc(this);
 DrawBackground(&dc);
 
 //1.状态控制绘画,好处就是可以调用Refresh连背景一起刷新.
 switch (button_status)
 {
  case BitmapButtonNormal:
 case BitmapButtonUp:
 case BitmapButtonLeave:
 {
 int width = DoGetBestSize().GetWidth();
 int height = DoGetBestSize().GetHeight();
 wxClientDC dc(this);
 dc.SetPen(*wxGREY_PEN);
 dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
 dc.DrawRectangle(0, 0, width, height);
 /*wxRect rc = wxRect(0, 0, width, height);
 dc.DrawRoundedRectangle(rc,5);*/
 
 wxPoint pt[3];
 pt[0].x = 1;
 pt[0].y = height-1;
 pt[1].x = pt[1].y = 1;
 pt[2].x = width-1;
 pt[2].y = 1;
 dc.SetPen(*wxWHITE_PEN);
 dc.DrawLines(3,pt);
 
 pt[1].x = width-1;
 pt[1].y = height-1;
 dc.SetPen(*wxBLACK_PEN);
 dc.DrawLines(3,pt);
 
 int x = normal_bitmap->GetWidth();
 int y = (height-normal_bitmap->GetHeight())/2.0;
 dc.DrawBitmap(*normal_bitmap, width-x-5, y, true);
 
 if (!text_font.IsNull())
 {
  dc.SetFont(text_font); 
 }
 int pointsize = text_font.GetPointSize();
 //dc.SetLayoutDirection(wxLayout_Default);
 
 int bit = text.Find('\n');
 if (bit!=-1)
 {
  wxString str;
  str = text.Left(bit);
  dc.DrawText(str,5,height/2-pointsize-2);
 
  int length = text.Length();
  str = text.Mid(bit+1,length-bit-1);
  dc.DrawText(str,5,height/2);
 }
 else
 {
  dc.DrawText(text,5,height/2-7);
 }
 }
 //dc.DrawBitmap(*normal_bitmap, 0, 0, true);
   break;
  case BitmapButtonEnter:
 //  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);
 //wxRect rc = wxRect(0, 0, width, height);
 //dc.DrawRoundedRectangle(rc,5);
 //
 //   break;
 //  }
 //  dc.DrawBitmap(*enter_bitmap_, 0, 0, true);
   break;
  case BitmapButtonDown:
   DrawExistBitmap(&dc,down_bitmap,normal_bitmap);
   break;
  case BitmapButtonDClick:
   //DrawExistBitmap(&dc,down_bitmap,normal_bitmap);
   break;
  case BitmapButtonDisable:
   //DrawExistBitmap(&dc,disable_bitmap,normal_bitmap);
   break;
  default:
 {
 int width = DoGetBestSize().GetWidth();
 int height = DoGetBestSize().GetHeight();
 wxClientDC dc(this);
 dc.SetPen(*wxGREY_PEN);
 dc.SetBrush(*wxTRANSPARENT_BRUSH);
 
 dc.DrawRectangle(0, 0, width, height);
 /*wxRect rc = wxRect(0, 0, width, height);
 dc.DrawRoundedRectangle(rc,5);*/
 
 wxPoint pt[3];
 pt[0].x = 1;
 pt[0].y = height-1;
 pt[1].x = pt[1].y = 1;
 pt[2].x = width-1;
 pt[2].y = 1;
 dc.SetPen(*wxWHITE_PEN);
 dc.DrawLines(3,pt);
 
 pt[1].x = width-1;
 pt[1].y = height-1;
 dc.SetPen(*wxBLACK_PEN);
 dc.DrawLines(3,pt);
 
 int x = normal_bitmap->GetWidth();
 int y = (height-normal_bitmap->GetHeight())/2.0;
 dc.DrawBitmap(*normal_bitmap, width-x-5, y, true);
 
 if (text.IsEmpty())
 {
  break;
 }
 
 if (!text_font.IsNull())
 {
  dc.SetFont(text_font);
 }
 int pointsize = text_font.GetPointSize();
 //dc.SetLayoutDirection(wxLayout_Default);
 
 int bit = text.Find('\n');
 if (bit!=-1)
 {
  wxString str;
  str = text.Left(bit);
  dc.DrawText(str,5,height/2-pointsize-2);
 
  int length = text.Length();
  str = text.Mid(bit+1,length-bit-1);
  dc.DrawText(str,5,height/2);
 }
 else
 {
  dc.DrawText(text,5,height/2-7);
 }
 }
   break;
 }
 
}
 
void wxBitmapButtonEx::DrawBackground(wxDC* dc)
{
 if (is_used_bg)
 {
  dc->DrawBitmap(background_bitmap, 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 wxBitmapButtonEx::OnEnter(wxMouseEvent& event)
{
 /* button_status = BitmapButtonEnter;
 Refresh();
 Update();*/
}
 
void wxBitmapButtonEx::OnLeave(wxMouseEvent& event)
{
 if (!IsEnabled())
 {
  return;
 }
 button_status = BitmapButtonLeave;
 Refresh();
 Update();
}
 
void wxBitmapButtonEx::OnDClick(wxMouseEvent& event)
{
 /* button_status = BitmapButtonDown;
 Refresh(false);
 Update();*/
}
 
void wxBitmapButtonEx::OnDown(wxMouseEvent& event)
{
 button_status = BitmapButtonDown;
 Refresh();
 Update();
}
 
void wxBitmapButtonEx::OnUp(wxMouseEvent& event)
{
 if (BitmapButtonDown != button_status)
 {
  return;
 }
 button_status = BitmapButtonUp;
 Refresh();
 Update();
 wxCommandEvent myEvent(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
 myEvent.SetEventObject(this);
 GetEventHandler()->ProcessEvent(myEvent);
}
 
wxBitmapButtonEx* wxBitmapButtonEx::set_normal_bitmap(wxBitmap* bitmap)
{
 normal_bitmap = bitmap;
 return this;
}
 
wxBitmapButtonEx* wxBitmapButtonEx::set_down_bitmap(wxBitmap* bitmap)
{
 down_bitmap = bitmap;
 return this;
}
 
wxBitmapButtonEx* wxBitmapButtonEx::set_enter_bitmap(wxBitmap* bitmap)
{
 enter_bitmap = bitmap;
 return this;
}
 
bool wxBitmapButtonEx::Enable(bool enable)
{
 if (enable)
 {
  button_status = BitmapButtonNormal;
 } else
 {
  button_status = BitmapButtonDisable;
 }
 
 Refresh(false);
 Update();
 return wxControl::Enable(enable);
}
 
bool wxBitmapButtonEx::Disable()
{
 return Enable(false);
}
 
wxBitmapButtonEx* wxBitmapButtonEx::set_disable_bitmap(wxBitmap* bitmap)
{
 disable_bitmap = bitmap;
 return this;
}
 
wxBitmapButtonEx* wxBitmapButtonEx::set_background(const wxBitmap& bitmap)
{
 is_used_bg = true;
 background_bitmap = bitmap;
 return this;
}
 
bool wxBitmapButtonEx::set_background_colour(const wxColour& colour)
{
 is_used_bg = false;
 return wxControl::SetBackgroundColour(colour);
}
//
void wxBitmapButtonEx::set_button_text(wxString str)
{
 text = str;
}
//
void wxBitmapButtonEx::set_text_font(wxFont font)
{
 text_font = font;
}

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

原文链接:https://blog.csdn.net/hellokandy/article/details/60469981

标签:

相关文章

热门资讯

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
返回顶部