开发者

Change height of COMBOBOX

How can I change the height of a COMBOBOX control created with a resource-definition at runtime, so that I can insert new strings in the combobox? The string insertion code is working but only if I set a fixed height for the combobox in the resource-definition (e.g. 28 units). But this is not convenient, because the number of strings is dynamic.

I know that I can create the dialog at runtime, but then I can't use dialog units, and resources are much more efficient...

Here are simplified versions of my code.

Resource file:

IDD_SETTINGS DIALOG 0, 0, 100, 100
BEGIN
    COMBOBOX IDC_COMBO, 0, 0, 100, 14, CBS_DROPDOWNLIST
END

Window procedure for main window and dialog:

BOOL CALLBACK WndProcSettings(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_INITDIALOG:
            //...
            break;
        default:
            return FALSE;
    }
    return TRUE;
}

LRESULT CALLBACK WndProcMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_COMMAND:
            switch (LOWORD(wParam)) {
                case IDC_SETTINGS:
                    DialogBox(hInstance, MAKEINTRESOURCE(IDD_SETTINGS), hWnd, WndProcSettings);
                    break;
            }
            break;
        default:
            return DefWindowProc(hWnd, message, wPar开发者_如何转开发am, lParam);
    }
    return(0L);
}


I assume you are referring to the height of the dropdown portion of the combobox.

You can still work with Dialog Units, take a look at GetDialogBaseUnits which will return the number of pixels per dialog unit. If you are working with a non-system font the following KB article details the calculations - How To Calculate Dialog Base Units with Non-System-Based Font.

You can programatically change the size of the combobox by using SetWindowPos.


In the meantime, I found a solution. Here is what I'm using now. I set the height for the combobox in the resource file to 14 DLU's (height of one item), so that the new height is calculated correctly. Using GetClientRect I get this height, and convert it to pixels with MapDialogRect.

HWND hCtl;
RECT rect;

hCtl = GetDlgItem(hWnd, IDC_COMBO);
GetClientRect(hCtl, &rect);
MapDialogRect(hCtl, &rect);
SetWindowPos(hCtl, 0, 0, 0, rect.right, (n_choices + 1) * rect.bottom, SWP_NOMOVE);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜