Projekt-Anfragen: Tel: 07022/9319004 info@CodeDocu.de Software Entwicklung in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

 

Diese kleine Software Anwendung erlaubt es mehrere Bildschirm-Auflösungen auszuwählen und dann bei Bedarf schnell umzuschalten.

Man benötigt dieses kleine Tool, wenn man öfters die Auflösung des Monitors für Video-Aufnahmen oder spezielle Spiele oder Multimedia Anwendungen ändern muss.

 

 

Video Anleitung, Funktionsweise:

 

 

Software App: Change Screen Resolution

Für: Windows 7, Windows 10

 

 

Die Anwendung wurde unter Windows Forms WinForms erstellt.

 

 

Settings:

Im Projekt muss man unter den Settings eine Speichermöglichkeit für die ausgewählten Auflösungen einfügen

 

 

C#, Winforms Code:

Im Wesentlichen basiert die Umschaltfunktion auf der User32 Methode

ChangeDisplaySettings

Welche über InteropServices eingebunden wird.

 

 

 

C# Code zum Verwenden:

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.Windows.Forms;

using System.Collections.Specialized; //StringCollections in Settings

 

namespace _20170329_Change_Resolution

{

 

 

    public partial class Form1 : Form

    {

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

        }

 

        #region Form

        public Form1()

        {

            //------------------< Form_Init() >------------------

            InitializeComponent();

           

            //< get saved resolutions >

            StringCollection selected_Resolution_Settings = Properties.Settings.Default.selected_Resolutions;

            if (selected_Resolution_Settings != null)

            {

                btnSelect.Text = "<>";

            }

            //</ get saved resolutions >

 

            fill_List_with_Devices();

            //------------------</ Form_Init() >------------------

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

 

        }

 

        private void Form1_Activated(object sender, EventArgs e)

        {

 

        }

        #endregion  /Form

 

 

        #region Lists

        private void listDevices_SelectedIndexChanged(

                object sender, EventArgs e)

        {

            int devNum = listDevices.SelectedIndex;

            bool isMain = MainDevice(devNum);

            //btnSet.Enabled = isMain; // enable only for the main device

            fill_List_with_Settings(devNum);

        }

        #endregion /Lists

 

        #region Buttons

        //----------------------------< Buttons >----------------------------

        private void btnRefresh_Click(object sender, EventArgs e)

        {

            fill_List_with_Settings(0);

        }

        private void btnSelect_Click(object sender, EventArgs e)

        {

            //---------< btnSelect_Click() >---------

            int intDevice = listDevices.SelectedIndex;

 

            if (btnSelect.Text!="<>")

            {

                save_Selected_Resolutions();

                //< off >

                listSettings.Items.Clear();

                btnSelect.Text = "<>";

                listSettings.SelectionMode = SelectionMode.One;

 

                fill_List_with_Settings(intDevice);

                //</ off >

            }

            else

            {

                //< Multiselect >

                btnSelect.Text = "select";

                listSettings.SelectionMode = SelectionMode.MultiSimple ;

                fill_List_with_Settings(intDevice);

                //</ Multiselect >

            }

            select_Resolutions();

            //---------</ btnSelect_Click() >---------

        }

        private void btnSet_Click(object sender, EventArgs e)

        {

            //set selected display mode

            int devNum = listDevices.SelectedIndex;

 

            string sSelected= listSettings.SelectedItem.ToString(); // 1: 1920 x 1080 32 bit

 

            //< check >

            if (sSelected == "") return;

            //</ check >

 

 

            int intPos = sSelected.IndexOf(":");

            string sNr = sSelected.Substring(0, intPos);

            int modeNum = Convert.ToInt32(sNr);

 

            //--< Get DeviceMode >--

            DEVMODE d = get_Device_Mode(devNum, modeNum);

            if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0 & d.dmPelsHeight != 0)

            {

                //< Set Display Setting >

                ChangeDisplaySettings(ref d, 0);

                //</ Set Display Setting >

            }

            //--< Get DeviceMode >--

        }

        //----------------------------</ Buttons >----------------------------

        #endregion  /Buttons

 

        #region Controls           

        private void listSettings_DoubleClick(object sender, EventArgs e)

        {

            btnSet_Click(sender, e);

        }

        #endregion  /Controls

 

        #region Methods

        //----------------------------< Methods >----------------------------

 

 

 

        #region Fill_Lists

        private void fill_List_with_Devices()

        {

            //----------------< fill_List_with_Devices() >----------------

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

 

            //< select first >          

            listDevices.SelectedIndex = 0;

            //</ select first >          

            //----------------</ fill_List_with_Devices() >----------------

        }

 

        private void fill_List_with_Settings(int devNum)

        {

            //----------------< fill_List_with_Settings() >----------------

            listSettings.Items.Clear();

 

            string devName = get_Device_Name(devNum);

            DEVMODE devMode = new DEVMODE();

 

            //< get settings >

            StringCollection selected_Resolution_Settings = Properties.Settings.Default.selected_Resolutions;

            //</ get settings >

 

            //----< @Loop: Resolutions >----

            for (int iMode=0;iMode<1000;iMode++)

            {

                bool result = EnumDisplaySettings(devName, iMode, ref devMode);

                //< check.break >

                if (result == false) break;

                //</ check.break >

              

                //--< add item >--

                string sResolution = iMode + ": " + DevmodeToString(devMode);

                if (btnSelect.Text=="<>")

                {

                    if (selected_Resolution_Settings!=null)

                    {

                        if (selected_Resolution_Settings.Contains(sResolution))

                        {

                            listSettings.Items.Add(sResolution);

                        }

                    }

                }

                else

                {

                    //< add >

                    listSettings.Items.Add(sResolution);

                    //</ add >

                }

                //--</ add item >--

 

            }

            //----</ @Loop: Resolutions >----

 

            //--< Select actual Setting >--

            if (listSettings.Items.Count > 0)

            {

                DEVMODE current = get_Device_Mode(devNum, -1);

                int selected = listSettings.FindString(DevmodeToString(current));

                if (selected >= 0)listSettings.SetSelected(selected, true);

            }

            //--</ Select actual Setting >--

 

            //----------------</ fill_List_with_Settings() >----------------

        }

        #endregion /Fill_Lists

 

 

        #region Settings

        private void select_Resolutions()

        {

            //---------< select_Resolutions() >---------

            //< get saved resolutions >

            StringCollection selected_Resolution_Settings = Properties.Settings.Default.selected_Resolutions;

            if (selected_Resolution_Settings == null) return;

            //</ get saved resolutions >

 

            for (int iItem = 0; iItem < listSettings.Items.Count; iItem++)

            {

                string sText = listSettings.Items[iItem].ToString();

                if (selected_Resolution_Settings.Contains(sText))

                {

                    listSettings.SelectedItems.Add(sText);

                }

            }

 

            //---------</ select_Resolutions() >---------

        }

 

        private void save_Selected_Resolutions()

        {

            //---------< save_Selected_Resolutions() >---------

            //< new list >

            StringCollection selected_Strings = new StringCollection();

            //</ new list >

 

            //< add strings >

            foreach (string sResolution in listSettings.SelectedItems)

            {

                selected_Strings.Add(sResolution);

            }

            //</ add strings >

 

            //< save >

            //*save list in settings

            Properties.Settings.Default.selected_Resolutions = selected_Strings;

            Properties.Settings.Default.Save();

            //< save >

 

 

            //---------</ save_Selected_Resolutions() >---------

        }

 

        #endregion /Settings

 

        private DEVMODE get_Device_Mode(int devNum, int modeNum)

        { //populates DEVMODE for the specified device and mode

            DEVMODE devMode = new DEVMODE();

            string devName = get_Device_Name(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 string get_Device_Name(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;

        }

 

        //----------------------------</ Methods >--------------------------

        #endregion  /Methods

 

 

        #region Imports

        //-----------------< Imports >-------------

 

        //*get Display Devices

        [DllImport("User32.dll")]

        private static extern bool EnumDisplayDevices(

            IntPtr lpDevice, int iDevNum,

            ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);

       

        //*get Display Settings Screen Resolutions

        [DllImport("User32.dll")]

        private static extern bool EnumDisplaySettings(

            string devName, int modeNum, ref DEVMODE devMode);

 

        //*set Display Resolution and Settings

        [DllImport("user32.dll")]

        public static extern int ChangeDisplaySettings(

            ref DEVMODE devMode, int flags);

 

 

 

        //-----------------</ Imports >-------------

        #endregion /Imports

 

 

    }

}

 

 

 

Code-Quellen:

Verweis: Import von User32 Methoden und Anwendung unter

http://www.news2news.com/vfp/?example=374&ver=vcs

 

Mobile
»
Code: Foto mit Windows verkleinern, Resize
»
C#: Bild in WinForms anzeigen
»
SMTP Fehler: Unable to read data from the transport connection
»
SMTPClient für Versenden mit SSL und Authentication
»
Winforms: Connectionstring zur Datenbank
»
Net WinForms Code: Screen zu Bitmap
»
WinForms: Code: Auflösung des Monitors wechseln per App
»
C#, WinForms: Settings speichern und lesen

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Info@CodeDocu.de