【问题标题】:Position winform in the bottom left corner of the screen将winform放在屏幕左下角
【发布时间】:2012-09-02 12:33:43
【问题描述】:

我正在尝试将一个表单放置在屏幕的左下角(在开始按钮上)我有以下代码尝试执行此操作,但只考虑了屏幕的工作区域 - 所以表单位于开始按钮的正上方:

int x = Screen.PrimaryScreen.WorkingArea.Left + this.Width;
int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
this.Location = new Point(x, y);

下面是一个演示/屏幕,以进一步展示我正在尝试做的事情:

【问题讨论】:

  • 您不能重叠任务栏或开始按钮。
  • 如果我安装了你的软件并且它自己定位以便故意隐藏我的开始按钮,我会立即卸载它。
  • @Hans Passant 查看答案

标签: c# .net windows winforms


【解决方案1】:

你可以试试这段代码

Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(0, 
                          workingArea.Bottom - Size.Height);

【讨论】:

  • 它会在任务栏后面
【解决方案2】:

工作区通常不包括任何任务栏、停靠的窗口和停靠的工具栏。 使用Screen.PrimaryScreen.Bounds 可以为您提供屏幕的完整高度和宽度。

示例代码如下:

public Form1()
        {
            InitializeComponent();
            Rectangle r = Screen.PrimaryScreen.WorkingArea;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(0, Screen.PrimaryScreen.Bounds.Height - this.Height);
            this.TopMost = true;
        }

这很可能会显示在任务栏下方,因为通常任务栏默认设置在顶部。我记得在 Windows XP 中有一个选项可以关闭该选项,但不确定。

编辑:

在 Windows XP 中,您可以使任务栏位于 Windows 后面。点击链接:Always on top task bar

正如 Ria 所指出的,将 this.TopMost 设置为 true 是可行的,并且是一个更好的选择。

【讨论】:

  • 我正在寻找如何将窗口定位在开始栏的顶部以及 this.StartPosition = FormStartPosition.Manual; 的代码是我错过的那块......谢谢!
【解决方案3】:

使用Screen.PrimaryScreen.Bounds 属性并设置this.TopMost = true。这行得通:

int y = Screen.PrimaryScreen.Bounds.Bottom - this.Height;
this.Location = new Point(0, y);
this.TopMost = true;

【讨论】:

  • 谢谢,是的,这确实有效,尽管其他人都这么说:)
  • +1 以及添加到代码中的任务栏高度,如图所示。
【解决方案4】:

Ria 's answer 是正确的,但它没有添加任务栏高度。
如果您想要显示的图像中的确切内容,则应使用此代码:

int nTaskBarHeight = Screen.PrimaryScreen.Bounds.Bottom - 
                                            Screen.PrimaryScreen.WorkingArea.Bottom;
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(0, workingArea.Bottom - Size.Height + nTaskBarHeight);
this.TopMost = true;

【讨论】:

    猜你喜欢
    • 2015-09-28
    • 2010-11-26
    • 1970-01-01
    • 2018-01-27
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多