英文:
No windows appearing when using WxWidgets on MSVC
问题
我在使用WxWidgets时遇到问题,我的代码如下:
#include <wx/wx.h>
#include <iostream>
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
    frame->Show(true);
    return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(NULL, wxID_ANY, title, pos, size)
{
}
wxIMPLEMENT_APP(MyApp);
int main()
{
    std::cout << "Hello world, why isn't there a window showing?\n";
}
它编译正常,但除了输出的内容:Hello world, why isn't there a window showing?之外,没有显示任何GUI窗口。如何解决这些错误?
英文:
I am having an issue in using WxWidgets, my code is as follows:
#include <wx/wx.h>
#include <iostream>
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
    frame->Show(true);
    return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(NULL, wxID_ANY, title, pos, size)
{
}
wxIMPLEMENT_APP(MyApp);
int main()
{
    std::cout << "Hello world, why isn't there a window showing?\n";
}
It compiles fine, but no window appears except from the output: Hello world, why isn't there a window showing? but it doesn't show any GUI window.

I have been using vcpkg on MSVC to get the WxWidgets, does any one know how I could resolve these errors?
答案1
得分: 2
你正在构建你的应用程序作为一个控制台应用程序(/subsystem:console链接器选项),这意味着它的入口点是main(),而不是构建为Windows应用程序(/subsystem:windows)。你可以这样做,但然后你需要自己调用库的入口点(wxEntry())。
但除非你有一个很好的理由这样做,如果你使用makefiles或其他方式将其制作为Windows应用程序,你应该只需在IDE/linker选项中更改应用程序类型。然后,你应该完全移除main(),因为它不会被使用 - 而它的缺失不会导致Windows应用程序的链接错误。
英文:
You're building your application as a console application (/subsystem:console linker option), meaning that its entry point is main(), instead of building it as a Windows application (/subsystem:windows). You can do it like this, but then you're responsible for calling the library entry point (wxEntry()) yourself.
But unless you have a good reason to do it like this, you should just change the application type in the IDE/linker option if you use makefiles or something else to make it a Windows application. You should then remove main() entirely as it won't be used -- and its absence won't result in linking errors for a Windows application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论