screen resolution - How can I change Windows 10 Display Scaling Programmatically using C# -


i'm trying find way change display scaling in windows 10 programmatically using c#.

let me that, i'm not trying create application automatically forces users screen change resolution/scaling. tool me beable toggle scales tray, have testing. purposely designed action.

so, able track down registry entries (hkey_current_user\control panel\desktop) set when user manually via official dialog seen below:

windows 10 display scaling dialog

however, working registry directly means need restart machine take affect.

i aware can use pinvoke change screen resolutions: setting display resolution

i wondering if there way change "%" given screen too? i.e.. screen above says 150%, i'd beable programmatically change through full range of 100-500%.

while searching same, found question , found possible solution.

i found per monitor toggle % value in registry @ computer\hkey_current_user\control panel\desktop\permonitorsettings\*monitorid*\dpivalue. looks meaning of value depends on screen (size , dpi) see this reddit post details.

for 24" 1080p screen 0 means 100% , 1 means 125%. this technet article seems explainig values bit.

unfortunately not enough change registry value. can refresh dpi changing resolution after writing registry.

the following code sets dpi , switches resolution low , high trigger dpi update.

using system; using system.windows.forms; using system.runtime.interopservices; using microsoft.win32;  namespace setdpiscale {     public partial class form1 : form     {         public enum dmdo         {             default = 0,             d90 = 1,             d180 = 2,             d270 = 3         }          [structlayout(layoutkind.sequential, charset = charset.auto)]         struct devmode         {             public const int dm_pelswidth = 0x80000;             public const int dm_pelsheight = 0x100000;             private const int cchdevicename = 32;             private const int cchformname = 32;              [marshalas(unmanagedtype.byvaltstr, sizeconst = cchdevicename)]             public string dmdevicename;             public short dmspecversion;             public short dmdriverversion;             public short dmsize;             public short dmdriverextra;             public int dmfields;              public int dmpositionx;             public int dmpositiony;             public dmdo dmdisplayorientation;             public int dmdisplayfixedoutput;              public short dmcolor;             public short dmduplex;             public short dmyresolution;             public short dmttoption;             public short dmcollate;             [marshalas(unmanagedtype.byvaltstr, sizeconst = cchformname)]             public string dmformname;             public short dmlogpixels;             public int dmbitsperpel;             public int dmpelswidth;             public int dmpelsheight;             public int dmdisplayflags;             public int dmdisplayfrequency;             public int dmicmmethod;             public int dmicmintent;             public int dmmediatype;             public int dmdithertype;             public int dmreserved1;             public int dmreserved2;             public int dmpanningwidth;             public int dmpanningheight;         }          [dllimport("user32.dll", charset = charset.auto)]         static extern int changedisplaysettings([in] ref devmode lpdevmode, int dwflags);          public form1()         {             initializecomponent();         }          private void button1_click(object sender, eventargs e)         {             changedpi(0); // 100%         }         private void button2_click(object sender, eventargs e)         {             changedpi(1); // 125%         }          void changedpi(int dpi)         {             registrykey key = registry.currentuser.opensubkey("control panel", true);              key = key.opensubkey("desktop", true);             key = key.opensubkey("permonitorsettings", true);             key = key.opensubkey("*monitor id change dpi*", true); // second monitor here              key.setvalue("dpivalue", dpi);              setresolution(1920, 1080); // sets resolution on primary screen             setresolution(2560, 1440); // returning primary screens default resolution         }          private static void setresolution(int w, int h)         {             long retval = 0;              devmode dm = new devmode();              dm.dmsize = (short)marshal.sizeof(typeof(devmode));              dm.dmpelswidth = w;             dm.dmpelsheight = h;              dm.dmfields = devmode.dm_pelswidth | devmode.dm_pelsheight;               retval = changedisplaysettings(ref dm, 0);         }     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -