首页 > 解决方案 > wxWidgets 菜单栏不显示

问题描述

我正在尝试使用 wxWidgets 创建一个带有菜单的简单程序。但是,菜单似乎没有正确显示。

这是我的代码:

helloworld.hpp:

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "TextFrame.hpp"

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

DECLARE_APP(HelloWorldApp)

你好世界.cpp:

#include "helloworld.hpp"

IMPLEMENT_APP(HelloWorldApp)

bool HelloWorldApp::OnInit() {
    TextFrame *frame = new TextFrame(_T("Hi"), 200, 200, 800, 600);

    frame->CreateStatusBar();
    frame->SetStatusText(_T("Hello, World!"));

    frame->Show(true);
    SetTopWindow(frame);

    return true;
}

文本框.hpp:

#pragma once

#include <wx/wxprec.h>

#ifndef WX_PREC
    #include <wx/wx.h>
#endif

class TextFrame : public wxFrame {
public:

    /** Constructor. Creates a new TextFrame */
    TextFrame(const wxChar *title, int xpos, int ypos, int width, int height);

private:

    wxTextCtrl *m_pTextCtrl;
    wxMenuBar *m_pMenuBar;
    wxMenu *m_pFileMenu;
    wxMenu *m_pHelpMenu;
};

文本框.cpp:

#include "TextFrame.hpp"

TextFrame::TextFrame(const wxChar *title, int xpos, int ypos, int width, int height)
    : wxFrame((wxFrame *) NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height))
{
    m_pTextCtrl = new wxTextCtrl(this, -1, _T("Type some text..."),
                       wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);

    m_pMenuBar = new wxMenuBar();
    // File Menu
    m_pFileMenu = new wxMenu();
    m_pFileMenu->Append(wxID_OPEN, _T("&Open"));
    m_pFileMenu->Append(wxID_SAVE, _T("&Save"));
    m_pFileMenu->AppendSeparator();
    m_pFileMenu->Append(wxID_EXIT, _T("&Quit"));
    m_pMenuBar->Append(m_pFileMenu, _T("&File"));
    // About menu
    m_pHelpMenu = new wxMenu();
    m_pHelpMenu->Append(wxID_ABOUT, _T("&About"));
    m_pMenuBar->Append(m_pHelpMenu, _T("&Help"));

    SetMenuBar(m_pMenuBar);
}

这段代码(大部分)直接来自这里

它编译成功(使用g++ TextFrame.cpp helloworld.cpp `wx-config -cxxflags --libs` -o helloworld),但是当我运行它时,我看到了默认菜单,而不是我尝试添加的自定义菜单。

我尝试了“菜单”示例并且效果很好,所以我认为我在这里做错了。

谢谢您的帮助。

标签: c++wxwidgets

解决方案


推荐阅读