使用的NativeWindow禁用屏幕保护程序 [英] Use NativeWindow to disable screensaver

查看:112
本文介绍了使用的NativeWindow禁用屏幕保护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要禁用屏幕和显示器的电源关闭。在这个阶段,没有窗户的形式,我可以忧色。因此,我wan't使用的NativeWindow。

I want to disable the screensaver and monitor power off. At this stage there's no windows form, which I could youse. Thus I wan't to use NativeWindow.

下面是我的代码

sealed class ObserverWindow : NativeWindow, IDisposable
{
    internal ObserverWindow()
    {
        this.CreateHandle(new CreateParams()
        {
            Parent= IntPtr.Zero
        });

    }

    public void Dispose()
    {
        DestroyHandle();
    }

    protected override void WndProc(ref Message msg)
    {
        if (msg.Msg == WM_SYSCOMMAND &&
            ((((long)msg.WParam & 0xFFF0) == SC_SCREENSAVE) ||
            ((long)msg.WParam & 0xFFF0) == SC_MONITORPOWER))
        {
            msg.Msg = 0;
            msg.HWnd = IntPtr.Zero;
        }
        base.WndProc(ref msg);
    }
}



现在的问题是,该WndProc的不叫带WM_SYSCOMMAND。 Actualy WndProc中被称为4倍。在最后一次通话有msg.Msg == WM_CREATE。

The Problem is, that the WndProc is not called with WM_SYSCOMMAND. Actualy the WndProc is called 4 times. At the last call there's msg.Msg == WM_CREATE.

我想我错过了一些创建参数。有没有人有建议?

I think I'm missing some create parameter. Does anyone have advise?

问候迈克尔

更新

我是跑在非STA线程的代码。因此,窗口没有reveive exept最初的人的任何消息。现在,我收到WM_SYSCOMMAND消息。但是,当屏幕保护程序被激活,没有消息。

I was running the code in a non STA thread. Thus the window did not reveive any messages exept the initial ones. Now I'm receiving WM_SYSCOMMAND messages. But when the screensaver is activated, there's no message.

我也试过来覆盖窗体的WndProc具有相同的结果。但是,这用在Windows XP中工作。有没有在Windows 7中的变化

I also tried to overwrite a Form's WndProc with the same result. But this used to work in Windows XP. Is there a change in Windows 7?

操作系统:?Windows 7的64位

OS: Windows 7 64bit.

SOLUTION

在本的问题状态,只有前台窗口可以取消屏幕保护程序。因此,上面的代码不能工作。的NativeWindow是伟大的接收消息,但不取消屏幕保护程序。对于后者我建议这个问题的答案。

As a comment in this Question states, only the foreground window can cancel the screensaver. Thus the above code can't work. The NativeWindow is great for receiving messages, but not for canceling a screensaver. For latter I recommend the answer to this question.

推荐答案

要做到这一点,正确的方法是,告诉的Windows,你的线程需要具有显示活性。常用的视频播放器。 P /调用SetThreadExecutionState()API函数,通过ES_DISPLAY_REQUIRED。而ES_SYSTEM_REQUIRED保持机器自动关闭。访问pinvoke.net为所需的声明。

The proper way to do this is by telling Windows that your thread needs to have the display active. Commonly used by video players. P/Invoke the SetThreadExecutionState() API function, pass ES_DISPLAY_REQUIRED. And ES_SYSTEM_REQUIRED to keep the machine from shutting down automatically. Visit pinvoke.net for the required declarations.

这篇关于使用的NativeWindow禁用屏幕保护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆