如何使用不同的项目串创建两个不同的组合框?

时间:2016-01-30 00:45:10

标签: c++ winapi codeblocks

我创建了一个包含项目列表的组合框。如何使用不同的项目字符串创建另一个?我是否可以改变hWnd,因为它似乎第一个已经拥有hWndCombobxes。因此,当我将它应用于第二个时,我收到一条错误消息,表明存在重复值。 以下是我的代码。我应该叫什么功能呢?

case WM_CREATE: {

        HWND hWndComboBox =    CreateWindow (TEXT("COMBOBOX"), TEXT (""),
               CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
                100, 150, 200, 150,
                hwnd ,(HMENU) ID_COMBOBOX 1, NULL, NULL);

        //  ADD 2 ITEMS
        SendMessage  (
            hWndComboBox,
            (UINT) CB_ADDSTRING,
            (WPARAM) 0, (LPARAM) TEXT ("Item 1"));

        SendMessage  (
            hWndComboBox ,
            (UINT) CB_ADDSTRING,
            (WPARAM) 0, (LPARAM)  TEXT ("Item 2"));

        //  SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD
        SendMessage (hWndComboBox , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);

2 个答案:

答案 0 :(得分:0)

// put this declaration somewhere up (or better move it to an include file)
#define ID_COMBOBOX_1     1001   
#define ID_COMBOBOX_2     1002
// end defines

case WM_CREATE: {


    // it is preferably to use SendDlgItemMessage instead of SendMessage
    // this make things easier
    // you will not need combobox's HWND, just ComboBox ID

    TCHAR *Combo_1_Data[]={
        TEXT("Item 1"),
        TEXT("Item 2")
    };

    TCHAR *Combo_2_Data[]={
        TEXT("Element 1"),
        TEXT("Element 2")
    };


    int i;

    // create two different ComboBoxs
    CreateWindow (TEXT("COMBOBOX"), TEXT (""),
        CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
        100, 150, 200, 150,
        hwnd ,(HMENU) ID_COMBOBOX_1, NULL, NULL);

    CreateWindow (TEXT("COMBOBOX"), TEXT (""),
        CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
        208, 150, 200, 150,
        hwnd ,(HMENU) ID_COMBOBOX_2, NULL, NULL);


    // Fill first Combo with its Data
    for( i = 0 ; i < (sizeof(Combo_1_Data) / sizeof(Combo_1_Data[0]) ) ; i++ ){

        SendDlgItemMessage  (hwnd,ID_COMBOBOX_1 ,CB_ADDSTRING, 0, (LPARAM) Combo_1_Data[i]);
    }
    SendDlgItemMessage (hwnd, ID_COMBOBOX_1  , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);


   // Fill second Combo with its Data
    for( i = 0 ; i < (sizeof(Combo_2_Data) / sizeof(Combo_2_Data[0] )) ; i++ ){

        SendDlgItemMessage  (hwnd,ID_COMBOBOX_2 ,CB_ADDSTRING, 0, (LPARAM) Combo_2_Data[i]);
    }
    SendDlgItemMessage (hwnd, ID_COMBOBOX_2  , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);

答案 1 :(得分:-2)

复制并粘贴其他组合框。为发送消息&#39;做同样的事情。功能。然后将第二个Combobox的hWnd更改为hWndListBox。对发送消息&#39;。

执行相同的操作
 HWND hWndListBox  = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
                    CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD  ,
                    100, 70, 200, 90,
                    hwnd ,(HMENU) NULL, NULL, NULL);

                 HWND hWndComboBox =    CreateWindow (TEXT("COMBOBOX"), TEXT (""),
                   CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
                    100, 150, 200, 100,
                    hwnd ,(HMENU) NULL, NULL, NULL);

    SendMessage  (
            hWndComboBox ,
            (UINT) CB_ADDSTRING,
            (WPARAM) 0, (LPARAM)  TEXT ("Item 2"));


     SendMessage  (
            hWndListBox ,
            (UINT) CB_ADDSTRING,
            (WPARAM) 0, (LPARAM) TEXT ("Item 1"));


    //  SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD


    SendMessage (hWndComboBox , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);
    SendMessage (hWndListBox    , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);