我的窗体已最大化,其中带有下拉框控件(停靠在右上角),其宽度为500像素Width
。
尝试打开组合框后,列表的一半超出了屏幕。我如何强制列表在表单内显示?
我的窗体已最大化,其中带有下拉框控件(停靠在右上角),其宽度为500像素Width
。
尝试打开组合框后,列表的一半超出了屏幕。我如何强制列表在表单内显示?
棘手的问题。我找不到一个好的修复方法,只能采用一种变通方法。添加一个新类并复制下面所示的代码。编译。从工具箱的顶部将新控件拖放到您的表单上。
这种解决方法并不是很好。问题在于下拉窗口会忽略移动的尝试,直到下拉动画完成。视觉效果不佳,你会看到窗口下降,然后跳到左边。我不知道有其他方法解决,也许别人知道。
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
I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...
I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...
I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?
How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...
I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...
When I m trying to change the default Image of a Control on Windows Forms in Form Designer (no matter where on which control) I get this error: Error message: An item with the same key has ...
I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.