WINDOWS API,CreateWindowW,WM_KEYDOWN,VK_ESCAPE, SendMessage(hwnd, WM_CLOSE, 0, 0)

67 篇文章 0 订阅
[cpp]  view plain copy
  1. #include <windows.h>  
  2.   
  3. LRESULT CALLBACK WndProc(HWNDUINTWPARAMLPARAM);  
  4. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,   
  5.                     PWSTR pCmdLine, int CmdShow)  
  6. {  
  7.   MSG  msg;      
  8.   WNDCLASSW wc = {0};  
  9.   wc.lpszClassName = L"Escape";  
  10.   wc.hInstance     = hInstance;  
  11.   wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);  
  12.   wc.lpfnWndProc   = WndProc;  
  13.   wc.hCursor       = LoadCursor(0, IDC_ARROW);  
  14.       
  15.   RegisterClassW(&wc);  
  16.   CreateWindowW(wc.lpszClassName, L"Escape按下键盘ESC键",  
  17.                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,  
  18.                 100, 100, 300, 180, 0, 0, hInstance, 0);    
  19.   
  20.   while( GetMessage(&msg, NULL, 0, 0)) {  
  21.       TranslateMessage(&msg);  
  22.       DispatchMessage(&msg);  
  23.   }  
  24.   
  25.   return (int) msg.wParam;  
  26. }  
  27.   
  28. LRESULT CALLBACK WndProc( HWND hwnd, UINT msg,   
  29.     WPARAM wParam, LPARAM lParam )  
  30. {      
  31.   switch(msg)    
  32.   {  
  33.     case WM_KEYDOWN:  
  34.   
  35.       if (wParam == VK_ESCAPE) { //按下键盘ESC键  
  36.           int ret = MessageBoxW(NULL, L"Are you sure to quit?",   
  37.                                   L"Message", MB_OKCANCEL);  
  38.           if ( ret == IDOK) {  
  39.                 SendMessage(hwnd, WM_CLOSE, 0, 0);  
  40.           }  
  41.       }  
  42.       break;  
  43.   
  44.     case WM_DESTROY:  
  45.   
  46.         PostQuitMessage(0);  
  47.       break;   
  48.   }  
  49.   
  50.   return DefWindowProcW(hwnd, msg, wParam, lParam);  
  51. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值