日期:2014-05-18  浏览次数:21013 次

怎么得到combobox中的DropDownButton或者得到DropDownButton的宽度
我现在想得到一个ComboBox实例中的右侧DropDownButton的宽度,请问用什么方法可以得到?

------解决方案--------------------
ComboBox是由文本框及ListBox组合而成的,但是它并不包含“Button”,所谓的组合框的“Button”其实就是它的一个未被TextBox覆盖的表面区域。

所以要得到这个区域的大小,实际就是组合框的大小与文本框的大小之差。
组合框的“Button”只所以看上去比较特别,其实它是在组合框的未被覆盖的区域通过GDI“绘制”出来的。
就像.net中的ControlPaint.DrawComboButton方法或ComboboxRenderer一样,Window也是这样的绘出来的一个假的按钮。

以上的说法只是我的看法,没有实际的跟据,但是通过Windows的句柄的角度来分析,的确没有找到这个按钮的句柄,但Combobox的TextBox及ListBox确实存在。
------解决方案--------------------
实际上ComboBox按钮的宽度就是垂直滚动条的宽度
这是我调整滚动条宽度发现的

C# code
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);

public const int SM_CXVSCROLL = 2; //垂直滚动条,宽度
public const int SM_CYHSCROLL = 3;
public const int SM_CYVSCROLL = 20;
public const int SM_CXHSCROLL = 21;

private void button1_Click(object sender, EventArgs e)
{
    Text = GetSystemMetrics(SM_CXVSCROLL).ToString();
}