how can I temporarily blank a Windows-7 2nd display monitor, in C#?

I try setting its width and height to 0 using ChangeDisplaySettingsEx, which blanks the display monitor, but then when I set W and H back (and also restore dmPosition.x) it just stays off.

My system has two monitors, and I'm trying to temporarily blank one of them. Later, I need to turn it back on.

HERE'S THE 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 to 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 to be 0
                    ref display_setting) ;
        }
        else
        {
            return_value = EnumDisplaySettings(lpDisplayDevice.DeviceName,
                    ENUM_CURRENT_SETTINGS,                                          ///////<<<<<<<<< might need to be 0
                    ref display_setting);
        }
        if(return_value > 0)// Succeeded
        {
            Console.WriteLine("Current Mode:\n\t" +
                "{0}w by {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 to landscape
            //display_setting.dmDisplayOrientation = 2;   // set landscape orientation to landscape upside down

            if (monitor_on_off)
            {
                // Set resolution to 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.  <<<<<<<<<<<<<<");
        }
    }

Answers


Here is the solution that I found and 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);
    }

Need Your Help

Partial bold text in asp.net

javascript jquery asp.net

This is how I am adding asp.net label,

How to invite users through my app on whatsapp and share content IOS?

ios xcode6 share whatsapp invite

I have tried this code but its showing only whatsapp icon, but not sending content on it.Kindly suggest what's wrong I am doing

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.