简体   繁体   English

如何在C#中暂时清空Windows-7第二显示器?

[英]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. 我尝试使用ChangeDisplaySettingsEx将其宽度和高度设置为0,这会使显示监视器空白,但是当我将W和H设置回(并且还恢复dmPosition.x)时,它会保持关闭状态。

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.  <<<<<<<<<<<<<<");
        }
    }

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

You can use DisplaySwitch app to do this : 您可以使用DisplaySwitch应用程序执行此操作:

String cmd = "DisplaySwitch";
String argument = "/internal";
System.Diagnostics.Process.Start(cmd, argument);

if you want the other monitor, use : 如果你想要其他显示器,请使用:

String argument = "/external";

To retrieve the dual screen : 要检索双屏幕:

String argument = "/extend";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中为 Windows-7 设置主监视器 - how to set primary monitor for Windows-7, in C# C#(对象发送者,GridViewCommandEventArgs e)如何为第二部分做些过分的准备? - C# (object sender, GridViewCommandEventArgs e) How can I overgive something for the 2nd part? 使用C#创建Windows-7库 - Creating windows-7 library using c# C#套接字:第二个(及更多)用户无法连接 - C# Sockets: 2nd (and more) users can't connect 如何在python和C#中通过第二个元组元素对列表进行排序 - How to sort a list by the 2nd tuple element in python and C# 如何从 c# 数组中的第 2 项切片元素? - How to slice elements from 2nd item in a c# array? 在 C#/NUnit 测试用例中从 Excel 连续读取数据,返回第二个用例的空白数据 - Consecutively reading data from Excel in C#/NUnit test cases, returns blank data for 2nd Case 如何监视C#Windows应用程序的状态? - How do I monitor the status of an C# windows application? 我如何通过使用foreach循环一列和另一个foreach填充第二列来填充C#列表视图,以便第二个循环不会首先覆盖 - How can i fill a C# listview by using foreach loop a column and another foreach for second column so that 2nd loop do not over write first 控制第二个cmd窗口-C# - Controlling 2nd cmd window - c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM