如何在WPF中更改屏幕分辨率?

发布于 2024-09-28 09:17:53 字数 417 浏览 0 评论 0原文

如何在 WPF 中以编程方式更改屏幕分辨率? 我怎样才能获得可用的显示分辨率列表?

我尝试尝试一下本文中描述的示例: http:// www.c-sharpcorner.com/UploadFile/Joshy_geo/changescreenresolution10102006112110AM/changescreenresolution.aspx 但Screen 和Resolution 类不存在于System.Diagnostics 命名空间中。

C#/WPF

谢谢。

how can I change screen resolution programmatically in WPF?
And how can I get a list of available resolutions for display?

I tried have a go at the example described in this article: http://www.c-sharpcorner.com/UploadFile/Joshy_geo/changescreenresolution10102006112110AM/changescreenresolution.aspx But Screen and Resolution classes do not exist in the System.Diagnostics namespace.

C#/WPF

Thanx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-10-05 09:17:53

抱歉给您带来麻烦了。我找到了 Windows 窗体的解决方案并做了一些小的更改。

WPF 解决方案

在窗口上放置两个列表框(listDevices 和 listSettings)和按钮(btnSave)。

<代码>
使用系统;
使用 System.Runtime.InteropServices;
使用系统.Windows;
using System.Windows.Controls;

public partial class MainWindow : Window
{
    [StructLayout(LayoutKind.Sequential)]
    public struct DISPLAY_DEVICE
    {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceString;
        public int StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceKey;

        public DISPLAY_DEVICE(int flags)
        {
            cb = 0;
            StateFlags = flags;
            DeviceName = new string((char)32, 32);
            DeviceString = new string((char)32, 128);
            DeviceID = new string((char)32, 128);
            DeviceKey = new string((char)32, 128);
            cb = Marshal.SizeOf(this);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public short dmOrientation;
        public short dmPaperSize;
        public short dmPaperLength;
        public short dmPaperWidth;
        public short dmScale;
        public short dmCopies;
        public short dmDefaultSource;
        public short dmPrintQuality;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;
        public short dmUnusedPadding;
        public short dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;
        public int dmDisplayFlags;
        public int dmDisplayFrequency;
    }
    public MainWindow()
    {
        InitializeComponent();
        EnumDevices();
    }

    private void listDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int devNum = listDevices.SelectedIndex;
        bool isMain = MainDevice(devNum);
        btnSet.IsEnabled = isMain; // enable only for the main device
        EnumModes(devNum);
    }

    private void btnSet_Click(object sender, RoutedEventArgs e)
    { //set selected display mode
        int devNum = listDevices.SelectedIndex;
        int modeNum = listSettings.SelectedIndex;
        DEVMODE d = GetDevmode(devNum, modeNum);
        if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0
            & d.dmPelsHeight != 0)
        {
            ChangeDisplaySettings(ref d, 0);
        }
    }

    private void EnumModes(int devNum)
    {
        listSettings.Items.Clear();

        string devName = GetDeviceName(devNum);
        DEVMODE devMode = new DEVMODE();
        int modeNum = 0;
        bool result = true;
        do
        {
            result = EnumDisplaySettings(devName,
                modeNum, ref devMode);

            if (result)
            {
                string item = DevmodeToString(devMode);
                listSettings.Items.Add(item);
            }
            modeNum++;
        } while (result);

        if (listSettings.Items.Count > 0)
        {
            DEVMODE current = GetDevmode(devNum, -1);

         //   int selected = listSettings.FindString(DevmodeToString(current));
            int selected = listSettings.Items.IndexOf(DevmodeToString(current));
            if (selected >= 0)
            {
                listSettings.SelectedIndex = selected;
             //   listSettings.SetSelected(selected, true);
            }
        }
    }

    private DEVMODE GetDevmode(int devNum, int modeNum)
    { //populates DEVMODE for the specified device and mode
        DEVMODE devMode = new DEVMODE();
        string devName = GetDeviceName(devNum);
        EnumDisplaySettings(devName, modeNum, ref devMode);
        return devMode;
    }

    private string DevmodeToString(DEVMODE devMode)
    {
        return devMode.dmPelsWidth.ToString() +
            " x " + devMode.dmPelsHeight.ToString() +
            ", " + devMode.dmBitsPerPel.ToString() +
            " bits, " +
            devMode.dmDisplayFrequency.ToString() + " Hz";
    }

    private void EnumDevices()
    { //populates Display Devices list
        this.listDevices.Items.Clear();
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);

        int devNum = 0;
        bool result;
        do
        {
            result = EnumDisplayDevices(IntPtr.Zero,
                devNum, ref d, 0);

            if (result)
            {
                string item = devNum.ToString() +
                    ". " + d.DeviceString.Trim();
                if ((d.StateFlags & 4) != 0) item += " - main";
                this.listDevices.Items.Add(item);
            }
            devNum++;
        } while (result);
    }

    private string GetDeviceName(int devNum)
    {
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
        bool result = EnumDisplayDevices(IntPtr.Zero,
            devNum, ref d, 0);
        return (result ? d.DeviceName.Trim() : "#error#");
    }

    private bool MainDevice(int devNum)
    { //whether the specified device is the main device
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
        if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
        {
            return ((d.StateFlags & 4) != 0);
        } return false;
    }

    [DllImport("User32.dll")]
    private static extern bool EnumDisplayDevices(
        IntPtr lpDevice, int iDevNum,
        ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);

    [DllImport("User32.dll")]
    private static extern bool EnumDisplaySettings(
        string devName, int modeNum, ref DEVMODE devMode);

    [DllImport("user32.dll")]
    public static extern int ChangeDisplaySettings(
        ref DEVMODE devMode, int flags);
}

WPF 屏幕分辨率 来源:http:// /www.mediafire.com/?ciiymhmc7v28v4y

Sorry for the trouble. I found the solution for Windows Forms and made minor changes.

WPF Solution

Place two Listbox (listDevices and listSettings) and Button (btnSave) on the window.


using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;

public partial class MainWindow : Window
{
    [StructLayout(LayoutKind.Sequential)]
    public struct DISPLAY_DEVICE
    {
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceString;
        public int StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string DeviceKey;

        public DISPLAY_DEVICE(int flags)
        {
            cb = 0;
            StateFlags = flags;
            DeviceName = new string((char)32, 32);
            DeviceString = new string((char)32, 128);
            DeviceID = new string((char)32, 128);
            DeviceKey = new string((char)32, 128);
            cb = Marshal.SizeOf(this);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public short dmOrientation;
        public short dmPaperSize;
        public short dmPaperLength;
        public short dmPaperWidth;
        public short dmScale;
        public short dmCopies;
        public short dmDefaultSource;
        public short dmPrintQuality;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;
        public short dmUnusedPadding;
        public short dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;
        public int dmDisplayFlags;
        public int dmDisplayFrequency;
    }
    public MainWindow()
    {
        InitializeComponent();
        EnumDevices();
    }

    private void listDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int devNum = listDevices.SelectedIndex;
        bool isMain = MainDevice(devNum);
        btnSet.IsEnabled = isMain; // enable only for the main device
        EnumModes(devNum);
    }

    private void btnSet_Click(object sender, RoutedEventArgs e)
    { //set selected display mode
        int devNum = listDevices.SelectedIndex;
        int modeNum = listSettings.SelectedIndex;
        DEVMODE d = GetDevmode(devNum, modeNum);
        if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0
            & d.dmPelsHeight != 0)
        {
            ChangeDisplaySettings(ref d, 0);
        }
    }

    private void EnumModes(int devNum)
    {
        listSettings.Items.Clear();

        string devName = GetDeviceName(devNum);
        DEVMODE devMode = new DEVMODE();
        int modeNum = 0;
        bool result = true;
        do
        {
            result = EnumDisplaySettings(devName,
                modeNum, ref devMode);

            if (result)
            {
                string item = DevmodeToString(devMode);
                listSettings.Items.Add(item);
            }
            modeNum++;
        } while (result);

        if (listSettings.Items.Count > 0)
        {
            DEVMODE current = GetDevmode(devNum, -1);

         //   int selected = listSettings.FindString(DevmodeToString(current));
            int selected = listSettings.Items.IndexOf(DevmodeToString(current));
            if (selected >= 0)
            {
                listSettings.SelectedIndex = selected;
             //   listSettings.SetSelected(selected, true);
            }
        }
    }

    private DEVMODE GetDevmode(int devNum, int modeNum)
    { //populates DEVMODE for the specified device and mode
        DEVMODE devMode = new DEVMODE();
        string devName = GetDeviceName(devNum);
        EnumDisplaySettings(devName, modeNum, ref devMode);
        return devMode;
    }

    private string DevmodeToString(DEVMODE devMode)
    {
        return devMode.dmPelsWidth.ToString() +
            " x " + devMode.dmPelsHeight.ToString() +
            ", " + devMode.dmBitsPerPel.ToString() +
            " bits, " +
            devMode.dmDisplayFrequency.ToString() + " Hz";
    }

    private void EnumDevices()
    { //populates Display Devices list
        this.listDevices.Items.Clear();
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);

        int devNum = 0;
        bool result;
        do
        {
            result = EnumDisplayDevices(IntPtr.Zero,
                devNum, ref d, 0);

            if (result)
            {
                string item = devNum.ToString() +
                    ". " + d.DeviceString.Trim();
                if ((d.StateFlags & 4) != 0) item += " - main";
                this.listDevices.Items.Add(item);
            }
            devNum++;
        } while (result);
    }

    private string GetDeviceName(int devNum)
    {
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
        bool result = EnumDisplayDevices(IntPtr.Zero,
            devNum, ref d, 0);
        return (result ? d.DeviceName.Trim() : "#error#");
    }

    private bool MainDevice(int devNum)
    { //whether the specified device is the main device
        DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
        if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
        {
            return ((d.StateFlags & 4) != 0);
        } return false;
    }

    [DllImport("User32.dll")]
    private static extern bool EnumDisplayDevices(
        IntPtr lpDevice, int iDevNum,
        ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);

    [DllImport("User32.dll")]
    private static extern bool EnumDisplaySettings(
        string devName, int modeNum, ref DEVMODE devMode);

    [DllImport("user32.dll")]
    public static extern int ChangeDisplaySettings(
        ref DEVMODE devMode, int flags);
}

WPF Screen Resolution Source: http://www.mediafire.com/?ciiymhmc7v28v4y

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文