c++ - 窗口不可见

标签 c++ directx

你好,我正在寻求帮助,我尝试使用 directx 和 c++ 创建一个窗口,一切似乎都很好,但是当我运行它时,窗口没有显示,但 NetBeans 告诉我构建成功并且告诉我程序正在运行。谁能帮我解决我的问题?

#define WIN32_LEAN_AND_MEAN 
#define UNICODE

#include <stdio.h>
#include <windows.h>

// Variables
LPCWSTR windowTitle     = L"Window";
int     windowWidth     = 980;
int     windowHeight    = 640;
HWND    windowInstance  = NULL;
LPCWSTR windowClassName = L"WindowClass";

// Forwards
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// Functions
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wcEx;

    wcEx.cbSize        = sizeof wcEx;
    wcEx.style         = CS_HREDRAW | CS_VREDRAW;
    wcEx.lpfnWndProc   = WindowProc;
    wcEx.lpszClassName = windowClassName;
    wcEx.hInstance     = hInstance;

    if (!RegisterClassEx(&wcEx))
        return printf("Console Output: RegisterClassEx has failed!");

    windowInstance = CreateWindowEx(
                        0,
                        windowClassName,
                        windowTitle,
                        WS_OVERLAPPEDWINDOW,
                        GetSystemMetrics(SM_CXSCREEN) / windowWidth,
                        GetSystemMetrics(SM_CYSCREEN) / windowHeight,
                        windowWidth,
                        windowHeight,
                        NULL,
                        NULL,
                        hInstance,
                        NULL
                     ); 

    if (!windowInstance)
        return printf("Console Output: There isn't an instance of 'windowInstance'. ");

    ShowWindow(windowInstance, nCmdShow);

    printf("Console Output: Program is running");

    MSG msg;

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

        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } 
    }

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

控制台:

 "/C/Development/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/none/Documents/NetBeansProjects/Program1'
"/C/Development/MinGW/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/program1.exe
make.exe[2]: Entering directory `/c/Users/none/Documents/NetBeansProjects/Program1'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW-Windows
g++     -o dist/Debug/MinGW-Windows/program1 build/Debug/MinGW-Windows/main.o 
make.exe[2]: Leaving directory `/c/Users/none/Documents/NetBeansProjects/Program1'
make.exe[1]: Leaving directory `/c/Users/none/Documents/NetBeansProjects/Program1'

BUILD SUCCESSFUL (total time: 3s)

运行控制台:

Console Output: RegisterClassEx has failed!
RUN FAILED (exit value 43, total time: 117ms)

最佳答案

您忘记将 wcEx 初始化为 0,这意味着您未设置的部分可能包含垃圾值,这可能导致 RegisterClassEx 失败。

更改为:

WNDCLASSEX wcEx;
memset(&wcEx, 0, sizeof wcEx);

或:

WNDCLASSEX wcEx = {0};

应该可以解决您的问题。

关于c++ - 窗口不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27023688/

相关文章:

c++ - 如何push_back到子 vector ?

c++ - 使用 C++ 获取网站的标题和 Favicon

java - 平滑后的GPS数据对比

c++ - 如何打印图中两个顶点之间的最短路径?

动画和实例化表演

directx - 将网格切成两半 - DirectX

c++ - DirectX 字体不绘制?

c++ - 计算 3 维中的相机 LookAt 位置 (DirectX)

c++ - 输出文件覆盖不正确

c# - 通过 sharpdx API 绘制和填充具有 4 个顶点(二维位置)的多边形