更改 COMBOBOX 的高度
如何在运行时更改使用资源定义创建的 COMBOBOX 控件的高度,以便可以在组合框中插入新字符串?字符串插入代码可以正常工作,但前提是我在资源定义中为组合框设置了固定高度(例如 28 个单位)。但这并不方便,因为字符串的数量是动态的。
我知道我可以在运行时创建对话框,但随后我无法使用对话框单元,并且资源效率更高...
这是我的代码的简化版本。
资源文件:
IDD_SETTINGS DIALOG 0, 0, 100, 100
BEGIN
COMBOBOX IDC_COMBO, 0, 0, 100, 14, CBS_DROPDOWNLIST
END
主窗口和对话框的窗口过程:
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, wParam, lParam);
}
return(0L);
}
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, wParam, lParam);
}
return(0L);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您指的是组合框下拉部分的高度。
您仍然可以使用对话框单元,请查看 GetDialogBaseUnits 将返回每个对话框单元的像素数。如果您使用的是非系统字体,以下知识库文章详细介绍了计算 - 如何使用以下命令计算对话框基本单位非基于系统的字体。
您可以使用 SetWindowPos< 以编程方式更改组合框的大小/a>.
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.
与此同时,我找到了解决办法。这是我现在正在使用的。我将资源文件中组合框的高度设置为 14 DLU(一项的高度),以便正确计算新的高度。使用
GetClientRect
获取此高度,并使用MapDialogRect
将其转换为像素。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 withMapDialogRect
.