【发布时间】:2013-02-17 19:06:37
【问题描述】:
我正在使用 c# WinForm 开发一个 sman 通知应用程序。我想把主窗体放在屏幕工作区的右下角。 在多个屏幕的情况下,有一种方法可以找到最右边的屏幕来放置应用程序,或者至少记住最后使用的屏幕并将表单放在其右下角?
【问题讨论】:
标签: c# winforms forms position
我正在使用 c# WinForm 开发一个 sman 通知应用程序。我想把主窗体放在屏幕工作区的右下角。 在多个屏幕的情况下,有一种方法可以找到最右边的屏幕来放置应用程序,或者至少记住最后使用的屏幕并将表单放在其右下角?
【问题讨论】:
标签: c# winforms forms position
我目前没有要检查的多个显示器,但应该是这样的
public partial class LowerRightForm : Form
{
public LowerRightForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
PlaceLowerRight();
base.OnLoad(e);
}
private void PlaceLowerRight()
{
//Determine "rightmost" screen
Screen rightmost = Screen.AllScreens[0];
foreach (Screen screen in Screen.AllScreens)
{
if (screen.WorkingArea.Right > rightmost.WorkingArea.Right)
rightmost = screen;
}
this.Left = rightmost.WorkingArea.Right - this.Width;
this.Top = rightmost.WorkingArea.Bottom - this.Height;
}
}
【讨论】:
var rightmost = Screen.AllScreens.OrderBy (s => s.WorkingArea.Right).Last();
MaxBy 效率最高,但如果我不得不冒险猜测 OrderByDescending.First 应该比 OrderBy.Last 更有效。
覆盖表单Onload 并设置新位置:
protected override void OnLoad(EventArgs e)
{
var screen = Screen.FromPoint(this.Location);
this.Location = new Point(screen.WorkingArea.Right - this.Width, screen.WorkingArea.Bottom - this.Height);
base.OnLoad(e);
}
【讨论】:
//Get screen resolution
Rectangle res = Screen.PrimaryScreen.Bounds;
// Calculate location (etc. 1366 Width - form size...)
this.Location = new Point(res.Width - Size.Width, res.Height - Size.Height);
【讨论】:
以下代码应该可以工作:)
var rec = Screen.PrimaryScreen.WorkingArea;
int margain = 10;
this.Location = new Point(rec.Width - (this.Width + margain), rec.Height - (this.Height + margain));
【讨论】:
int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
// Add this for the real edge of the screen:
x = 0; // for Left Border or Get the screen Dimension to set it on the Right
this.Location = new Point(x, y);
【讨论】: