在C#中以编程方式打开屏幕保护程序 [英] Turn on screensaver programmatically in C#

查看:74
本文介绍了在C#中以编程方式打开屏幕保护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要C#代码以编程方式打开/关闭Windows屏幕保护程序.
我用谷歌搜索并找到了一些代码.

I need C# code to turn on/off windows screensaver programmatically.
I googled and found some code.

[DllImport("User32.dll")]
public static extern int SendMessage
    (IntPtr hWnd,
    uint Msg,
    uint wParam,
    uint lParam);
public const uint WM_SYSCOMMAND = 0x112;
public const uint SC_SCREENSAVE = 0xF140;
public enum SpecialHandles
{
    HWND_DESKTOP = 0x0,
    HWND_BROADCAST = 0xFFFF
}
public static void TurnOnScreenSaver()
{
    SendMessage(
        new IntPtr((int)SpecialHandles.HWND_BROADCAST),
        WM_SYSCOMMAND,
        SC_SCREENSAVE,
        0);
}



它是完整的代码吗?哪里是主要方法?如何执行?
我应该在此代码中添加Qhat吗?

我使用:
.NET Framework 3.5
Windows XP Professional Service Pack 2



Is it complete code? Qhere is the main method? How can i execute it?
Qhat should I add to this code?

I use:
.NET framework 3.5
Windows XP professional service pack 2

推荐答案

是的,应该工作-您要做的就是在要使用它时调用TurnOnScreenSaver.
例如:
Yes that should work - all you have to do is call TurnOnScreenSaver when you want to use it.
For example:
private void button1_Click_1(object sender, EventArgs e)
    {
    Timer t = new Timer();
    t.Interval = 2000;
    t.Tick += new EventHandler(t_Tick);
    t.Start();
    }
void t_Tick(object sender, EventArgs e)
    {
    Timer t = sender as Timer;
    if (t != null)
        {
        t.Stop();
        }
    TurnOnScreenSaver();
    }


感谢您的回复.效果很好.
我需要另一个方法如TurnOnScreenSaver()来停止屏幕保护程序.那就是我想先打开屏幕保护程序(已经完成),然后在特定条件下我要关闭屏幕保护程序并继续该过程(打开和关闭).我不想使用计时器.
Thanks for your reply. that works fine.
i need another method like TurnOnScreenSaver() that stops the screen saver. That is i wanna turn on the screen saver first (already done) then on a specific condition i wanna turn off the screen saver and continue the process(on and off) . i don''t wanna use Timer.


这篇关于在C#中以编程方式打开屏幕保护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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