首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

【API】怎么响应RadioButton被点击的的消息

2012-10-15 
【求助API】如何响应RadioButton被点击的的消息?主窗口上创建一个GroupBox,再在其基础上创建了两个RadioButt

【求助API】如何响应RadioButton被点击的的消息?
主窗口上创建一个GroupBox,再在其基础上创建了两个RadioButton,问要如何响应这两个RadioButton被点击的的消息

我试了很多方法都不行,是不是要写子窗口过程函数?有没有不写子窗口过程函数的方法。

C/C++ code
#include <windows.h>LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int iCmdShow){    static TCHAR szAppName[] = TEXT("TEST");    HWND hwnd;    MSG msg;    WNDCLASS wndclass;    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 = NULL;    wndclass.lpszMenuName = NULL;    wndclass.lpszClassName = szAppName;    if (!RegisterClass(&wndclass))        return 0;    hwnd = CreateWindow(szAppName, TEXT("TEST0"),                        WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX,                        150, 150, 400, 400,                        NULL, NULL, hInstance, NULL);    ShowWindow(hwnd, iCmdShow);    UpdateWindow(hwnd);    while (GetMessage(&msg, NULL, 0, 0))    {        TranslateMessage(&msg);        DispatchMessage(&msg);    }    return (int(msg.wParam));}LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    static HWND hGroupBox, hRadioButton1, hRadioButton2;    RECT rect;    HDC hdc;    PAINTSTRUCT ps;    switch (message)    {    case WM_CREATE:        hGroupBox = CreateWindow(TEXT("button"), TEXT("GroupBox"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX,                                 50, 50, 300, 200,                                 hwnd, (HMENU)5000, ((LPCREATESTRUCT)lParam)->hInstance, NULL);        hRadioButton1 = CreateWindow(TEXT("button"), TEXT("RadioButton1"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP,                                     50, 50, 150, 50,                                     hGroupBox, (HMENU)5001, ((LPCREATESTRUCT)lParam)->hInstance, NULL);        hRadioButton2 = CreateWindow(TEXT("button"), TEXT("RadioButton2"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,                                     50, 100, 150, 50,                                     hGroupBox, (HMENU)5002, ((LPCREATESTRUCT)lParam)->hInstance, NULL);        SendMessage(hRadioButton1, BM_SETCHECK, (WPARAM)TRUE, 0);        return 0;    case WM_PAINT:        hdc = BeginPaint(hwnd, &ps);        GetClientRect(hwnd, &rect);        EndPaint(hwnd, &ps);        return 0;    case WM_COMMAND:        switch (LOWORD(wParam))        {        case 5000:            MessageBox(hwnd, TEXT("GroupBox"), TEXT("Caption"), MB_OK);            return 0;        case 5001:            MessageBox(hwnd, TEXT("RadioButton1"), TEXT("Caption"), MB_OK);            return 0;        case 5002:            MessageBox(hwnd, TEXT("RadioButton2"), TEXT("Caption"), MB_OK);            return 0;        default:            return DefWindowProc(hwnd, message, wParam, lParam);        }        return 0;    case WM_DESTROY:        PostQuitMessage(0);        return 0;    }    return DefWindowProc(hwnd, message, wParam, lParam);}


[解决办法]
CSDN不能编辑帖子真烦人
C/C++ code
        hRadioButton1 = CreateWindow(TEXT("button"), TEXT("RadioButton1"),                                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP,                                     50, 50, 150, 50,                                     hwnd, (HMENU)5001,                                     ((LPCREATESTRUCT)lParam)->hInstance, NULL);        hRadioButton2 = CreateWindow(TEXT("button"), TEXT("RadioButton2"),                                     WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,                                     50, 100, 150, 50,                                     hwnd, (HMENU)5002,                                     ((LPCREATESTRUCT)lParam)->hInstance, NULL); 


[解决办法]
SetWindowLong来处理默认情况下不能自行处理的消息
下面的代码是我之前的程序里面用到的,是为了自行处理WM_LBUTTONDOWN,你修改一下处理WM_COMMAND,BN_CLICKED。

C/C++ code
WNDPROC BProc_LBDOWN1LRESULT CALLBACK ButtonLBDown(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam) {    WNDPROC OldDlgProc = NULL;    if (GetDlgItem(GetParent(hwnd),IDC_BUTTON_LBDOWN1) == hwnd)//LButtonDown1的消息影射处理    {        OldDlgProc = BProc_LBDOWN1;        switch(msg)         {         case WM_LBUTTONDOWN:            TRACE(L"Left Button 1 Down\n");            break;        }    }    else        return 0;     return CallWindowProc(OldDlgProc,hwnd,msg,wParam,lParam); } 

热点排行