Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中将显示器发送到睡眠模式#_C# - Fatal编程技术网

C# 在c中将显示器发送到睡眠模式#

C# 在c中将显示器发送到睡眠模式#,c#,C#,这是一个标准的windows功能,显示在配置时间后进入睡眠模式。在Windows7中,是否有可能从c#.net应用程序立即将显示器发送到睡眠模式?我已经试过一件我发现的东西,但它对我不起作用 [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = Cha

这是一个标准的windows功能,显示在配置时间后进入睡眠模式。在Windows7中,是否有可能从c#.net应用程序立即将显示器发送到睡眠模式?我已经试过一件我发现的东西,但它对我不起作用

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

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();

private const int SC_MONITORPOWER = 0xF170;
private const UInt32 WM_SYSCOMMAND = 0x0112;
private const int MONITOR_ON = -1;
private const int MONITOR_OFF = 2;
private const int MONITOR_STANBY = 1;

public static void DisplayToSleep()
{
    var hWnd = GetDesktopWindow();
    var ret = SendMessage(hWnd , Constants.WM_SYSCOMMAND, (IntPtr)Constants.SC_MONITORPOWER, (IntPtr)Constants.MONITOR_OFF);
}
hWnd似乎有一个有效值,但ret始终为0


thx,kopi_b

这在WinForms应用程序中运行良好:

public partial class Form1 : Form
{
    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);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
    }
}

问题似乎来自
GetDesktopWindow
函数。

我有Visual Studio 2010和Windows 7,并创建了一个带有“睡眠”和“休眠”按钮的Windows窗体应用程序。以下几点对我很有用:

private void Sleep_Click(object sender, EventArgs e)
{
    bool retVal = Application.SetSuspendState(PowerState.Suspend, false, false);

    if (retVal == false)
        MessageBox.Show("Could not suspend the system.");
}

private void Hibernate_Click(object sender, EventArgs e)
{
    bool retVal = Application.SetSuspendState(PowerState.Hibernate, false, false);

    if (retVal == false)
        MessageBox.Show("Could not hybernate the system.");
}

我发现这

您需要使用
HWND\u广播
而不是桌面窗口手柄,以确保显示器关闭电源:

private const int HWND_BROADCAST = 0xFFFF;

var ret = SendMessage((IntPtr)HWND_BROADCAST, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MONITOR_OFF);

+1、同意。修复SendMessage()声明,它在64位操作系统上不起作用。@Hans,说得好,它实际上在我的Windows 7 x64位上起作用,但根据地址的不同,它可能会损坏。我把申报单修好了。@Hans,现在怎么样?像我那样把
int
转换成
IntPtr
正确吗?我想那会有用的,是的。但我不想挂起或休眠整个系统,只是显示器应该进入睡眠状态。