July 20

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace MonitorPowerSetting
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        int HWND_BROADCAST = 0xFFFF;
        int WM_SYSCOMMAND = 0x0112;
        int SC_MONITORPOWER = 0xF170;

        [DllImport("user32.dll")]
        private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

        /*
Start the registry editor (regedit.exe)
Move to HKEY_USERS/.DEFAULT/Control Panel/Desktop
Double click PowerOffActive and set to 1. Click OK
Double click ScreenSaveActive and set to 1. Click OK
Double click SCRNSAVE.EXE (if present) and set to "(NONE)" (don't type the quotes). Click OK
Move to HKEY_USERS/.DEFAULT/Control Panel/PowerCfg
Double click CurrentPowerPolicy and set to 0 (the Home/Office Desk power setting configuration). Click OK
Close the registry editor
Reboot the machine
To change the settings such as monitor timeout change the settings on your local profile, export HKEY_CURRENT_USER/Control Panel/PowerCfg/PowerPolicies/0 to a file and import to HKEY_USERS/.DEFAULT/Control Panel/PowerCfg/PowerPolicies/0.

If you need to do this on lots of machines create a .reg file and push via a login/startup script (which can be specified via Group Policy).
         */

        private void btnTurnOff_Click(object sender, RoutedEventArgs e)
        {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);//DLL function
        }

        private void btnOneMin_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btnNormal_Click(object sender, RoutedEventArgs e)
        {
            RegistryKey hklm = Registry.Users;
            RegistryKey subkey = hklm.OpenSubKey(@".DEFAULT", false);
            subkey = subkey.OpenSubKey(@"Control Panel", false);
            subkey = subkey.OpenSubKey(@"PowerCfg", true);

            subkey.SetValue("CurrentPowerPolicy", "0");
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值