C# Winforms 获取当前屏幕分辨率的方式

有些时候需要根据屏幕分辨率动态决定窗体弹出的位置,这时就需要先获取屏幕的分辨率,通过搜索,临时有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); 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值