C++ WinAPI窗口立即关闭
我一直在尝试学习WINAPI,但我创建的窗口立即关闭。如您所见,当按下W键或左键时,它将关闭程序,但在没有按下任何按钮的情况下运行时,它仍将关闭C++ WinAPI窗口立即关闭,c++,winapi,C++,Winapi,我一直在尝试学习WINAPI,但我创建的窗口立即关闭。如您所见,当按下W键或左键时,它将关闭程序,但在没有按下任何按钮的情况下运行时,它仍将关闭 #include <windows.h> #include <windowsx.h> // the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPA
#include <windows.h>
#include <windowsx.h>
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Game", // title of the window
WS_OVERLAPPEDWINDOW, // window style
1, // x-position of the window
1, // y-position of the window
1800, // width of the window
1000, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
// enter the main loop:
// this struct holds Windows event messages
MSG msg;
// wait for the next message in the queue, store the result in 'msg'
while (GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
}
// return this part of the WM_QUIT message to Windows
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
// this message is read when the window is closed
case WM_MOUSEMOVE:
{
// Retrieve mouse screen position
int x = (short)LOWORD(lParam);
int y = (short)HIWORD(lParam);
// Check to see if the left button is held down:
bool leftButtonDown = wParam & MK_LBUTTON;
// Check if right button down:
bool rightButtonDown = wParam & MK_RBUTTON;
if (leftButtonDown == true)
{
//left click
//example lets close the program when press w
PostQuitMessage(0);
return 0;
}
}
case WM_KEYDOWN:
{
switch (wParam)
{
case 'W':
//w pressed
//example lets close the program when press w
PostQuitMessage(0);
return 0;
}
}
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
}
default:
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}
#包括
#包括
//WindowProc函数原型
LRESULT回调WindowProc(HWND HWND,
UINT消息,
WPARAM WPARAM,
LPARAM(LPARAM);
//任何Windows程序的入口点
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
//窗口的句柄,由函数填充
HWND-HWND;
//此结构保存窗口类的信息
WNDCLASSEX wc;
//清除窗口类以供使用
零内存(&wc,sizeof(WNDCLASSEX));
//用所需信息填写结构
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.hInstance=hInstance;
wc.hCursor=LoadCursor(空,IDC_箭头);
wc.hbrBackground=(HBRUSH)彩色窗口;
wc.lpszClassName=L“WindowClass1”;
//注册窗口类
注册类别(&wc);
//创建窗口并将结果用作句柄
hWnd=CreateWindowEx(空,
L“WindowClass1”,//窗口类的名称
L“游戏”//窗口的标题
WS\u重叠窗口,//窗口样式
1,//窗口的x位置
1,//窗口的y位置
1800,//窗户的宽度
1000,//窗户的高度
NULL,//我们没有父窗口,NULL
NULL,//我们没有使用菜单,NULL
hInstance,//应用程序句柄
NULL);//用于多个窗口,NULL
//在屏幕上显示窗口
显示窗口(hWnd、nCmdShow);
//进入主循环:
//此结构保存Windows事件消息
味精;
//等待队列中的下一条消息,将结果存储在“msg”中
while(GetMessage(&msg,NULL,0,0))
{
//将击键消息转换为正确的格式
翻译信息(&msg);
//将消息发送到WindowProc函数
发送消息(&msg);
}
//将WM_退出消息的这一部分返回到Windows
返回msg.wParam;
}
//这是程序的主要消息处理程序
LRESULT回调WindowProc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM)
{
//对给定的消息进行排序并找到要运行的代码
开关(信息)
{
//此消息在窗口关闭时读取
案例WM_MOUSEMOVE:
{
//检索鼠标屏幕位置
INTX=(短)低阶(lParam);
int y=(短)HIWORD(lParam);
//检查左按钮是否按下:
bool leftButtonDown=wParam和MK_按钮;
//检查右键是否按下:
bool rightButtonDown=wParam&mkrbutton;
如果(leftButtonDown==true)
{
//左键单击
//示例:按w键时关闭程序
PostQuitMessage(0);
返回0;
}
}
案例WM_键控:
{
交换机(wParam)
{
案例“W”:
//w压
//示例:按w键时关闭程序
PostQuitMessage(0);
返回0;
}
}
案例WM_销毁:
{
//完全关闭应用程序
PostQuitMessage(0);
返回0;
}
违约:
打破
}
//处理switch语句未处理的任何消息
返回DefWindowProc(hWnd、message、wParam、lParam);
}
您的开关
语句中没有中断
你最终会被逐出家门
PostQuitMessage(0);
你可以这样做:
case WM_FOO:
{
if ( bar ) {
return 0;
}
break;
}
您的switch
语句中没有break
你最终会被逐出家门
PostQuitMessage(0);
你可以这样做:
case WM_FOO:
{
if ( bar ) {
return 0;
}
break;
}
您的交换机中缺少一些break
语句,因此,例如,如果您收到WM\u MOUSEMOVE
消息和leftButtonDown!=如果为true
,则执行将通过WM_KEYDOWN
,等等
最终,你会得到一个案例WM\u DESTROY:
,它将向你发布一条可爱的退出消息
另一方面,通过在调试器中逐个语句单步执行,很容易发现这一点。您的开关中缺少一些break
语句,例如,如果您收到WM\u MOUSEMOVE
消息和leftButtonDown!=如果为true
,则执行将通过WM_KEYDOWN
,等等
最终,你会得到一个案例WM\u DESTROY:
,它将向你发布一条可爱的退出消息
另一方面,通过在调试器中逐个语句单步执行,很容易发现这一点。如果不通过WM\u MOUSEMOVE
消息检测单击,请使用WM\u MOUSEDOWN
问题是,您的代码可能是通过单击某个东西启动的,因此当您的窗口收到第一条WM_MOUSEMOVE
消息时,按钮实际上仍然被按下。代码运行速度比手指快得多。不要通过WM\u MOUSEMOVE
消息检测点击,而是使用WM\u MOUSEDOWN
问题是,您的代码可能是通过单击某个东西启动的,因此当您的窗口收到第一条WM_MOUSEMOVE
消息时,按钮实际上仍然被按下。代码运行速度比手指快得多。旁注,有些注释是不正确的。样式注释:编写if(condition)
,而不是if(condition==true)
。请尝试删除此项:“return DefWindowProc(hWnd,message,wParam,lParam);”这有什么区别吗?旁注,有些评论是不正确的。风格评论: