窗口不可见

时间:2014-11-19 17:57:20

标签: 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)

1 个答案:

答案 0 :(得分:2)

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

更改为:

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

或:

WNDCLASSEX wcEx = {0};

应该解决你的问题。