【问题标题】:Drawing list on right side of combobox in C# (winforms)C#中组合框右侧的绘制列表(winforms)
【发布时间】:2018-03-13 09:59:57
【问题描述】:

如果组合框控件中的列表比组合框宽,我如何在组合框右侧绘制它?它总是左对齐。 例如 - 如果组合框位置靠近表单的右侧,并且组合框中的列表比组合框的宽度宽,则列表的一部分对用户不可见。

【问题讨论】:

  • Align Text in Combobox的可能重复
  • 我不想将列表中的文本左对齐或右对齐,而是将整个列表绘制在组合框控件的右侧。
  • 那么你真的尝试过自己做吗?
  • 是的,我在 DrawItem 事件中尝试了一些东西,但我不确定如何获取对组合框列表的引用。

标签: c# .net winforms


【解决方案1】:

经过一番搜索,我设法在另一个论坛上找到了解决方案。我在 C# 中采用了它并稍作改动。

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

//  Fix Combo dropdown list if (left or right) off screen.

public class ComboExtended : ComboBox 
{

    [DllImport("user32.dll")]
    private extern static bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [MarshalAs(UnmanagedType.Bool)] bool bRepaint);

    [DllImport("user32.dll")]
    private extern static bool GetWindowRect(IntPtr hWnd, ref tagRECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    private struct tagRECT
    {
        public int left, top, right, bottom;
    }

    private const int WM_CTLCOLORLISTBOX = 308;
    private bool ListMoved = false;
    protected override void OnDropDownClosed(EventArgs e)
    {
        base.OnDropDownClosed(e);
        ListMoved = false;
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (!ListMoved && m.Msg == WM_CTLCOLORLISTBOX)
        {
            tagRECT rc = new tagRECT();
            GetWindowRect(m.LParam, ref rc);
            Form parentForm = this.FindForm();
            int posLeft = rc.right - rc.left > this.Width ? rc.left - ((rc.right - rc.left) - this.Width) : rc.left;
            int posTop = rc.bottom - rc.top > parentForm.Height - rc.top ? (rc.top - (rc.bottom - rc.top)) - this.Height : rc.top;
            MoveWindow(m.LParam, posLeft, posTop, rc.right - rc.left, rc.bottom - rc.top, true);
            ListMoved = true;
        }
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-14
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多