【发布时间】:2016-01-05 12:49:15
【问题描述】:
我想创建一个在桌面底角显示一个小窗口的应用程序。启动时,窗口应该非常小,理想情况下,宽度只有几个像素。
这是我以前做的代码:
public partial class DurationT64 : Form
{
private Size fullSize;
private Point fullPos;
private Point compactPos;
public DurationT64()
{
InitializeComponent();
var workingArea = Screen.PrimaryScreen.WorkingArea;
this.MinimumSize = new Size(0, this.Height);
this.MaximumSize = new Size(this.Width, this.Height);
// fullPos: the window location when it is in full size form.
fullPos = new Point(workingArea.Right - this.Width, workingArea.Bottom - this.Height);
this.Location = fullPos;
// fullSize: the size of the windown when it is in full size form.
fullSize = new Size(this.Width, this.Height);
this.Size = fullSize;
// compactPos: the window location when it is in compact size form.
compactPos = new Point(workingArea.Right - 30, fullPos.Y);
this.Width = 1;
this.Location = compactPos;
}
}
如您所见,在此示例中,我打算创建一个宽度仅为 1 像素的窗口,靠近主显示器的右边缘放置。
但是,我意识到窗口并没有我预期的那么小。它下降到 20 像素宽,但不小于这个值。请参考下面的屏幕截图,例如: an image shows that the window is wider than it suppose to be
我对这个问题进行了一些研究,并注意到 Zach Johnson (@zach-johnson) 在 2009 年提出了一个解决方案。这里是它的链接Overcome OS Imposed Windows Form Minimum Size Limit。
但是,该链接中提出的其他方法(Zach 提出的拦截 WM_ 消息和@Ace 提出的 SetBoundsCore 方法)对我有用。
谁能给我一些解决这个问题的方法?如果可能的话,最好是纯粹基于 C#/Winform 的解决方案,不依赖原生 Win32 窗口消息循环。
非常感谢!
【问题讨论】:
-
你试过
this.MinimumSize = new Size(1, 1);吗?
标签: c# winforms user-interface window