这段代码可以实现对话框的大小更改,但是最大化后下拉框(组合框)就不能弹出了,请问怎么解决?void CCreatingDlg::OnSize(UINT nType, int cx, int cy)//控件随对话框的大小同步改变
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
for (int ix=1000;ix<=1010;ix++) //控件的ID
{
CWnd *pWnd;
pWnd = GetDlgItem(ix); //获取ID为i的空间的句柄,因为“确认”ID为1,“取消”ID为2
if(pWnd) //判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建
{
CRect rect; //获取控件变化前的大小
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
rect.left=rect.left*cx/m_rect.Width();//调整控件大小
rect.right=rect.right*cx/m_rect.Width();
rect.top=rect.top*cy/m_rect.Height();
rect.bottom=rect.bottom*cy/m_rect.Height();
pWnd->MoveWindow(rect);//设置控件大小
}
}
GetClientRect(&m_rect);// 将变化后的对话框大小设为旧大小
}
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
for (int ix=1000;ix<=1010;ix++) //控件的ID
{
CWnd *pWnd;
pWnd = GetDlgItem(ix); //获取ID为i的空间的句柄,因为“确认”ID为1,“取消”ID为2
if(pWnd) //判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建
{
CRect rect; //获取控件变化前的大小
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
rect.left=rect.left*cx/m_rect.Width();//调整控件大小
rect.right=rect.right*cx/m_rect.Width();
rect.top=rect.top*cy/m_rect.Height();
rect.bottom=rect.bottom*cy/m_rect.Height();
pWnd->MoveWindow(rect);//设置控件大小
}
}
GetClientRect(&m_rect);// 将变化后的对话框大小设为旧大小
}
咱么设置的只是那个上面的edit的风格!!!而不是下面得listbox的
风格,因为,listbox根本还不存在!!! 现在的问题是:我需要在dialog中捕捉单击下拉框的消息,进而获得那个
listbox,从而就可以设置他的scrollbar了 //----------------------------------------------------
Q:
I found an example from MSDN on how to use SetHorizontalExtent function to make list box part horizontal scrollable. But it didn 't work. Anyone knows how to implement it. Appreciate any response.
A:
//----------------------------------------------------
Q:
在对话框中画出一个ComboBox控件,如何通过编程来控制下拉框的高度(我是在代码中创建ComboBox控件的)。谢谢!
A:
如果你指的是下拉部分的高度,既可以显示几个下拉项,可以使用CComboBox::SetWindowPos来修改控件的高度,这里SetWindowPos修改的高度就是下拉部分的高度。 //------------------------------------------------------
Q:
如何能够改变下拉框的的按钮的尺寸?比如我对话框的字体是宋体9号,可是下拉框的尺寸缺仍旧不变小。
A:
按钮的高度可以被改变,但宽度好象不能修改。你可以发送CB_SETITEMHEIGHT消息给控件。该消息定义为:
CB_SETITEMHEIGHT
wParam = (WPARAM) index; // item index
lParam = (LPARAM) (int) height; // item height
如果index参数为-1,则设置按钮的高度。如果index参数为0,则修改列表项的高度。如:
m_cmb.SendMessage(CB_SETITEMHEIGHT, -1, 30); //-------------------------------------------------------- 在ComboBox中改变列表框的宽度
我们经常会使用到组合框,而组合框是是有2种功能的--下拉和列表。一般情况下,列表框的宽度和选择框是一样宽的,但是我们有些时候确实很需要把列表框的宽度变大,一便让我们能更好的看我们要选的东西。
为了能有这个功能,我写了下面的这个函数。首先得在你的对话框中添加一个的WM_CTLCOLOR的消息句柄,或者使用CComboBox的继承类,而且在其中添加下面的代码: HBrush tvisualcombo::onctlcolor(CDC* pdc, CWND* pwnd, UINT nctlcolor)
{
HBrush hbr = ccombobox::onctlcolor(pdc, pwnd, nctlcolor);
switch (nctlcolor) {
case ctlcolor_edit:
break;
case ctlcolor_listbox:
if (listwidth > 0) {
// new width of a listbox is defined
CRect rect;
pwnd-> GetWindowRect(&rect);
if (rect.Width() != listwidth) {
rect.right = rect.left + listwidth;
pwnd-> MoveWindow(&rect);
}
}
break;
} // todo: return a different brush if the default is not desired
return hbr;
}
这样之后还没有完全好,你还得刷新一下列表框,那样才能随时根据列表框中的文本的长度,而改变列表框的宽度,要想这样的话,你还得这样,你必须扫描列表框中的条目,还得计算其中文本的长度(通过pdc),这样你如果再刷新列表框的话,才能一条目中比较长的来显示。 上面的方法是通过WM_CTLCOLOR消息来实现的,后来才知道在MFC的CComboBox类中有一个函数也可以实现同样的功能,就是: CComboBox::SetDroppedWidth(int width); 通过这个函数,你可以把宽度设成你自己喜欢的值,而它的实际的宽度是下面2个值中的最大值: 1.你所设置的值(就是通过上面的函数所设置的值) 2.列表框的值
//-------------------------------------------------------------
This code snippet uses MFC classes to resize a dropdown combo box at run-time. It should be easy to adapt to plain API code if you need to. It should work on any version of Windows. You can resize at design-time by clicking on the drop-down arrow in developer studio. void set_DropDownSize(CComboBox& box, UINT LinesToDisplay)
/*--------------------------------------------------------------------------
* Purpose: Set the proper number of lines in a drop-down list or
* combo box.
* Description: Resizes the combo box window to fit the proper number
* of lines. The window must exist before calling this function.
* This function should be called when the combo box is created, and when
* the font of the combo box changes. (e.g. WM_SETTINGCHANGE)
* Testing needed:
* Are there cases where SM_CYBORDER should be used instead of SM_CYEDGE?
* owner-draw variable height combo box
* Subclassed combo box with horizontal scroll-bar
* Returns: nothing
* Author: KTM
*--------------------------------------------------------------------------*/
{
ASSERT(IsWindow(box)); // Window must exist or SetWindowPos won 't work CRect cbSize; // current size of combo box
int Height; // new height for drop-down portion of combo box box.GetClientRect(cbSize);
Height = box.GetItemHeight(-1); // start with size of the edit-box portion
Height += box.GetItemHeight(0) * LinesToDisplay; // add height of lines of text // Note: The use of SM_CYEDGE assumes that we 're using Windows '95
// Now add on the height of the border of the edit box
Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges // The height of the border of the drop-down box
Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges // now set the size of the window
box.SetWindowPos(NULL, // not relative to any other windows
0, 0, // TopLeft corner doesn 't change
cbSize.right, Height, // existing width, new height
SWP_NOMOVE | SWP_NOZORDER // don 't move box or change z-ordering.
);
}
这样设置就可以了