有些时候需要根据屏幕分辨率动态决定窗体弹出的位置,这时就需要先获取屏幕的分辨率,通过搜索,临时有3种方式,见下方代码:
//方式一:系统信息之主显示器尺寸,单位像素
int xWidth1 = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
int yHeight1 = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;//高度
Console.WriteLine(xWidth1 + "px * " + yHeight1 + "px");
//方式二:绘画类之矩形
System.Drawing.Rectangle rec = Screen.GetWorkingArea(this);
int xWidth2 = rec.Width;
int yHeight2 = rec.Height;
Console.WriteLine(xWidth2 + "px * " + yHeight2 + "px");
//方式三:屏幕类之主屏幕之边界
int xWidth3 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int yHeight3 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
Console.WriteLine(xWidth3 + "px * " + yHeight3 + "px");
输出:
由上可见,第二种方式会去掉任务栏的高度,其他二种方法获取的都是显示器真实的分辨率。
下面给出设置一个winform窗体出现在左下角:左留白15px,任务栏之上留白15px的代码
//设置窗体位置为手动设置
this.StartPosition = FormStartPosition.Manual;
int yHeight = SystemInformation.PrimaryMonitorSize.Height;
//任务栏的高度是40px,在任务之上15px,得55px,再加上自身高度,获得左上角的坐标值
this.Location = new Point(15,yHeight - 55 - this.Height);