89.md 9.5 KB
Newer Older
W
init  
wizardforcel 已提交
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
# wxWidgets 中的菜单和工具栏

> 原文: [http://zetcode.com/gui/wxwidgets/menustoolbars/](http://zetcode.com/gui/wxwidgets/menustoolbars/)

菜单栏是 GUI 应用程序中最可见的部分之一。 它是位于各个菜单中的一组命令。 在控制台应用程序中,您必须记住所有这些神秘命令,在这里,我们将大多数命令分组为逻辑部分。 有公认的标准可以进一步减少学习新应用程序的时间。 要在 wxWidgets 中实现菜单栏,我们需要三个类:`wxMenuBar``wxMenu``wxMenuItem`

## 简单菜单示例

在 wxWidgets 中创建菜单栏非常简单。

`menu.h`

```
#include <wx/wx.h>
#include <wx/menu.h>

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

    void OnQuit(wxCommandEvent& event);

    wxMenuBar *menubar;
    wxMenu *file;

};

```

`menu.cpp`

```
#include "menu.h"

SimpleMenu::SimpleMenu(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

  menubar = new wxMenuBar;
  file = new wxMenu;
  file->Append(wxID_EXIT, wxT("&Quit"));
  menubar->Append(file, wxT("&File"));
  SetMenuBar(menubar);

  Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
      wxCommandEventHandler(SimpleMenu::OnQuit));
  Centre();

}

void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
  Close(true);
}

```

`main.h`

```
#include <wx/wx.h>

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

```

`main.cpp`

```
#include "main.h"
#include "menu.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{

    SimpleMenu *menu = new SimpleMenu(wxT("Simple Menu"));
    menu->Show(true);

    return true;
}

```

```
menubar = new wxMenuBar;

```

首先,我们创建一个菜单栏对象。

```
file = new wxMenu;

```

接下来,我们创建一个菜单对象。

```
file->Append(wxID_EXIT, wxT("&Quit"));

```

我们将菜单项添加到菜单对象中。 第一个参数是菜单项的 ID。 第二个参数是菜单项的名称。 在这里,我们没有明确创建一个`wxMenuItem`。 它是通过`Append()`方法在后台创建的。 稍后,我们将手动创建`wxMenuItem`

```
menubar->Append(file, wxT("&File"));
SetMenuBar(menubar);

```

W
wizardforcel 已提交
118
之后,我们将菜单添加到菜单栏中。 &字符创建一个加速键。 带下划线的&后面的字符。 这样,可以通过 `Alt` + `F` 快捷方式访问菜单。 最后,我们调用`SetMenuBar()`方法。 该方法属于`wxFrame`小部件。 它设置菜单栏。
W
init  
wizardforcel 已提交
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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

![Simle menu example](img/af5054ee76309a29fce9dec98656aecb.jpg)

Figure: Simple menu example

## 子菜单

每个菜单也可以有一个子菜单。 这样,我们可以将类似的命令分组。 例如,我们可以将隐藏或显示各种工具栏(例如个人栏,地址栏,状态栏或导航栏)的命令放置在称为工具栏的子菜单中。 在菜单中,我们可以使用分隔符来分隔命令。 这是一条简单的线。 通常的做法是使用单个分隔符将命令(例如新建,打开,保存)与命令(例如打印,打印预览)分开。 在我们的示例中,我们将看到如何创建子菜单和菜单分隔符。

`menu.h`

```
#include <wx/wx.h>
#include <wx/menu.h>

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

  void OnQuit(wxCommandEvent & event);

  wxMenuBar *menubar;
  wxMenu *file;
  wxMenu *imp;
  wxMenuItem *quit;

};

```

`menu.cpp`

```
#include "menu.h"

SubMenu::SubMenu(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

  menubar = new wxMenuBar;
  file = new wxMenu;

  file->Append(wxID_ANY, wxT("&New"));
  file->Append(wxID_ANY, wxT("&Open"));
  file->Append(wxID_ANY, wxT("&Save"));
  file->AppendSeparator();

  imp = new wxMenu;
  imp->Append(wxID_ANY, wxT("Import newsfeed list..."));
  imp->Append(wxID_ANY, wxT("Import bookmarks..."));
  imp->Append(wxID_ANY, wxT("Import mail..."));

  file->AppendSubMenu(imp, wxT("I&mport"));

  quit = new wxMenuItem(file, wxID_EXIT, wxT("&Quit\tCtrl+W"));
  file->Append(quit);

  menubar->Append(file, wxT("&File"));
  SetMenuBar(menubar);

  Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, 
      wxCommandEventHandler(SubMenu::OnQuit));
  Centre();

}

void SubMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
  Close(true);
}

```

`main.h`

```
#include <wx/wx.h>

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

```

`main.cpp`

```
#include "main.h"
#include "menu.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{

    SubMenu *smenu = new SubMenu(wxT("Submenu"));
    smenu->Show(true);

    return true;
}

```

我们在文件菜单中创建了一个子菜单。 这是一个导入子菜单,可以在 Opera Web 浏览器中看到。

```
file->AppendSeparator();

```

创建菜单分隔线,并调用`AppendSeparator()`方法。

```
imp = new wxMenu;
imp->Append(wxID_ANY, wxT("Import newsfeed list..."));
imp->Append(wxID_ANY, wxT("Import bookmarks..."));
imp->Append(wxID_ANY, wxT("Import mail..."));

file->AppendSubMenu(imp, wxT("I&mport"));

```

子菜单的创建类似于普通菜单。 它附有`AppendSubMenu()`方法。

![Submenu](img/92362f05cb44ede5a7437aecfeff88b6.jpg)

Figure: Submenu

## 工具栏

菜单将我们可以在应用程序中使用的所有命令分组。 使用工具栏可以快速访问最常用的命令。

```
virtual wxToolBar* wxFrame::CreateToolBar(long style = wxTB_DEFAULT_STYLE,
    wxWindowID id = wxID_ANY, const wxString & name = wxToolBarNameStr)

```

要创建工具栏,我们调用框架窗口小部件的`CreateToolBar()`方法。

## 一个简单的工具栏

我们的第一个示例将创建一个简单的工具栏。

`toolbar.h`

```
#include <wx/wx.h>

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

    void OnQuit(wxCommandEvent& event);
};

```

`toolbar.cpp`

```
#include "toolbar.h"

Toolbar::Toolbar(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, 
                 wxDefaultPosition, wxSize(300, 250)) {

    wxImage::AddHandler(new wxPNGHandler);

    wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG);

    wxToolBar *toolbar = CreateToolBar();
    toolbar->AddTool(wxID_EXIT, wxT("Exit application"), exit);
    toolbar->Realize();

    Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, 
        wxCommandEventHandler(Toolbar::OnQuit));
}

void Toolbar::OnQuit(wxCommandEvent& WXUNUSED(event)) {

    Close(true);
}

```

`main.h`

```
#include <wx/wx.h>

class MyApp : public wxApp {

    public:
        virtual bool OnInit();
};

```

`main.cpp`

```
#include "main.h"
#include "toolbar.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {

    Toolbar *toolbar = new Toolbar(wxT("Toolbar"));
    toolbar->Show(true);

    return true;
}

```

在我们的示例中,我们创建了一个工具栏和一个工具按钮。 单击工具栏按钮将终止应用程序。

```
wxToolBar *toolbar = CreateToolBar();

```

我们创建一个工具栏。

```
toolbar->AddTool(wxID_EXIT, wxT("Exit application"), exit);

```

我们将工具添加到工具栏。

```
toolbar->Realize();

```

添加工具后,我们将调用`Realize()`方法。

![Toolbar](img/855a3cd833ced4192f5c0b4811179ae4.jpg)

Figure: Toolbar

#### 工具栏

如果我们要拥有多个工具栏,则必须以其他方式创建它们,例如 除了调用`CreateToolbar()`方法。

`toolbars.h`

```
#include <wx/wx.h>

class Toolbar : public wxFrame {

    public:
        Toolbar(const wxString& title);

        void OnQuit(wxCommandEvent& event);

        wxToolBar *toolbar1;
        wxToolBar *toolbar2;
};

```

`toolbars.cpp`

```
#include "toolbars.h"

Toolbar::Toolbar(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, 
                 wxDefaultPosition, wxSize(300, 250)) {

    wxImage::AddHandler(new wxPNGHandler);

    wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG);
    wxBitmap newb(wxT("new.png"), wxBITMAP_TYPE_PNG);
    wxBitmap open(wxT("open.png"), wxBITMAP_TYPE_PNG);
    wxBitmap save(wxT("save.png"), wxBITMAP_TYPE_PNG);

    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);

    toolbar1 = new wxToolBar(this, wxID_ANY);
    toolbar1->AddTool(wxID_ANY, wxT("New"), newb);
    toolbar1->AddTool(wxID_ANY, wxT("Open"), open);
    toolbar1->AddTool(wxID_ANY, wxT(""), save);
    toolbar1->Realize();

    toolbar2 = new wxToolBar(this, wxID_ANY);
    toolbar2->AddTool(wxID_EXIT, wxT("Exit application"), exit);
    toolbar2->Realize();

    vbox->Add(toolbar1, 0, wxEXPAND);
    vbox->Add(toolbar2, 0, wxEXPAND);

    SetSizer(vbox);

    Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, 
        wxCommandEventHandler(Toolbar::OnQuit));
}

void Toolbar::OnQuit(wxCommandEvent& WXUNUSED(event)) {

    Close(true);
}

```

`main.h`

```
#include <wx/wx.h>

class MyApp : public wxApp {

    public:
        virtual bool OnInit();
};

```

`main.cpp`

```
#include "main.h"
#include "toolbars.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {

    Toolbar *toolbar = new Toolbar(wxT("Toolbar"));
    toolbar->Show(true);

    return true;
}

```

在我们的示例中,我们创建了两个水平工具栏。 我们将它们放置在垂直包装机上。

```
toolbar1 = new wxToolBar(this, wxID_ANY);
...
toolbar2 = new wxToolBar(this, wxID_ANY);

```

在这里,我们创建两个工具栏。

```
vbox->Add(toolbar1, 0, wxEXPAND);
vbox->Add(toolbar2, 0, wxEXPAND);

```

在这里,我们将它们添加到垂直框大小调整器中。

![Toolbars](img/c5b6741f436a008f12f8486536d5f8c4.jpg)

Figure: Toolbars

在 wxWidgets 教程的这一部分中,我们介绍了菜单和工具栏。