如果组合框控件中的列表比组合框宽,我怎样才能在组合框的右侧绘制它?它始终保持对齐。 例如 - 如果组合框位置靠近窗体的右侧,并且组合框中的列表比组合框的宽度宽,则列表的一部分对用户不可见。
答案 0 :(得分: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;
}
}
}