当前位置:  开发笔记 > 编程语言 > 正文

打开/关闭显示器

如何解决《打开/关闭显示器》经验,为你挑选了6个好方法。

是否可以通过代码(C#)打开/关闭显示器?



1> Wim Haanstra..:

你有没有尝试谷歌搜索?

首先访问:http: //www.codeproject.com/KB/cs/Monitor_management_guide.aspx

我不需要使用Windows提供的一些DLL.

(我猜你需要一个C#解决方案,因为那是你应用的唯一标签).

编辑2013年2月8日:

有人提到该解决方案不再适用于Windows 7和8.这里有一个在Windows 7下运行良好的解决方案,还没有尝试过Windows 8.

http://cocoa.ninja/posts/Turn-off-your-monitor-in-Csharp.html

namespace MonitorOff {

    public enum MonitorState {
        MonitorStateOn = -1,
        MonitorStateOff = 2,
        MonitorStateStandBy = 1
    }

    public partial class Form1 : Form {
        [DllImport("user32.dll")]
        private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

        public Form1() {
            InitializeComponent();
            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
        }

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }

        private void button1_Click(object sender, EventArgs e) {
            SetMonitorInState(MonitorState.MonitorStateOff);
        }

        private void SetMonitorInState(MonitorState state) {
            SendMessage(0xFFFF, 0x112, 0xF170, (int)state);
        }
    }
}


我这样做,但由于答案仍然在谷歌2013年排名很高,我认为像我这样的其他人会来,看到这个,然后下载并尝试该项目,发现它在2009年后的Windows操作系统中不起作用.我试图将它们保存10分钟以上.我决不会试图剥夺你的答案增加的价值,我相信它帮助了成千上万的人,我只是想让人们知道一些事情已经发生了变化.

2> Lee..:

按开/关按钮


如果你想在代码中这样做,显然这可以在Win32 API中实现:

SendMessage hWnd,WM_SYSCOMMAND,SC_MONITORPOWER,param

其中WM_SYSCOMMAND = 0x112和SC_MONITORPOWER = 0xF170,param表示将监视器置于的模式:-1:开2:关1:节能模式

hWnd可以是任何窗口的句柄 - 所以如果你有一个表单,这样的东西应该有效

int WM_SYSCOMMAND = 0x112;
int SC_MONITORPOWER = 0xF170;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

public static void Main(string[] args)
{
    Form f = new Form();
    bool turnOff = true;   //set true if you want to turn off, false if on
    SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ? 2 : -1));
}

注意我还没有尝试过这个......


它可以很好地关闭显示器,但我似乎无法让它打开.

3> Quinxy von B..:

上面的答案/sf/ask/17360801/非常适合关闭Windows 7/8显示器但不能唤醒它.在这些系统上你需要像这样做一些hackish(如/sf/ask/17360801/):

[DllImport("user32.dll")]
static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

private const int MOUSEEVENTF_MOVE = 0x0001;

private void Wake(){
mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);
Sleep(40);
mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, UIntPtr.Zero);
}



4> Saleh Parsa..:

此代码可用于打开和关闭..它也适用于Windows 7.

   private int SC_MONITORPOWER = 0xF170;

    private uint WM_SYSCOMMAND = 0x0112;

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);



    enum MonitorState
    {
        ON = -1,
        OFF = 2,
        STANDBY = 1
    }
    private void SetMonitorState(MonitorState state)
    {
        Form frm = new Form();

        SendMessage(frm.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)state);

    }

要调用该函数,您必须执行以下操作:

SetMonitorState(MonitorState.ON);

要么

SetMonitorState(MonitorState.OFF);

注意:此代码在WPF应用程序中测试.使用以下命名空间:

using System.Runtime.InteropServices;
using System.Windows.Forms;



5> 小智..:

对于谁在控制台应用程序上想要此功能:

using System;
using System.Runtime.InteropServices;
using System.Timers;

namespace TurnScreenOFF
{
    class Program
    {
        private static int WM_SYSCOMMAND = 0x0112;
        private static uint SC_MONITORPOWER = 0xF170;

        public static void Main(string[] args)
        {
            SendMessage(GetConsoleWindow(), WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
        }

        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
    }
}

适应和测试.100%在Windows 8上工作.


无需调用GetConsoleWindow,也无需在非控制台应用程序上获取GUI hwnd,只需使用HWND_BROADCAST消息(**SendMessage(New IntPtr(0xFFFF)**...).

6> klaasjan69..:

我找不到复制粘贴示例,因此我自己创建了一个,不要忘记添加对System.Windows.Forms的引用.

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;


namespace monitor_on_off
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

        private const int WmSyscommand = 0x0112;
        private const int ScMonitorpower = 0xF170;
        private const int MonitorShutoff = 2;
        private const int MouseeventfMove = 0x0001;

        public static void MonitorOff(IntPtr handle)
        {
            SendMessage(handle, WmSyscommand, (IntPtr)ScMonitorpower, (IntPtr)MonitorShutoff);
        }

        private static void MonitorOn()
        {
            mouse_event(MouseeventfMove, 0, 1, 0, UIntPtr.Zero);
            Thread.Sleep(40);
            mouse_event(MouseeventfMove, 0, -1, 0, UIntPtr.Zero);
        }

        static void Main()
        {
            var form = new Form();

            while (true)
            {
                MonitorOff(form.Handle);
                Thread.Sleep(5000);
                MonitorOn();
                Thread.Sleep(5000);
            }
        }
    }
}

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有