C# 屏幕右下角的表格位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15188939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:24:18  来源:igfitidea点击:

Form position on lower right corner of the screen

c#winformsformsposition

提问by Terix

I am using c# WinForm to develop a sman notification app. I would like to place the main form on the lower right corner of the screen working area. In case of multiple screens, there is a way to find the rightmost screen where to place the app, or at least remember the last used screen and palce the form on its lower right corner?

我正在使用 c# WinForm 开发一个 sman 通知应用程序。我想把主窗体放在屏幕工作区的右下角。在多个屏幕的情况下,有没有办法找到最右边的屏幕放置应用程序的位置,或者至少记住上次使用的屏幕并将表单放在其右下角?

采纳答案by David Goshadze

I don't currently have multiple displays to check, but it should be something like

我目前没有要检查的多个显示器,但它应该类似于

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

回答by Sami Franka

Override the Form Onloadand set the new location :

覆盖表单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);
}

回答by Umut D.

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

回答by Goodbye

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

回答by Anonymous Helper

This following code should work :)

以下代码应该可以工作:)

var rec = Screen.PrimaryScreen.WorkingArea;
int margain = 10;

this.Location = new Point(rec.Width - (this.Width + margain), rec.Height - (this.Height + margain));