gpt4 book ai didi

C 编程 HWND

转载 作者:太空宇宙 更新时间:2023-11-04 08:03:22 24 4
gpt4 key购买 nike

当我编译我的代码时,应该打开一个窗口,但它没有。我已经创建了一个类、HWND 和应用程序处理程序;依然没有。

我有点新,很抱歉这个问题。

应用程序运行良好,没有错误,但窗口似乎没有出现。

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


LRESULT CALLBACK myCallBack(HWND regularWnd, UINT message, WPARAM wparam, LPARAM lparam){
switch(message){
case 0x0201:
printf("left Click");
MessageBox(regularWnd, "Left Click", "event handler", MB_OK);
break;
case WM_CLOSE:
DestroyWindow(regularWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(regularWnd, message, wparam, lparam);
break;
}
return 0;
}

int WINAPI WinMain(HINSTANCE newHInstance, HINSTANCE prevHINSTANCE, LPSTR lpCmdLine, int cmdShow){

WNDCLASSEX regularWndClass;

regularWndClass.cbSize = sizeof(WNDCLASSEX);
regularWndClass.cbWndExtra = 0;
regularWndClass.cbClsExtra = 0;
regularWndClass.style = 0;
regularWndClass.lpszClassName = "regularWindowClass";
regularWndClass.lpszMenuName = NULL;
regularWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+2);
regularWndClass.lpfnWndProc = myCallBack;
regularWndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
regularWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
regularWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
regularWndClass.hInstance = newHInstance;

if(!RegisterClassEx(&regularWndClass) < 0){
perror("Error Wnd class: ");
exit(0);
}

HWND regularWnd;

regularWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "regularWindowClass", "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, newHInstance, NULL);

if(regularWnd < 0){
perror("window error : ");
exit(0);
}

ShowWindow(regularWnd, cmdShow);
UpdateWindow(regularWnd);

MSG message;
while(GetMessage(&message, NULL, 0, 0) > 0){
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}

最佳答案

直接错误在这一行:

DefWindowProc(regularWnd, message, wparam, lparam);

一个窗口过程应该返回一个 LRESULT 并且 DefWindowProc 会在必要时返回它,但是您没有传递它。将此更改为

return DefWindowProc(regularWnd, message, wparam, lparam);

您的窗口将按预期显示。

除此之外,不使用 errno,因此 perror() 将不起作用。您必须使用 GetLastError()FormatMessage() 来获取有意义的错误消息,并使用 WinMain()(强烈建议子系统 windows),默认情况下你不会有一个控制台来显示它们....

最后,UpdateWindow() 是完全没有必要的。

关于C 编程 HWND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44478514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com