简体   繁体   English

Win32窗口不显示

[英]Win32 Window not displaying

I am working on a directX application and I'm trying to set up a window. 我正在处理DirectX应用程序,并且正在尝试设置一个窗口。 But the problem is that my window isn't showing up instead displaying my popup I made for when the window fails to create. 但是问题是我的窗口没有显示,而是显示了当窗口创建失败时我为之创建的弹出窗口。 I have made windows multiple times and now it is not working. 我已经多次制作了Windows,但现在无法正常工作。 The only thing that I changed in my routine is that I switched my application to a 64 bit application instead of a 32 bit application. 我在例程中所做的唯一更改是将应用程序切换到64位应用程序,而不是32位应用程序。 I have a 64 bit computer and it should work. 我有一台64位计算机,它应该可以工作。

main.cpp main.cpp中

#include "Render\window.h"

int CALLBACK WinMain(HINSTANCE appInstance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdCount)
{
    Window window("Program", 800, 600);

    MSG msg = { 0 };
    while (true)
    {
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);

            if (msg.message == WM_QUIT) break;
        }
    }

    return 0;
}

window.h 在window.h

#pragma once

#include <Windows.h>

class Window
{
private:
    const char* const m_Title;
    const int m_Width;
    const int m_Height;
    HWND m_Handler;
public:
    Window(const char* title, int width, int height);

    inline HWND GetHandler() const { return m_Handler; }
private:
    void Init();
};

window.cpp window.cpp

#include "window.h"

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    if (msg == WM_DESTROY || msg == WM_CLOSE)
    {
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wparam, lparam);
}

Window::Window(const char* title, int width, int height)
    : m_Title(title), m_Width(width), m_Height(height)
{
    Init();
}

void Window::Init()
{
    WNDCLASS windowClass;
    windowClass.style = CS_OWNDC;
    windowClass.lpfnWndProc = WinProc;
    windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
    windowClass.lpszClassName = L"MainWindow";
    RegisterClass(&windowClass);

    m_Handler = CreateWindow(L"MainWindow", (LPCWSTR)m_Title, WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_VISIBLE, 100, 100, m_Width, m_Height, nullptr, nullptr, nullptr, nullptr);
    if (m_Handler == 0)
    {
        MessageBox(nullptr, L"Problem with creating window!", L"Error", MB_OK);
        exit(0);
    }
}

Your WNDCLASS structure contains uninitialized data. 您的WNDCLASS结构包含未初始化的数据。 Are you ignoring compiler warnings? 您是否忽略了编译器警告? You don't check for errors when calling RegisterClass . 调用RegisterClass时不检查错误。 Quite likely that RegisterClass fails and you press on regardless. RegisterClass很可能会失败,因此无论如何都要按。

Make sure that the WNDCLASS structure is initialized: 确保WNDCLASS结构已初始化:

WNDCLASS windowClass = { 0 };

And check for errors whenever you call Win32 API functions. 并在每次调用Win32 API函数时检查错误。 Here, check the value returned by RegisterClass . 在这里,检查RegisterClass返回的值。 The documentation tells you what it means. 该文档告诉您这是什么意思。

To your credit you did at least check the value returned by CreateWindow . 值得称赞的是,您至少检查了CreateWindow返回的值。 But the documentation tells you that in the event of failure, call GetLastError to find out why the call failed. 但是文档告诉您,如果失败,请调用GetLastError找出调用失败的原因。 You did not do that? 你没有那样做吗? I suspect that your biggest problem is that you aren't reading the documentation in enough detail. 我怀疑您最大的问题是您没有足够详细地阅读文档。

When you call CreateWindow , you tried to pass m_Title as the window text argument. 调用CreateWindow ,您尝试将m_Title作为窗口文本参数传递。 The compiler objected with a type mismatch error. 编译器反对类型不匹配错误。 You suppressed that error with this cast: 您使用此强制转换抑制了该错误:

(LPCWSTR)m_Title

Now, m_Title is const char* . 现在, m_Titleconst char* It is not const wchar_t* . 它不是const wchar_t* No amount of casting makes it so. 没有大量的铸造就可以做到。 Don't cast away type mismatch errors. 不要丢弃类型不匹配错误。 Pass the correct type. 传递正确的类型。

Either call CreateWindowA and pass m_Title , or change m_Title to be of type const wchar_t* . 调用CreateWindowA并传递m_Title ,或将m_Title更改为const wchar_t*类型。 If you do the latter you'll need to pass a wide literal, L"Program" instead of "Program" . 如果执行后者,则需要传递一个宽文字,即L"Program"而不是"Program"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM