C# WPF 获取屏幕的宽高

C# WPF 获取屏幕的宽高

获取主显示器的工作区宽度和高度,当前屏幕选择的分辨率,该分辨率是以像素为单位:
SystemParameters.PrimaryScreenWidth
SystemParameters.PrimaryScreenHeight
获取整个屏幕的大小,包括任务栏等区域:
SystemParameters.VirtualScreenWidth
SystemParameters.VirtualScreenHeight
当前屏幕工作区的宽和高(除去任务栏)
SystemParameters.WorkArea.Size.Width
SystemParameters.WorkArea.Size.Height

中间显示示例:

//中间显示
//Number.WindowStartupLocation = WindowStartupLocation.CenterScreen;
//int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
//int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
double xWidth = SystemParameters.PrimaryScreenWidth;//当前屏幕相对宽度
double yHeight = SystemParameters.PrimaryScreenHeight;//当前屏幕相对高度
Number.WindowStartupLocation = WindowStartupLocation.Manual;
Number.Left = xWidth / 2 - 120;
Number.Top = 10;

.net6下wpf 使用 System.Windows.SystemParameters 无法获取屏幕最大尺寸时,解决方法如下:

1、在WPF项目中引入Forms框架
项目右击 》》》属性》》》选中为该项目启用windows窗体。

C# WPF 获取屏幕的宽高

2、使用如下代码获取实际分辨率

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TabletTools
{
    class MixTools
    {
        public static void GetFullSize(ref double width, ref double height)
        {
            Screen screen = Screen.PrimaryScreen;
            Rectangle bounds = screen.Bounds;

            width = bounds.Width;
            height = bounds.Height;
        }
    }
}

原创文章,作者:Gary,如若转载,请注明出处:https://www.cpw5.top/131.html

淘宝小店:陈皮王五工作室

(0)
GaryGary
上一篇 2024-08-17 下午2:46
下一篇 2024-09-01 上午9:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注