c++ - 为什么我的 Win32(c++, visual studio 2013) 窗口没有出现?

标签 c++ winapi visual-studio-2013 window

代码如下:

#include <windows.h>
#include <windowsx.h>


// the WindowProc callback function prototype
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);

// win32 entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    ///*
    // window handler
    HWND hwnd;

    // struct for window information
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // setting wc struct with window values/properties
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "TestWindow";
    wc.lpfnWndProc = WindowProc;

    // register window before creation/use
    RegisterClassEx(&wc);

    // creating the window class
    hwnd = CreateWindowEx(NULL, "TestWindow", "First Win32 Program", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);

    // show the window 
    ShowWindow(hwnd, nCmdShow);//*/

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){
    return 0;
}

这是编译器/调试/运行时控制台输出:

'wintest.exe' (Win32): Loaded 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
The program '[3484] wintest.exe' has exited with code 0 (0x0).

这是一个非常基本的简单程序,它所做的只是假设从左上角开始弹出一个大小为 1000x1000 的 win32 窗口。

最佳答案

我看到的第一个错误是在您的窗口过程中。您未明确处理的任何消息都应传递给 DefWindowProc

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    return DefWindowProc(hwnd, message, wparam, lparam);
}

这是窗口过程所需的绝对最低限度。实际上,您至少需要处理 WM_CLOSEWM_DESTROY,然后在添加功能时会处理更多。

当它在创建过程中向窗口发送 WM_NCCREATE 消息时,您损坏的窗口过程导致 CreateWindowEx 失败。

另一个明显的问题是您根本不执行任何错误检查。通过忽略您进行的每个 API 调用的返回值,您将无法诊断程序失败的地方。

最后,您不包括消息循环。因此,即使您可以让您的窗口显示出来,该过程也会立即终止。

这个程序会显示一个窗口。显然还有更多工作要做,但这是一个开始。

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    switch(message)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
    int nCmdShow)
{
    WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "TestWindow";
    wc.lpfnWndProc = WindowProc;

    if (!RegisterClassEx(&wc))
        return 1;

    HWND hwnd = CreateWindowEx(0, "TestWindow", "First Win32 Program", 
        WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL);
    if (!hwnd)
        return 1;

    ShowWindow(hwnd, nCmdShow);

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

    return 0;
}

关于c++ - 为什么我的 Win32(c++, visual studio 2013) 窗口没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140323/

相关文章:

c++ - 强制仅使用特定类型调用函数

c++ - 类的多态性和封装

c++ - 如何在 Windows 中 Hook 复制操作

c++ - 链接 SFML 时出错。 LNK2001

css - 在设计 View 中一起使用 "display:inline-block"和 "line-height"时 Visual Studio 中可能存在错误

c++ - boost 不可复制的怪异性

c++ - 为什么我的数组程序中不能使用空格?

c# - 用鼠标滚轮滚动 ListView 偶尔会取消滚动

c++ - SetConsoleMode 标志中 ENABLE_PROCESSED_INPUT 的含义

c# - NUnit 从 ReSharper 与 Visual Studio 2013 获取不同的哈希码值