C 编程 HWND
C programming HWND
当我编译我的代码时,应该打开 window 但它没有。我创建了 class、HWND 和应用程序处理程序;依然没有。
我是新手,不好意思问这个问题。
应用程序运行良好,没有错误,但 window 似乎没有出现。
#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(®ularWndClass) < 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);
一个window过程应该return一个LRESULT
和DefWindowProc
在必要时这样做,但你不传递它。将其更改为
return DefWindowProc(regularWnd, message, wparam, lparam);
您的 window 将按预期显示。
除此之外,winapi 不使用 errno
,因此 perror()
将不起作用。您必须使用 GetLastError()
和 FormatMessage()
来显示有意义的错误消息,并且使用 WinMain()
(强烈建议使用子系统 windows
),您不会默认显示控制台他们....
最后,UpdateWindow()
完全没有必要。
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);
return 0;
case WM_CLOSE:
DestroyWindow(regularWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(regularWnd, message, wparam, lparam);
}
将您的 window 过程更改为此
另外,使用GetLastError
和FormatMessage
打印错误;不是 perror
,那是或 C 标准库调用。这是一个如何使用这个函数的例子
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx
当我编译我的代码时,应该打开 window 但它没有。我创建了 class、HWND 和应用程序处理程序;依然没有。
我是新手,不好意思问这个问题。
应用程序运行良好,没有错误,但 window 似乎没有出现。
#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(®ularWndClass) < 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);
一个window过程应该return一个LRESULT
和DefWindowProc
在必要时这样做,但你不传递它。将其更改为
return DefWindowProc(regularWnd, message, wparam, lparam);
您的 window 将按预期显示。
除此之外,winapi 不使用 errno
,因此 perror()
将不起作用。您必须使用 GetLastError()
和 FormatMessage()
来显示有意义的错误消息,并且使用 WinMain()
(强烈建议使用子系统 windows
),您不会默认显示控制台他们....
最后,UpdateWindow()
完全没有必要。
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);
return 0;
case WM_CLOSE:
DestroyWindow(regularWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(regularWnd, message, wparam, lparam);
}
将您的 window 过程更改为此
另外,使用GetLastError
和FormatMessage
打印错误;不是 perror
,那是或 C 标准库调用。这是一个如何使用这个函数的例子
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680582(v=vs.85).aspx