Win32 Combobox,打印所选的名称以编辑文本框

时间:2018-01-13 06:44:05

标签: c++ winapi combobox textbox

我正在为win32测试Combobox的理论。已经尝试了很多东西,但我不知道如何让它做我想做的事。当在组合框中进行选择时,我希望它打印到编辑框中。我相信这可以通过手柄完成。

因此,如果我通过按钮1的演员表将它添加到我的按钮1,你如何将其打印到编辑字段?

我的代码如下。我留下了一些我尝试过的东西但是对它进行了评论。问题是添加控件,我在想我的开关和案例。

我错过了将文本填充到编辑框的一部分。

对于这种类型的编程而言,我对编程并不是一个新手。我正在寻找一种简单的方法。在此先感谢您的时间。

以下是更新后的代码以及我现在所获得的内容。我遵循unicode指南。我甚至和visual studio c ++结交了朋友(我想)。

#include <windows.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#define OPTINBT1 1
#define OPTINBT2 2
#define COMBO1 3

HWND hWnd, hComboOne;

void addControl(HWND);

LPCWSTR egClassName = L"myWindowClass";

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{

    WNDCLASSW wc = { 0 };

    wc.lpszClassName = egClassName;
    wc.lpfnWndProc = WindowProcedure;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    if (!RegisterClassW(&wc))
    {
        const wchar_t  Error01[] = L"Register Issue To Check On : ";
        const wchar_t  Error01_Caption[] = L"Error 01";

        MessageBoxW(hWnd, Error01, Error01_Caption, MB_OK | MB_ICONERROR);

        return 0;
    }

    LPCWSTR parentWinTitle = L"My Window";

    hWnd = CreateWindowW(egClassName, parentWinTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500, NULL, NULL, NULL, NULL);

    if (hWnd == NULL)
    {

        const wchar_t Error02[] = L"Window Creation Issue To Check On : ";
        const wchar_t Error02_Caption[] = L"Window Creation Issue To Check On : ";
        MessageBoxW(hWnd, Error02, Error02_Caption, MB_OK | MB_ICONERROR);

    }
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    MSG msg = { 0 };

    while (GetMessage(&msg, NULL, 0, 0))
    {

        TranslateMessage(&msg);
        DispatchMessage(&msg);

    }

    return 0;

}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{

    switch (msg)
    {
    case WM_CREATE:
        addControl(hWnd);
        break;

    case WM_COMMAND:

        if (HIWORD(wp) == CBN_SELCHANGE)
        {
            if (LOWORD(wp) == COMBO1)
            {
                HWND hcombo = (HWND)lp;
                LRESULT index = SendMessageW(hcombo, CB_GETCURSEL, (WPARAM)0, (LPARAM)0);

                wchar_t buf[256];

                SendMessageW(hcombo, (UINT)CB_GETLBTEXT, (WPARAM)index, (LPARAM)buf);
                MessageBoxW(hWnd, buf, L"Good Example", 0);
            }
            break;
        }
        switch (LOWORD(wp))
        {
        case OPTINBT1:
            MessageBoxW(hWnd, L"This is Radio button1: ", L"Radio Button 2 is good", MB_OK);
            break;
        case OPTINBT2:
            MessageBoxW(hWnd, L"This is Radio button2: ", L"Radio Button 1 is good", MB_OK);
            break;
        default:break;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }

    return 0;
}

void addControl(HWND hWnd)
{
    HWND OptBt1, OptBt2;

    const LPCWSTR cont1 = L"STATIC";
    const LPCWSTR cont2 = L"COMBOBOX";
    const LPCWSTR cont3 = L"BUTTON";
    const LPCWSTR emptyS = L"";
    const LPCWSTR bl = L"RButton 1";
    const LPCWSTR b2 = L"RButton 2";
    //Option buttons
    OptBt1 = CreateWindowW(cont3, bl, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 8, 90, 25, hWnd, (HMENU)OPTINBT1, NULL, NULL);
    OptBt2 = CreateWindowW(cont3, b2, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 40, 90, 25, hWnd, (HMENU)OPTINBT2, NULL, NULL);
    SendMessage(OptBt1, BM_SETCHECK, BST_CHECKED, 0);

    hComboOne = CreateWindowW(cont2, emptyS, WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_HASSTRINGS | WS_VSCROLL, 77, 70, 150, 150, hWnd, (HMENU)COMBO1, 0, 0);

    LPCWSTR ComboBoxItems[] = { L"Subject1", L"Subject2", L"Subject3",
        L"Subject4", L"Subject5" };

    /** Or the array way */
    SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[0]);
    SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[1]);
    SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[2]);
    SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[3]);
    SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[4]);

}

2 个答案:

答案 0 :(得分:4)

应在WM_CREATE之前插入

addControl

WM_COMMAND回复CBN_SELCHANGE通知以检测组合框选择更改。

当您显示消息框时,您可以使用您自己的窗口MessageBox(hWnd,...)的句柄。如果您提供NULL作为句柄,则消息框将成为桌面窗口的子项,其行为就像在无模式模式下显示一样。

switch(msg)
{
case WM_CREATE:
    addControl(hWnd);
    break;

case WM_COMMAND:
    if(HIWORD(wp) == CBN_SELCHANGE)
    {
        if(LOWORD(wp) == COMBO1)
        {
            HWND hcombo = (HWND)lp;
            int index = SendMessage(hcombo, CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
            char buf[256];
            SendMessage(hcombo, (UINT)CB_GETLBTEXT, (WPARAM)index, (LPARAM)buf);
            MessageBox(hWnd, buf, 0, 0);
        }
        break;
    }

    switch(LOWORD(wp))
    {
    case OPTINBT1:
        MessageBox(hWnd, "This is Radio button1: ", "ComboBox Working??", MB_OK);
        break;
    case OPTINBT2:
        MessageBox(hWnd, "This is Radio button2: ", "ComboBox Working??", MB_OK);
        break;
    default:break;
    }
    break;

与您的问题无关,但您正在将Unicode(示例L"EDIT")与ANSI(示例"EDIT")混合使用。

CreateWindow这样的窗口函数是宏。在ANSI中,它是CreateWindowA,在Unicode中,它是宽函数CreateWindowW

您正在以旧ANSI模式编译程序。 CreateWindowCreateWindowA的宏,需要ANSI输入。 CreateWindow("EDIT",...)

如果用Unicode编译程序,CreateWindow是宽函数CreateWindowW的宏,它使用Unicode参数CreateWindow(L"EDIT",...)

如果在Visual Studio中创建项目,则默认使用新的Unicode标准。在任何地方使用宽字符(L"Text")和Unicode函数。我建议用警告级别4编译程序。确保程序编译时没有警告。

答案 1 :(得分:3)

您想要回复发送到您的窗口过程的CBN_SELCHANGE消息。在WM_COMMAND的情况下,wParam的低位字将包含控制ID,高位字将包含可选的命令代码。

该命令代码对于正确响应组合框消息至关重要。您需要通过向CBN_SELCHANGE然后CB_GETCURSELCB_GETLBTEXTLEN发送组合框来回复CB_GETLBTEXT消息,之后您可以向编辑控件发送{{1检索到的文本。