Win32 Api 如何设置 Combo-box下拉列表的高度
CreateWindow的时候nHeight参数设置足够大,如果Create的时候指定的高度不大
可在后面用MoveWindow再来重设其Height
(需要GetWindowRect 、ScreenToClient配合)
举例
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
//wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("设置COMBO控件高度"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
static HWND hEdit;
static HWND hBtn;
static HWND hCombo;
TCHAR buf[1000];
int i;
switch (message)
{
case WM_CREATE:
hCombo = CreateWindow("COMBOBOX", "Combo1",CBS_DROPDOWNLIST|WS_CHILD|WS_VSCROLL, 10,20,80,50,hwnd, NULL, NULL, NULL);//高50
for(i=1;i<11;i++)
{
sprintf(buf, "第%d项", i);
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)buf);
}
ShowWindow(hCombo, SW_SHOW);
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""),
WS_CHILDWINDOW, 100, 100, 200, 30,
hwnd, NULL, NULL, NULL);
ShowWindow(hEdit, SW_SHOW);
hBtn = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("BUTTON"), TEXT("按钮"),
WS_CHILDWINDOW, 340, 100, 100, 30,
hwnd, NULL, NULL, NULL);
ShowWindow(hBtn, SW_SHOW);
return 0 ;
case WM_COMMAND:
if(lParam == (LPARAM)hBtn)
{
RECT rt;
POINT p1;
int width;
int height;
GetWindowRect(hCombo, &rt);
p1.x= rt.left;
p1.y= rt.top;
width = rt.right-rt.left;
height = (rt.bottom-rt.top)*5;
ScreenToClient(hwnd, &p1);
MoveWindow(hCombo, p1.x, p1.y, width,height,TRUE);
}
return 0;
case WM_DESTROY:
DestroyWindow(hCombo);
DestroyWindow(hEdit);
DestroyWindow(hBtn);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
太棒了,你的回答完美的解决了我的问题!非常感谢你!^_^
我还想再问一句,如果只想用户从我的下拉列表中选择内容,而不想让他们编辑内容,该怎么设置呢?
如上 所示,dwStyle 参数 设置 用位运算 中的 按位或 | 添加上 CBS_DROPDOWNLIST