【问题标题】:My first WinAPI program in Qt Creator doesn't show window?我在 Qt Creator 中的第一个 WinAPI 程序不显示窗口?
【发布时间】:2012-03-17 19:07:27
【问题描述】:

我是使用 WinAPI 的新手。我正在关注一个教程,在那里我找到了一个代码 sn-p。 sn -p 演示了一个基本程序。我在下面发布我的完整代码:

#include "a.h"
#include "windows.h"

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX WndClsEx;
    WndClsEx.cbSize      = sizeof(WNDCLASSEX);
    WndClsEx.style       = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc = WndProcedure;
    WndClsEx.cbClsExtra  = 0;
    WndClsEx.cbWndExtra  = 0;
    WndClsEx.hInstance   = hInstance;

    return 0;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}

从 Qt Creator 运行代码时,我没有收到任何错误。但是,运行它时不会出现任何窗口,但输出控制台会显示:

“MyProgram.exe 以代码 0 退出”

这可能是什么原因?

【问题讨论】:

  • 这不是 QT 程序。它也不是本机 Win32 程序,缺少太多代码。您需要 RTFM。
  • 你是不是指的是“Qt Creator”?例如,您使用的 IDE?因为Qt和“Qt Creator”是两个不同的东西。正如 Hans 所说,您显示的代码与 Qt 无关。
  • 感谢您的快速回复...我实际上不知道该怎么做.. 我正在使用 Qt SDK,其中 Qt 创建者是这里的嵌入式部分。我想使用 Qt 进行 winapi 编程。不可能吗?你能给我一些建议吗?

标签: winapi qt-creator win32gui


【解决方案1】:

我在下面发布我的完整代码:

您的代码看起来很像标准 Win32,但缺少很多代码。

例如,这个非常简单的 test.cpp 文件包含一个完整的 Win32 应用程序:

#define STRICT
#include <windows.h>

long PASCAL WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpszCmdParam, int nCmdShow)
{
   static char szClassName[] = "Hello World";
   MSG         msg;
   WNDCLASS    wndclass;

   memset(&wndclass, '\0', sizeof(wndclass));

   wndclass.style          = CS_HREDRAW|CS_VREDRAW;
   wndclass.lpfnWndProc    = WndProc;
   wndclass.cbClsExtra     = 0;
   wndclass.cbWndExtra     = 0;
   wndclass.hInstance      = hInstance;
   wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
   wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
   wndclass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
   wndclass.lpszMenuName   = 0;
   wndclass.lpszClassName  = szClassName;
   RegisterClass (&wndclass);

   // create a new window 
   HWND hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                              szClassName,
                              "My Hello World Window",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);

   ShowWindow (hwnd, nCmdShow);

   while (GetMessage (&msg, NULL, 0, 0)) {
      TranslateMessage (&msg);
      DispatchMessage (&msg);
   }

   return msg.wParam;
}

long APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
       case WM_DESTROY:
          PostQuitMessage (0);
          return 0;
    }

    return DefWindowProc (hwnd, message, wParam, lParam);
}

可以从命令行编译链接:

C:\TEMP>cl test.cpp user32.lib gdi32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
user32.lib
gdi32.lib

生成的test.exe可以运行,它会显示一个窗口:

C:\TEMP>test.exe

【讨论】:

    【解决方案2】:

    正如 Hans Passant 在他的评论中所暗示的,您遗漏了很多代码。我不知道你从哪个教程中复制了这个 sn-p,但肯定那里肯定有更多代码。

    例如,你没有registered it,也没有created the actual window,你不是showing it,而且(正如@rodrigo 提到的)你错过了消息循环。 This example on MSDN 说明了这一切的样子。

    是的,您可以在 Qt Creator 中完美地开发应用程序,而无需实际使用 Qt 作为您的用户界面。然而,我不会拒绝它。由于您拥有所有可用的工具,因此也可以看看 Qt 本身。你可能只是喜欢它。

    【讨论】:

    • 而且最重要的是,他没有消息循环。没有它,即使他做了所有其他事情,程序也会在这之后终止,窗口也会消失。
    • @rodrigo 是的,在我链接到的示例中隐含,但值得一提。我已经添加了。
    • 是的,你是对的。我错过了很多代码。后来我注意到教程中还缺少了一些实际存在的内容。谢谢。
    • 太好了,很高兴能帮到你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2010-12-03
    • 1970-01-01
    • 2011-05-18
    • 2023-04-02
    • 2015-07-02
    • 2020-03-02
    相关资源
    最近更新 更多