【问题标题】:My screensaver is stopping screen suspend but I don't want it to我的屏幕保护程序正在停止屏幕暂停,但我不希望它停止
【发布时间】:2012-06-05 15:42:25
【问题描述】:

我在 .Net 4.0 中制作了屏幕保护程序。它基本上只是移动图像中的位并使用计时器上的 .Invalidate 并覆盖 onPaint 事件来显示它。

到目前为止,它运行良好 - 但是 - 我注意到它存在一个问题。

它在暂停超时后停止监视器暂停。自从我安装了它,我的显示器现在 24/7 全天候运行。

问题是,我没有做任何事情来专门停止省电功能 - 我已确保我的计算机的省电设置已设置(它们是)。因此,我选择了另一个屏幕保护程序,以确保设置仍然有效。超时后监视器暂停。

然后我需要做什么才能很好地使用电源管理?我在谷歌上搜索了这个答案,我发现的一切都是如何阻止电源管理,我没有明确阻止它!我只想在适当的时候允许暂停。

【问题讨论】:

    标签: c# c#-4.0 power-management


    【解决方案1】:

    您是否会意外捕获WM_SYSCOMMAND 的 0xF170 (SC_MONITORPOWER) 子命令?

    【讨论】:

    • 回答这个评论,不,我不是。但是,我考虑这样做并在收到消息后暂停绘图程序。这种考虑最终导致了我现在要添加并接受的解决方案作为答案。尽管如此,你还是一路走来的关键踏脚石,我想让你知道,因为我投票支持你。
    • 很高兴我至少能帮上点忙 ;)
    【解决方案2】:

    我能够让我的程序“玩得很好”。我不知道为什么这有效而原始代码无效,但这不仅有效 - 它实际上使程序更加“节能”友好,因为它通过在屏幕暂停后不进行计算来减少 CPU 周期。简而言之,我预览 WndProc 消息并查找监视器正在暂停消息,一旦收到它,我就会停止重绘,直到它恢复(可以恢复并使屏幕保护程序仍然处于活动状态)。

    代码更改:

        // Field Definitions
        /// <summary>
        /// Constants that relate to the WndProc messages we wish to intercept and evaluate.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "Standard practice to use this naming style for Win32 API Constants.")]
        private const int WM_SYSCOMMAND = 0x0112, SC_MONITORPOWER = 0xF170;
    
        /// <summary>
        /// Gets or sets whether we are suspended. Should coincide with whether the display is turned on or not.
        /// </summary>
        private bool isSuspended = false;
    
    
        // New overridden method
        /// <summary>
        /// Intercepts WndProc messages. We are looking for the screen suspend activity. From it, we will return that we are able to suspend and we ourselves will suspend.
        /// </summary>
        /// <param name="m">Message to be checked.</param>
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                // The 0x000F bits are used to indicate the specific state and must be ignored to see if this is a monitor power event.
                if ((m.WParam.ToInt32() & 0xFFF0) == SC_MONITORPOWER)
                {
                    switch (m.WParam.ToInt32() & 0x000F)
                    {
                        case -1:
                            // Display powering on - resume operation
    #if DEBUG
                            System.Diagnostics.Debug.WriteLine("Display powered on.");
    #endif
                            this.isSuspended = false;
                            break;
                        case 0:
                        case 1:
                        case 2:
                            // Display being powered off - suspend operation
    #if DEBUG
                            System.Diagnostics.Debug.WriteLine("Display suspended");
    #endif
                            this.isSuspended = true;
                            break;
                        default:
    #if DEBUG
                            System.Diagnostics.Debug.WriteLine(string.Format("Unknown power state: {0}", (m.WParam.ToInt32() & 0x000F).ToString("0")));
    #endif
                            // Assuming that unknown values mean to power off. This is a WAG.
                            this.isSuspended = true;
                            break;
                    }
                }
            }
    
            base.WndProc(ref m);
        }
    
    
        // Change to my refreshing timer.
        /// <summary>
        /// Called when the refresh timer ticks. This invalidates the form, forcing it to be redrawn, which creates a framerate for us.
        /// </summary>
        /// <param name="sender">Who called this method.</param>
        /// <param name="e">Event Arguments.</param>
        private void RefreshTimer_Tick(object sender, EventArgs e)
        {
            if (this.isSuspended)
            {
                // Program is in suspended mode, so don't do anything this update.
                return;
            }
    
            // Program is not suspended, so invalidate the client area so it can be painted again.
            this.Invalidate();
        }
    

    进行此更改会在调用挂起时停止所有重绘(并停止 GDI+ 计算),并且在进行此更改后,屏幕保护程序会根据电源管理设置“运行”。

    【讨论】:

    • 非常酷——如果你不介意的话,我可能会用它作为模板来更改我们拥有的监控应用程序,它在运行时会对数据库造成很大的影响,当屏幕出现时毫无意义无论如何都不会显示。
    • 我一点也不介意,这部分是你自己的成功!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2012-10-08
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多