0

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

我的代码:

#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) ;
     }

我试图抓住我的第一个按钮标签 button1 并具有“ID”BUTTON1。我试过在我的 WM_COMMAND 中捕获它们,它说我的 BUTTON1 身份不明。

4

1 回答 1

3

您正在寻找的是:

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

但在它识别 BUTTON1 之前,您必须以消息处理程序可以识别的方式声明它:例如,将其设为全局变量,而不是在 WinMain 中声明它:

HMENU       BUTTON1;

int WINAPI WinMaietcetc..

让我知道这个是否奏效 !

于 2013-05-13T15:55:29.367 回答