组合框下拉位置

发布于 2024-08-24 22:03:31 字数 103 浏览 3 评论 0原文

我有一个最大化的表单,其中有组合框控件(停靠在右上角),宽度为 500px 宽度

在尝试打开组合框后,列表的一半超出了屏幕。如何强制列表显示在表单中?

I have a maximized form which has Combo-Box control (docked in top-right) with 500px Width

After trying to open the combo-box , the half of the list goes out-of-screen . how can I force list shows within form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-08-31 22:03:31

棘手的问题。我找不到解决这个问题的好办法,只是一个解决方法。添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到窗体上。

解决方法并不是一个很好的方法。问题是下拉窗口将忽略移动它的尝试,直到下拉动画完成。视觉效果不太好,你会看到窗口下拉,然后跳到左边。我不知道还有什么其他方法可以把它打在头上,也许其他人知道。

C# 版本:

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);
}

VB.Net 版本:

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class MyComboBox
    Inherits ComboBox

    'P/Invoke declarations
    Private Structure COMBOBOXINFO
        Public cbSize As Int32
        Public rcItem As RECT
        Public rcButton As RECT
        Public buttonState As Integer
        Public hwndCombo As IntPtr
        Public hwndEdit As IntPtr
        Public hwndList As IntPtr
    End Structure

    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure


    <DllImport("user32.dll", EntryPoint:="SendMessageW", CharSet:=CharSet.Unicode)>
    Private Shared Function SendMessageCb(hWnd As IntPtr, msg As Integer, wp As IntPtr, ByRef lp As COMBOBOXINFO) As IntPtr
    End Function

    <DllImport("user32.dll")>
    Private Shared Function SetWindowPos(hWnd As IntPtr, after As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, flags As Integer) As Boolean
    End Function

    <DllImport("user32.dll")>
    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
    End Function


    Protected Overrides Sub OnDropDown(e As EventArgs)
          ' Is dropdown off the right side of the screen?
          Dim pos As Point = Me.PointToScreen(Me.Location)
          Dim scr As Screen = Screen.FromPoint(pos)

          If (scr.WorkingArea.Right < pos.X + Me.DropDownWidth) Then
              Me.BeginInvoke(New Action(Sub()

                                            'Retrieve handle to dropdown list
                                            Dim info As COMBOBOXINFO = New COMBOBOXINFO()
                                            info.cbSize = Marshal.SizeOf(info)
                                            SendMessageCb(Me.Handle, &H164, IntPtr.Zero, info)
                                            'Move the dropdown window
                                            Dim rc As RECT
                                            GetWindowRect(info.hwndList, rc)
                                            Dim x As Integer = scr.WorkingArea.Right - (rc.Right - rc.Left)
                                            SetWindowPos(info.hwndList, IntPtr.Zero, x, rc.Top, 0, 0, 5)
                                        End Sub))
          End If

          MyBase.OnDropDown(e)

    End Sub



End Class

Tricky problem. I could not find a good fix for this, merely a workaround. Add a new class and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

The workaround is not a very good one. The problem is that the dropdown window will ignore the attempt to move it until the drop-down animation is complete. The visual effect is not great, you'll see the window drop down, then jump to the left. I don't know an other way to slap it over the head, maybe somebody else does.

C# version:

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);
}

VB.Net version:

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class MyComboBox
    Inherits ComboBox

    'P/Invoke declarations
    Private Structure COMBOBOXINFO
        Public cbSize As Int32
        Public rcItem As RECT
        Public rcButton As RECT
        Public buttonState As Integer
        Public hwndCombo As IntPtr
        Public hwndEdit As IntPtr
        Public hwndList As IntPtr
    End Structure

    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure


    <DllImport("user32.dll", EntryPoint:="SendMessageW", CharSet:=CharSet.Unicode)>
    Private Shared Function SendMessageCb(hWnd As IntPtr, msg As Integer, wp As IntPtr, ByRef lp As COMBOBOXINFO) As IntPtr
    End Function

    <DllImport("user32.dll")>
    Private Shared Function SetWindowPos(hWnd As IntPtr, after As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, flags As Integer) As Boolean
    End Function

    <DllImport("user32.dll")>
    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
    End Function


    Protected Overrides Sub OnDropDown(e As EventArgs)
          ' Is dropdown off the right side of the screen?
          Dim pos As Point = Me.PointToScreen(Me.Location)
          Dim scr As Screen = Screen.FromPoint(pos)

          If (scr.WorkingArea.Right < pos.X + Me.DropDownWidth) Then
              Me.BeginInvoke(New Action(Sub()

                                            'Retrieve handle to dropdown list
                                            Dim info As COMBOBOXINFO = New COMBOBOXINFO()
                                            info.cbSize = Marshal.SizeOf(info)
                                            SendMessageCb(Me.Handle, &H164, IntPtr.Zero, info)
                                            'Move the dropdown window
                                            Dim rc As RECT
                                            GetWindowRect(info.hwndList, rc)
                                            Dim x As Integer = scr.WorkingArea.Right - (rc.Right - rc.Left)
                                            SetWindowPos(info.hwndList, IntPtr.Zero, x, rc.Top, 0, 0, 5)
                                        End Sub))
          End If

          MyBase.OnDropDown(e)

    End Sub



End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文