No windows appearing when using WxWidgets on MSVC.

huangapple go评论72阅读模式
英文:

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 &lt;wx/wx.h&gt;
#include &lt;iostream&gt;

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

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString&amp; title, const wxPoint&amp; pos, const wxSize&amp; size);
};

bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame(&quot;Hello World&quot;, wxPoint(50, 50), wxSize(450, 340));
    frame-&gt;Show(true);
    return true;
}

MyFrame::MyFrame(const wxString&amp; title, const wxPoint&amp; pos, const wxSize&amp; size)
    : wxFrame(NULL, wxID_ANY, title, pos, size)
{
}

wxIMPLEMENT_APP(MyApp);

int main()
{
    std::cout &lt;&lt; &quot;Hello world, why isn&#39;t there a window showing?\n&quot;;
}

It compiles fine, but no window appears except from the output: Hello world, why isn&#39;t there a window showing? but it doesn't show any GUI window.
No windows appearing when using WxWidgets on MSVC.

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.

huangapple
  • 本文由 发表于 2023年4月11日 02:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定