在 Win32 程序中用 main() 函数替换 WinMain()

2021-12-17 00:00:00 winapi c++

我在 StackOverflow 和 Google 上搜索了一点,但没有找到这个想法.我想用这种类型的用户编程来启动我的应用程序:

I searched a little bit on StackOverflow and Google but couldn't get the idea. I want to start my application with this type of user programming:

int main()
{
  Window App("Test", 640, 480);

  while(App.IsOpen())
  {
    // Do the stuff
  }
}

但这是不可能的,因为我应该将 hInstancehPrevInstance 和其他参数传递给 WinMain 函数.实际上,我设计了一个 Window 类,使窗口创建更容易一些.我在 SFML 上看到了这个实现,但我不知道它是怎么做到的.

But this isn't possible because I should pass the hInstance and hPrevInstance and other parameters to a WinMain function. Actually there is a Window class which I designed to make the window creation a little bit easier. I saw this implementation on SFML but I don't know how it did come to this.

现在我使用的是通常的方式:

Right now I'm using the usual way:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, int)
{
  Window App(hInst, hPrevInst, "Test", 640, 480);

  while(App.IsOpen())
  {
    // Do the stuff
  }
}

谢谢.

推荐答案

您可以在windows"中使用标准的 main应用程序(即 GUI 子系统 Windows 应用程序),即使使用 Microsoft 工具,如果您将以下内容添加到 Microsoft 链接器选项:

You can use standard main in a "windows" app (that is, a GUI subsystem Windows application) even with the Microsoft tools, if you add the following to the Microsoft linker options:

/subsystem:windows /ENTRY:mainCRTStartup

请注意,这对于 GNU 工具链来说不是必需的.

Note that this is not necessary for the GNU toolchain.

对于 Microsoft 工具,您也可以将其添加到主文件中:

Still for the Microsoft tools you can alternatively add this to your main file:

#ifdef _MSC_VER
#    pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

James McNellis 告诉您如何获取 hInstance.

James McNellis tells you how to get the hInstance.

相关文章