为什么SetWindowPos不会更改ComboBoxEx下拉高度?

时间:2015-09-07 21:19:21

标签: visual-studio-2010 winapi combobox controls

我正在使用图像测试ComboBoxEx控件。我使用过microsoft example而没有任何重大改变。它工作,但组合框下拉列表不显示列表!实际上,SetWindowPos函数无论如何都不会改变ComboBoxEx下拉高度! 我在window7sp1x64中使用VS2010sp1,release,win32和Common Control 6.0 pragma。完全显示下拉列表的唯一方法是将CreateWindowEx中的ComboboxEx高度更改为一个很大的值! 如何以编程方式更改ComboBoxEx下拉列表?

我想创建一个组合框,一起显示文本和图像。我完全使用了以下代码:

HWND *CreateComboBoxEX(HWND hwndParent)
{
    HWND hwnd;
    INITCOMMONCONTROLSEX icex;

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_USEREX_CLASSES;

    InitCommonControlsEx(&icex);

    hwnd = CreateWindowEx(0, WC_COMBOBOXEX, L"", 
    WS_BORDER | WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 0, 0, 200, 40,
    hwndParent, (HMENU)NULL, GetModuleHandle(0), NULL);
    if (hwnd == NULL)
        return(NULL);
    return(hwnd);
}

BOOL WINAPI AddItems(HWND hwndCB)
{

//  Declare and init locals.
COMBOBOXEXITEM cbei;
int iCnt;


typedef struct {
    int iImage;
    int iSelectedImage;
    int iIndent;
    LPTSTR pszText;
} ITEMINFO, *PITEMINFO;

ITEMINFO IInf[ ] = {
    { 0, 3,  0, L"first"}, 
    { 1, 4,  1, L"second"},
    { 2, 5,  2, L"third"},
    { 0, 3,  0, L"fourth"},
    { 1, 4,  1, L"fifth"},
    { 2, 5,  2, L"sixth"},
    { 0, 3,  0, L"seventh"},
    { 1, 4,  1, L"eighth"},
    { 2, 5,  2, L"ninth"},
    { 0, 3,  0, L"tenth"},
    { 1, 4,  1, L"eleventh"},
    { 2, 5,  2, L"twelfth"},
    { 0, 3,  0, L"thirteenth"},
    { 1, 4,  1, L"fourteenth"},
    { 2, 5,  2, L"fifteenth"}
};

// Set the mask common to all items.
cbei.mask = CBEIF_TEXT | CBEIF_INDENT |
            CBEIF_IMAGE| CBEIF_SELECTEDIMAGE;

for(iCnt=0;iCnt<MAX_ITEMS;iCnt++){
    // Initialize the COMBOBOXEXITEM struct.
    cbei.iItem          = iCnt;
    cbei.pszText        = IInf[iCnt].pszText;
    cbei.cchTextMax     = sizeof(IInf[iCnt].pszText);
    cbei.iImage         = IInf[iCnt].iImage;
    cbei.iSelectedImage = IInf[iCnt].iSelectedImage;
    cbei.iIndent        = IInf[iCnt].iIndent;


    // Tell the ComboBoxEx to add the item. Return FALSE if 
    // this fails.
    if(SendMessage(hwndCB,CBEM_INSERTITEM,0,(LPARAM)&cbei) == -1)
        return FALSE;
}
// Assign the existing image list to the ComboBoxEx control 
// and return TRUE. 
// g_himl is the handle to the existing image list
SendMessage(hwndCB,CBEM_SETIMAGELIST,0,(LPARAM)g_himl);

// Set size of control to make sure it's displayed correctly now
// that the image list is set.
SetWindowPos(hwndCB,NULL,20,20,250,120,SWP_NOACTIVATE);

return TRUE; 
}

在WindowProc的WM_CREATE部分:

himglist = ImageList_Create(24, 24, ILC_COLOR32 | ILC_MASK, 0, 0);
hBitmap = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_IMGLIST));
ImageList_Add(himglist, hBitmap, 0);
DeleteObject(hBitmap);
hwndComboBoxEx = CreateComboBox(hWnd);
AddItems(hwndComboBoxEx);

程序显示带有文本和图像的组合框,当我点击它时,它没有显示下拉列表。我想完全看到下拉列表。 我检查了SetWindowPos。它会改变ComboBoxEx宽度,但不会改变高度! 我糊涂了!怎么了?

1 个答案:

答案 0 :(得分:3)

ComboBoxEx有一个子窗口,它是一个常规的ComboBox,有些消息无法正常传递。我认为这也取决于Window版本。

您应该在创建控件时设置高度。同时删除指针。

HWND CreateComboBoxEX(HWND hwndParent)
{
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_USEREX_CLASSES;
    InitCommonControlsEx(&icex);

    //*** put the correct height in here: *** 
    HWND hwnd = CreateWindowEx(0, WC_COMBOBOXEX, L"", 
            WS_BORDER | WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 0, 0, 200, 120, 
            hwndParent, (HMENU)NULL, GetModuleHandle(0), NULL);

    return(hwnd);
}

稍后您可以使用SetWindowPos更改X / Y位置和宽度。

另一种方法:

使用SetWindowPos更改X / Y位置和ComboBoxEx的宽度。然后调整ComboBoxEx的子项的大小以设置正确的高度。

//height will have no effect here
SetWindowPos(hwndCB, NULL, 20, 20, 250, 120, SWP_NOACTIVATE);

HWND child = (HWND)SendMessage(hwndCB, CBEM_GETCOMBOCONTROL, 0, 0);
if (child)
{
    //Resize using the same width, but different height. And don't move. 
    SetWindowPos(child, NULL, 0, 0, 250, 120, SWP_NOMOVE);
}