使用单选按钮切换主视图中的不同视图 window

Use radio buttons for switching different views in the main window

我在 Win32/C++ 中有一个应用程序,它的三个按钮位于主 window 的左侧。单击其中一个时,必须显示一组单选按钮,以允许用户在不同的子菜单之间切换视图。

代码如下所示:

case WM_CREATE:
    {

        HWND hWndButton=CreateWindowEx(NULL,
            "BUTTON",
            "Inserting",
            WS_TABSTOP|WS_VISIBLE|
            WS_CHILD|BS_DEFPUSHBUTTON,
            10,
            10,
            100,
            24,
            hWnd,
            (HMENU)INSERT_BUTTON,
            GetModuleHandle(NULL),
            NULL);

        HWND hWndButton2=CreateWindowEx(NULL,
            "BUTTON",
            "Listing",
            WS_TABSTOP|WS_VISIBLE|
            WS_CHILD|BS_DEFPUSHBUTTON,
            10,
            60,
            100,
            24,
            hWnd,
            (HMENU)LIST_BUTTON,
            GetModuleHandle(NULL),
            NULL);

        HWND hWndButton3=CreateWindowEx(NULL,
            "BUTTON",
            "Consulting",
            WS_TABSTOP|WS_VISIBLE|
            WS_CHILD|BS_DEFPUSHBUTTON,
            10,
            110,
            100,
            24,
            hWnd,
            (HMENU)SELECT_BUTTON,
            GetModuleHandle(NULL),
            NULL);
    }
    break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case INSERT_BUTTON:
            {
                 HWND hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
                "BUTTON",
                "Select a table:", 
                WS_VISIBLE | WS_CHILD| BS_GROUPBOX,  // Styles 
                150,30,470,70, 
                hWnd, 
                NULL,
                GetModuleHandle(NULL), NULL);

                 HWND hwndCB1 = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "TSector", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_GROUP, 
                                                160, 60, 80, 20, hWnd,(HMENU)CB1, GetModuleHandle(NULL), NULL);

                 HWND hwndCB2 = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "TSeccio", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON , 
                                                250, 60, 80, 20, hWnd,(HMENU)CB2, GetModuleHandle(NULL), NULL);

                 HWND hwndCB3 = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "TActivitat", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 
                                                340, 60, 80, 20, hWnd,(HMENU)CB3, GetModuleHandle(NULL), NULL);

                 HWND hwndCB4 = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "TClasse", WS_VISIBLE | WS_CHILD |  BS_AUTORADIOBUTTON, 
                                                430, 60, 80, 20, hWnd,(HMENU)CB4, GetModuleHandle(NULL), NULL);

                 HWND hwndCB5 = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "TQuantitat", WS_VISIBLE | WS_CHILD |  BS_AUTORADIOBUTTON, 
                              520, 60, 85, 20, hWnd,(HMENU)CB5, GetModuleHandle(NULL), NULL);

                 switch(HIWORD(wParam))
                 {
                        case CB1:
                             //Do something
                             break;            
                 }                

            }
            break;
        }
        break;

也许创建单选按钮后的切换没有很好地实现,但无论如何,在编译时,无法识别我想要在单击每个单选按钮时显示的任何菜单:我正在检索的编译错误说我需要声明每个菜单。 这对我来说听起来很奇怪,因为它适用于其他情况。 任何帮助将非常感激。

CB1 是您的控件的标识符,如果未声明则只需使用#define 声明,如下所示:

#define CB1 1000

标识符不能与其他控件的id重复

创建菜单的一个好方法是创建隐藏的所有子菜单,当有人单击某个选项时,您只需使用 ShowWindow 函数显示子菜单。

要创建隐藏的子菜单,只需删除 CreateWindowEx 中的 WS_VISIBLE。

看这个例子:

// Opts
#define Fruits 1
#define Colors 2
// Fruits
#define Apple 5
#define Pineapple 6
// Colors
#define Red 10
#define Blue 11

void HideAll(HWND hwnd)
{
    // Fruits
    ShowWindow(GetDlgItem(hwnd, Apple), SW_HIDE);
    ShowWindow(GetDlgItem(hwnd, Pineapple), SW_HIDE);
    // Colors
    ShowWindow(GetDlgItem(hwnd, Red), SW_HIDE);
    ShowWindow(GetDlgItem(hwnd, Blue), SW_HIDE);
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            // Menus - All created visible
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Fruits", WS_VISIBLE | WS_CHILD,
                                            10, 10, 80, 20, hwnd,(HMENU)Fruits,          GetModuleHandle(NULL), NULL);
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Colors", WS_VISIBLE | WS_CHILD,
                                            10, 40, 80, 20, hwnd,(HMENU)Colors, GetModuleHandle(NULL), NULL);
            // Fruits - All created Invisible
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Apple",  WS_CHILD,
                                            100, 10, 80, 20, hwnd,(HMENU)Apple, GetModuleHandle(NULL), NULL);
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Pineapple",  WS_CHILD,
                                            100, 40, 80, 20, hwnd,(HMENU)Pineapple, GetModuleHandle(NULL), NULL);
            // Colors - All created Invisible
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Red",  WS_CHILD,
                                            100, 10, 80, 20, hwnd,(HMENU)Red, GetModuleHandle(NULL), NULL);
            CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Blue",  WS_CHILD,
                                            100, 40, 80, 20, hwnd,(HMENU)Blue, GetModuleHandle(NULL), NULL);

            break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case Fruits:
                // Hide old
                HideAll(hwnd);
                // Show new
                ShowWindow(GetDlgItem(hwnd, Apple), SW_SHOW);
                ShowWindow(GetDlgItem(hwnd, Pineapple), SW_SHOW);
                break;
            case Colors:
                // Hide old
                HideAll(hwnd);
                // Show new
                ShowWindow(GetDlgItem(hwnd, Red), SW_SHOW);
                ShowWindow(GetDlgItem(hwnd, Blue), SW_SHOW);
                break;
            }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

这是程序的结果:

当有人点击水果时,它会显示水果子选项:

当有人点击颜色时,ShowWindow 会隐藏旧的子选项并显示新的子选项: