how can I temporarily blank a Windows-7 2nd display monitor, in C#? -
i try setting width , height 0 using changedisplaysettingsex, blanks display monitor, when set w , h (and restore dmposition.x) stays off.
my system has 2 monitors, , i'm trying temporarily blank 1 of them. later, need turn on.
here's code.................
public static void set_monitor_settings( int devnum, bool monitor_on_off ) { if( monitor_on_off ) console.writeline("turn on ultrasound monitor"); else console.writeline("turn off ultrasound monitor"); // init: display_device lpdisplaydevice = new display_device(0); // out display_device monitor_name = new display_device(0); // out devmode display_setting = new devmode(); lpdisplaydevice.cb = marshal.sizeof(lpdisplaydevice); display_setting.dmsize = (ushort)marshal.sizeof(display_setting); // set lpdisplaydevice select 2nd display device: enumdisplaydevices(null, devnum, ref lpdisplaydevice, 0); console.writeline("\ndevnum =" + devnum); console.writeline("cb =" + lpdisplaydevice.cb); console.writeline("deviceid =" + lpdisplaydevice.deviceid); console.writeline("devicekey =" + lpdisplaydevice.devicekey); console.writeline("devicename =" + lpdisplaydevice.devicename.trim()); console.writeline("devicestring =" + lpdisplaydevice.devicestring.trim()); // open settings of 2nd device: int return_value; if( monitor_on_off ) { return_value = enumdisplaysettings(lpdisplaydevice.devicename, 0, //enum_current_settings, ///////<<<<<<<<< might need 0 ref display_setting) ; } else { return_value = enumdisplaysettings(lpdisplaydevice.devicename, enum_current_settings, ///////<<<<<<<<< might need 0 ref display_setting); } if(return_value > 0)// succeeded { console.writeline("current mode:\n\t" + "{0}w {1}h, " + "{2} bit, " + "{3} degrees, " + "{4} hertz", display_setting.dmpelswidth, display_setting.dmpelsheight, display_setting.dmbitsperpel, display_setting.dmdisplayorientation * 90, display_setting.dmdisplayfrequency); //console.writeline(display_setting.dmdisplayorientation + "\t = raw orientation"); display_setting.dmdisplayorientation = 0; // set landscape orientation landscape //display_setting.dmdisplayorientation = 2; // set landscape orientation landscape upside down if (monitor_on_off) { // set resolution g4.1 (april 2013) requirement: display_setting.dmpelswidth = 1280; display_setting.dmpelsheight = 1024; display_setting.dmposition.x = 1200; } else { display_setting.dmpelswidth = 0; display_setting.dmpelsheight = 0; } int iret = changedisplaysettingsex(lpdisplaydevice.devicename, ref display_setting, intptr.zero, cds_updateregistry, intptr.zero); if (iret == 0) console.writeline("successful"); else console.writeline(iret + "\t = changedisplaysettingsex return value "); } else { console.writeline(">>> enumdisplaysettings returned 0. <<<<<<<<<<<<<<"); } }
here solution found , used successfully......
[dllimport("user32.dll")] static extern intptr sendmessage(intptr hwnd, int msg, intptr wparam, intptr lparam); private const int wm_syscommand = 0x0112; private const int sc_monitorpower = 0xf170; private const int monitorturnon = -1; private const int monitorshutoff = 2; //turn them off public static void turn_off_monitors() { form f = new form(); sendmessage(f.handle, wm_syscommand, (intptr)sc_monitorpower, (intptr)monitorshutoff); } //turn them on public static void turn_on_monitors() { form f = new form(); sendmessage(f.handle, wm_syscommand, (intptr)sc_monitorpower, (intptr)monitorturnon); }
Comments
Post a Comment