当前位置: 代码迷 >> .NET面试 >> combobox 可以用 CB_SETDROPPEDWIDTH改变下拉项的宽, 小弟我想改变他的left 有办法吗
  详细解决方案

combobox 可以用 CB_SETDROPPEDWIDTH改变下拉项的宽, 小弟我想改变他的left 有办法吗

热度:80   发布时间:2016-05-02 01:29:50.0
combobox 可以用 CB_SETDROPPEDWIDTH改变下拉项的宽, 我想改变他的left 有办法吗?
combobox 可以用 CB_SETDROPPEDWIDTH改变下拉项的宽, 我想改变他的left 有办法吗?
------解决方案--------------------
不是特别完美,但是可以试一下下面的链接提供的解决方案
combo box dropdown position

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

public class MyComboBox : ComboBox {
  protected override void OnDropDown(EventArgs e) {
    // Is dropdown off the right side of the screen?
    Point pos = this.PointToScreen(this.Location);
    Screen scr = Screen.FromPoint(pos);
    if (scr.WorkingArea.Right < pos.X + this.DropDownWidth) {
       this.BeginInvoke(new Action(() => {
        // Retrieve handle to dropdown list
        COMBOBOXINFO info = new COMBOBOXINFO();
        info.cbSize = Marshal.SizeOf(info);
        SendMessageCb(this.Handle, 0x164, IntPtr.Zero, out info);
        // Move the dropdown window
        RECT rc;
        GetWindowRect(info.hwndList, out rc);
        int x = scr.WorkingArea.Right - (rc.Right - rc.Left);
        SetWindowPos(info.hwndList, IntPtr.Zero, x, rc.Top, 0, 0, 5);
      }));
    }
    base.OnDropDown(e);
  }

  // P/Invoke declarations
  private struct COMBOBOXINFO {
    public Int32 cbSize;
    public RECT rcItem, rcButton;
    public int buttonState;
    public IntPtr hwndCombo, hwndEdit, hwndList;
  }
  private struct RECT {
    public int Left, Top, Right, Bottom;
  }
  [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
  private static extern IntPtr SendMessageCb(IntPtr hWnd, int msg, IntPtr wp, out COMBOBOXINFO lp);
  [DllImport("user32.dll")]
  private static extern bool SetWindowPos(IntPtr hWnd, IntPtr after, int x, int y, int cx, int cy, int flags);
  [DllImport("user32.dll")]
  private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
}
  相关解决方案