如何检查按钮是否被按下

时间:2013-05-13 15:37:48

标签: c++ winapi button

我的目标是使用一堆不同的子控件填充窗口的客户区。像按钮复选框和静态文本之类的东西,但我很难弄清楚如何捕捉按钮点击的消息。

我的代码:

#include <windows.h>
#include <CommCtrl.h>
#include "resource.h"

HMENU        BUTTON1;

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpszCmdParam, int nCmdShow)
     {
     static char szAppName[] = "Keyboarding" ;
     HWND        hwnd;
     MSG         msg;   
     WNDCLASS    wndclass; 
     HMENU       hMenu;


     hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1));

     if (!hPrevInstance)
          {
          wndclass.style         = CS_HREDRAW | CS_VREDRAW;
          wndclass.lpfnWndProc   = WndProc;
          wndclass.cbClsExtra    = 0;
          wndclass.cbWndExtra    = 0;
          wndclass.hInstance     = hInstance ;
          wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
          wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
          wndclass.hbrBackground = static_cast<HBRUSH>(GetStockObject (WHITE_BRUSH));
          wndclass.lpszMenuName  = NULL ;
          wndclass.lpszClassName = szAppName ;

          RegisterClass (&wndclass) ;
      }

     hwnd = CreateWindow (szAppName,         // window class name
                    "Lab 6",   // window caption
                    WS_OVERLAPPEDWINDOW,     // window style
                    CW_USEDEFAULT,           // initial x position
                    CW_USEDEFAULT,           // initial y position
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,                // hmmmmm???
                    NULL,                    // parent window handle
                    hMenu,                    // window menu handle
                    hInstance,               // program instance handle
                  NULL) ;              // creation parameters

     HWND button1 = CreateWindow( WC_BUTTON, 
                         "Push Button", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         50, 
                         100, 
                         24, 
                         hwnd, 
                         BUTTON1, 
                         hInstance,
                         NULL);

     HWND button2 = CreateWindow( WC_BUTTON, 
                         "Auto Check Button?", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         50, 
                         100, 
                         24, 
                         hwnd, 
                         NULL, 
                         hInstance,
                         NULL);

     HWND button3 = CreateWindow( WC_BUTTON, 
                         "Push Button", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         50, 
                         100, 
                         24, 
                         hwnd, 
                         NULL, 
                         hInstance,
                         NULL);

     HWND editControl = CreateWindow( WC_BUTTON, 
                         "Push Button", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         50, 
                         100, 
                         24, 
                         hwnd, 
                         NULL, 
                         hInstance,
                         NULL);

     HWND StaticControl = CreateWindow( WC_STATIC, 
                         "Hello World", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         100, 
                         100, 
                         24, 
                         hwnd, 
                         NULL, 
                         hInstance,
                         NULL);

     HWND ListBox = CreateWindow( WC_BUTTON, 
                         "Push Button", 
                         WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
                         50, 
                         50, 
                         100, 
                         24, 
                         hwnd, 
                         NULL, 
                         hInstance,
                         NULL);

     ShowWindow (hwnd, nCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
          {
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
          }
     return (int) msg.wParam ;
     }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam,
                            LONG lParam)
     {
     HDC         hdc ;
     PAINTSTRUCT ps ;
     TEXTMETRIC  tm;
     RECT        rClientRect;
     RECT        rWindowRect;
     SIZE        size;
     int        cButtons = 0;

     switch (message)
          {         

     case WM_COMMAND:
         switch(LOWORD(wParam))
         {
         case BUTTON1:
             break;
         }      

          case WM_PAINT:
                hdc = BeginPaint (hwnd, &ps) ;

                EndPaint (hwnd, &ps) ;
                return 0 ;

          case WM_DESTROY:
                PostQuitMessage (0) ;
                return 0 ;
          }

     return DefWindowProc (hwnd, message, wParam, lParam) ;
     }

我正试着抓住我的第一个按钮labaled button1并且有“ID”BUTTON1。我试过在我的WM_COMMAND中捕获它们,它说我的BUTTON1未被识别。

1 个答案:

答案 0 :(得分:3)

您正在寻找的是:

case WM_COMMAND:
    switch(LOWORD(wParam)) {
         case BUTTON1: {

但在它识别BUTTON1之前,你必须以一种消息处理程序可以识别它的方式声明它:例如,使它成为一个全局变量,而不是在WinMain中声明它:

HMENU       BUTTON1;

int WINAPI WinMaietcetc..

让我知道这是否有效!