【问题标题】:Win32 API logic error: Code compiles fine but the main window doesn't show upWin32 API 逻辑错误:代码编译正常但主窗口不显示
【发布时间】:2015-05-24 20:32:32
【问题描述】:

由于程序大小和性能问题,我正在重写我编写的软件;代码编译正常,但窗口不显示。

我已经研究了有关此问题的其他问题,但没有解决我的问题,这就是为什么我要问一个新问题,为了完整起见,请解释代码有什么问题以及如何解决问题。

我正在使用 Visual Studio 2013。 代码如下:

WFrame.h

#pragma once

#include <Windows.h>
#include <tchar.h>
#include <wchar.h>

class WFrame
{
public:
    WFrame();
    WFrame(LPCWSTR szClassName, LPCWSTR szWindowTitle, HINSTANCE hInstance, int nCmdShow, int nX, int nY, int nWidth, int nHeight, DWORD dwStyle = WS_EX_OVERLAPPEDWINDOW);
    ~WFrame();

    void show();

    static LRESULT CALLBACK staticWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    virtual LRESULT CALLBACK WindowProcedure(UINT uMsg, WPARAM wParam, LPARAM lParam);
protected:

private:
    HWND hWnd;
    int cmdShow;
};

WFrame.cpp

#include "WFrame.h"


WFrame::WFrame()
{

}

WFrame::WFrame(LPCWSTR szClassName, LPCWSTR szWindowTitle, HINSTANCE hInstance, int nCmdShow, int nX, int nY, int nWidth, int nHeight, DWORD dwStyle)
{
    WNDCLASSEX wClass;
    ZeroMemory(&wClass, sizeof(WNDCLASSEX));
    wClass.cbClsExtra = NULL;
    wClass.cbSize = sizeof(WNDCLASSEX);
    wClass.cbWndExtra = NULL;
    wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wClass.hIcon = NULL;
    wClass.hIconSm = NULL;
    wClass.hInstance = hInstance;
    wClass.lpfnWndProc = (WNDPROC) WFrame::staticWindowProcedure;
    wClass.lpszClassName = szClassName;
    wClass.lpszMenuName = NULL;
    wClass.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClassEx(&wClass))
    {
        int nResult = GetLastError();
        MessageBox(NULL,
            L"Window class creation failed",
            L"Window Class Failed",
            MB_ICONERROR);
    }

    this->hWnd = CreateWindow(
        szClassName,
        szWindowTitle,
        dwStyle,
        nX,
        nY,
        nWidth,
        nHeight,
        NULL,
        NULL,
        hInstance,
        NULL);

    if (!hWnd)
    {
        int nResult = GetLastError();

        MessageBox(NULL,
            L"Window creation failed",
            L"Window Creation Failed",
            MB_ICONERROR);
    }

}

WFrame::~WFrame()
{
    delete this;
}

void WFrame::show()
{
    ShowWindow(this->hWnd, this->cmdShow);
    UpdateWindow(this->hWnd);
}

LRESULT CALLBACK WFrame::staticWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    WFrame* winptr = (WFrame*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

    if (winptr == NULL) {
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    else {
        return winptr->WindowProcedure(uMsg, wParam, lParam);
    }
}

LRESULT CALLBACK WFrame::WindowProcedure(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            DestroyWindow(this->hWnd);
            break;
        case WM_DESTROY:
            if (!GetParent(this->hWnd))
                PostQuitMessage(0);
            break;
    }

    return DefWindowProc(this->hWnd, uMsg, wParam, lParam);
}

Main.cpp

#include <Windows.h>

#include "WFrame.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WFrame * frame = new WFrame(L"Window", L"Window", hInstance, nCmdShow, 0, 0, 1024, 700);
    frame->show();

    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

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

    return msg.wParam;
}

感谢进一步的回答。

【问题讨论】:

  • this-&gt;cmdShow的值是多少?
  • cmdShow 是程序启动时发送到 WinMain 函数的值,它可能会根据您对程序的设置而有所不同,但是,在这种情况下,我刚刚检查过,它是 10。
  • 我试图向您指出@chishui 在他们的回答中所说的话。 ://
  • 你不需要输入delete this 你也不需要delete-&gt;。

标签: c++ winapi


【解决方案1】:

我没有看到你给成员变量cmdShow赋值,所以它的默认值为0,也就是SW_HIDE,所以你应该试试下面的代码,看看窗口是否可以显示或在WFrame初始化程序中赋值cmdShow。

void WFrame::show()
{
    ShowWindow(this->hWnd, SW_SHOW);
    UpdateWindow(this->hWnd);
}

【讨论】:

  • 我刚刚注意到,他也不叫SetWindowLongPtr,所以永远不会使用WFrame::WindowProcedure。我编译了代码并修复了您发现的错误并且它有效。顺便说一句,cmdShow 的值并不默认为0。它是未定义的。
  • 谢谢 :D 我没有设置 this->cmdShow 值,所以现在它可以工作了,你的回答也很有价值@BetaCarotin,感谢你们俩。
【解决方案2】:

您正在使用WS_EX_OVERLAPPEDWINDOW 作为CreateWindow 的dwStyle 参数。这个标志实际上应该用于CreateWindowEx 的dwExStyle 参数。请改用WS_OVERLAPPEDWINDOW。

完成后不要忘记delete frame;。你实际上甚至不需要一个指针。只需在堆栈上实例化 frame。

此外,对于您处理的窗口消息,您应该return 0;,而不是调用DefWindowProc。

【讨论】:

  • @Beta Carotin /OT 每天需要多少根胡萝卜才能让您的眼睛保持如此锐利的注意力?你很了不起!
  • @πάνταῥεῖ 不少,但我用咖啡代替 ;)
  • 如何使用可识别的头像:cdn.blogs.sheknows.com/vegan.sheknows.com/2012/04/…?咖啡不能代替胡萝卜,别把我当回事 ;-) ...
  • @πάνταῥεῖ 这真的很可爱。我喜欢它:)
  • 感谢您的回答,我进行了您建议的更改,但窗口仍然没有出现,您知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 2018-07-28
  • 2014-08-27
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多