且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows窗体ComboBox下拉位置

更新时间:2023-12-06 10:33:04

这是扩展的





实现-具有下拉位置和AutoWith DropDown的组合框



您可以处理 WM_CTLCOLORLISTBOX 消息, lparam 是下拉菜单的句柄,然后您可以使用 SetWindowPos



如果 AutoWidthDropDown 为true,您还可以计算最长项目的宽度并将其设置为 DropDownWidth

 使用系统; 
使用System.Drawing;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用System.Linq;





 公共类MyComboBox: ComboBox 
{
private const UInt32 WM_CTLCOLORLISTBOX = 0x0134;
private const int SWP_NOSIZE = 0x1;
[DllImport( user32.dll)]
静态外部布尔值SetWindowPos(IntPtr hWnd,IntPtr hWndInsertAfter,
int X,int Y,int cx,int cy,uint uFlags);
public enum DropDownAlignments {左= 0,中间,右}
public bool AutoWidthDropDown {get;组; }
public DropDownAlignments DropDownAlignment {get;组; }
受保护的覆盖无效WndProc(ref message m)
{
if(m.Msg == WM_CTLCOLORLISTBOX){
var bottomLeft = this.PointToScreen(new Point(0,Height ));
var x = bottomLeft.X;
if(DropDownAlignment == MyComboBox.DropDownAlignments.Middle)
x-=(DropDownWidth-Width)/ 2;
else if(DropDownAlignment == DropDownAlignments.Right)
x-=(DropDownWidth-Width);
var y = bottomLeft.Y;
SetWindowPos(m.LParam,IntPtr.Zero,x,y,0,0,SWP_NOSIZE);
}
base.WndProc(ref m);
}
受保护的覆盖无效OnDropDown(EventArgs e)
{
if(AutoWidthDropDown)
DropDownWidth = Items.Cast< Object>()。Select(x =&gt ; GetItemText(x))
.Max(x => TextRenderer.MeasureText(x,字体,
Size.Empty,TextFormatFlags.Default).Width);
else
DropDownWidth = this.Width;
base.OnDropDown(e);
}
}


Usually dropdown item start position aligned with ComboBox start position like the image above. But i need to develop ComboBox Control which has the lengthy dropdown item gets aligned in middle.I mean Dropdown item left position should still more left positioned than ComboBox like the image below. Any help would be greatly appreciated.

Here is an extended ComboBox which have 2 new useful features to let you set the position and size of drop-down:

  • DropDownAlignment: You can set it to Left, then the drop-down will appear in it's normal position and it's left is aligned with left of control. If you set it to Middle, the middle of drop-down will be aligned with control and if you set it to Right, the right of drop-down will be aligned with right of control.

  • AutoWidthDropDown: If you set it to true to, then DropdownWidth will be set to the width of the longest item. If you set it to false it uses Width as DropDownWidth.

Here is appearance of drop-down after setting AutoWidthDropDown to true and DropDownAlignment to Left, Middle and Right:

Implementation - ComboBox with DropDown Position and AutoWith DropDown

You can handle WM_CTLCOLORLISTBOX message, the lparam is the handle of drop-down and then you can set position of drop-down using SetWindowPos.

Also you can calculate width of longest item and set as DropDownWidth if AutoWidthDropDown is true.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Linq;

public class MyComboBox : ComboBox
{
    private const UInt32 WM_CTLCOLORLISTBOX = 0x0134;
    private const int SWP_NOSIZE = 0x1;
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
        int X, int Y, int cx, int cy, uint uFlags);
    public enum DropDownAlignments { Left = 0, Middle, Right }
    public bool AutoWidthDropDown { get; set; }
    public DropDownAlignments DropDownAlignment { get; set; }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_CTLCOLORLISTBOX)  {
            var bottomLeft = this.PointToScreen(new Point(0, Height));
            var x = bottomLeft.X;
            if (DropDownAlignment == MyComboBox.DropDownAlignments.Middle)
                x -= (DropDownWidth - Width) / 2;
            else if (DropDownAlignment == DropDownAlignments.Right)
                x -= (DropDownWidth - Width);
            var y = bottomLeft.Y;
            SetWindowPos(m.LParam, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE);
        }
        base.WndProc(ref m);
    }
    protected override void OnDropDown(EventArgs e)
    {
        if (AutoWidthDropDown)
            DropDownWidth = Items.Cast<Object>().Select(x => GetItemText(x))
                  .Max(x => TextRenderer.MeasureText(x, Font,
                       Size.Empty, TextFormatFlags.Default).Width);
        else
            DropDownWidth = this.Width;
        base.OnDropDown(e);
    }
}