CreateWindowEx失败。如何让我的窗口(带按钮)实际弹出一次?

时间:2016-09-03 05:57:16

标签: c++ win32gui createwindowex

我只能假设大部分内容都有效,因为我无法通过CreateWindowEx检查。 如果有人会仔细检查我所有有趣的按钮代码,那也很棒。

<Directory /var/www/paradoxmayhem.com/>
        RewriteEngine On

        RewriteCond %{SERVER_NAME} =www.paradoxmayhem.com [OR]
        RewriteCond %{SERVER_NAME} =paradoxmayhem.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</Directory>

这是我需要帮助的地方

#include <windows.h>
#include <tchar.h> //I was told I needed this line but I don't believe I do.
#define ID_BTNENCRYPT 0

const char g_szClassName[] = "MyWindowClass";

HWND button;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)       {
switch(msg) { //Fun button stuff
    case WM_CREATE: {
        button = CreateWindow("button",
                              "Encrypt Message",
                              WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                              450, 620, 200, 30,
                              hwnd, (HMENU) ID_BTNENCRYPT, GetModuleHandle(NULL), NULL);

        break;
    }
    case WM_COMMAND: { //More fun button stuff
        switch (LOWORD(wParam)){
            case ID_BTNENCRYPT: {
                ::MessageBox(hwnd, "This will be your message once I get my $h!t together", "Encrypted Message", MB_OK);
            }
        }
        break;
    }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        DefWindowProc(hwnd, msg, wParam, lParam);

}

return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR     lpCmdLine, int nCmdShow) {
FreeConsole();

WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
if (!RegisterClassEx(&wc)) {
    ::MessageBox(NULL, "Window Registration Status: Hopelessly F***ed", "", MB_OK);
    return 0;
} //No apparent error in Window Registration

我很遗憾地收到错误消息,是的,我的窗口创建失败了。

hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "Great Window",
    WS_OVERLAPPEDWINDOW,
    300, 300,
    350, 350,
    NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
    ::MessageBox(NULL,"Window Creation Status: Gone to $h!t", "", MB_OK);
}

1 个答案:

答案 0 :(得分:0)

对于未处理的邮件,您的WndProc()未返回DefWindowProc()的返回值。缺少return语句,因此您的所有邮件最终都会降至return 0。当{{3}}返回0时,CreateWindowEx()会失败:

  

如果应用程序处理此消息,则应返回TRUE以继续创建窗口。 如果应用程序返回FALSE,则CreateWindow或CreateWindowEx函数将返回NULL句柄

你需要改变这个:

default:
    DefWindowProc(hwnd, msg, wParam, lParam);

对此:

default:
    return DefWindowProc(hwnd, msg, wParam, lParam);