windows系统壁纸和休眠模式配置

27 篇文章 0 订阅
5 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Start10
{
    public class SystemHelper
    {
        [StructLayout(LayoutKind.Sequential, Pack = 1)]

        internal struct TokPriv1Luid
        {

            public int Count;

            public long Luid;

            public int Attr;

        }

        [DllImport("kernel32.dll", ExactSpelling = true)]

        internal static extern IntPtr GetCurrentProcess();

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

        [DllImport("advapi32.dll", SetLastError = true)]

        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool ExitWindowsEx(int flg, int rea);

        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

        internal const int TOKEN_QUERY = 0x00000008;

        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

        internal const int EWX_LOGOFF = 0x00000000;

        internal const int EWX_SHUTDOWN = 0x00000001;

        internal const int EWX_REBOOT = 0x00000002;
        /// <summary>
        /// 包含这个参数可以让系统强制关机,可能会让应用程序丢失数据
        /// </summary>
        internal const int EWX_FORCE = 0x00000004;
        /// <summary>
        /// 
        /// </summary>
        internal const int EWX_POWEROFF = 0x00000008;
        /// <summary>
        /// 如果在超时时间以后应用进程仍然没有响应WM_QUERYENDSESSION或WM_ENDSESSION消息,那么就强制关闭它们。
        /// </summary>
        internal const int EWX_FORCEIFHUNG = 0x00000010;

        public static void DoExitWin(int flg)
        {

            bool ok;

            TokPriv1Luid tp;

            IntPtr hproc = GetCurrentProcess();

            IntPtr htok = IntPtr.Zero;

            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);

            tp.Count = 1;

            tp.Luid = 0;

            tp.Attr = SE_PRIVILEGE_ENABLED;

            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);

            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);

            ok = ExitWindowsEx(flg, 0);

        }
        /// <summary>
        /// 锁屏
        /// </summary>
        /// <returns></returns>
        [DllImport("user32 ")]
        public static extern bool LockWorkStation();//这个是调用windows的系统锁定

        /// <summary>
        /// WTSDisconnectSession来断开(而不是终止)当前会话,从而进入用户切换界面。
        /// </summary>
        /// <param name="hServer"></param>
        /// <param name="sessionId"></param>
        /// <param name="bWait"></param>
        /// <returns></returns>
        [DllImport("Wtsapi32.dll", SetLastError = true)]
        extern static bool WTSDisconnectSession(IntPtr hServer, int sessionId, [MarshalAs(UnmanagedType.Bool)] bool bWait);

        public static void switchSystemUsersMethod()
        {
            IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
            int WTS_CURRENT_SESSION = -1;

            if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false))
            {
                Console.WriteLine("WTSDisconnectSession Failed: " + Marshal.GetLastWin32Error());
            }
        }

        const uint SPI_GETSCREENSAVERRUNNING = 0x0072;

        const uint SPI_GETSCREENSAVEACTIVE = 0x0010;
        const uint SPI_SETSCREENSAVEACTIVE = 0x0011;
        const uint SPIF_SENDCHANGE = 0x0002;
        const uint SPIF_SENDWININICHANGE = SPIF_SENDCHANGE;

        const uint SPI_GETDESKWALLPAPER = 0x0073;
        const uint SPI_SETDESKWALLPAPER = 0x0014;
        const uint SPIF_UPDATEINIFILE = 0x001;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool SystemParametersInfo(uint uAction, uint uParam, StringBuilder lpvParam, uint init);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, string path, uint fWinIni);

        /// <summary>
        /// 判读系统屏保是否正在运行
        /// </summary>
        /// <returns></returns>
        public static bool IsScreenSaverRunning()
        {
            bool isRun = false;
            SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRun, 0);
            return isRun;
        }

        /// <summary>
        /// 设置系统进程模式
        /// </summary>
        /// <param name="esFlags"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        static extern uint SetThreadExecutionState(uint esFlags);
        const uint ES_SYSTEM_REQUIRED = 0x00000001;
        const uint ES_DISPLAY_REQUIRED = 0x00000002;
        const uint ES_CONTINUOUS = 0x80000000;

        /// <summary>
        /// 播放时调用,禁止系统休眠
        /// </summary>
        public static void SetSystemMode()
        {
            //播放时调用,     
            SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
        }
        /// <summary>
        /// 恢复系统正常
        /// </summary>
        public static void SetSystemNormalMode()
        {
            //播放结束后调用 ,恢复系统正常    
            SetThreadExecutionState(ES_CONTINUOUS);
        }

        /// <summary>
        /// 设置桌面壁纸
        /// </summary>
        /// <param name="path"></param>
        public static void SetWallPaper(string path)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER,
                                0, path,
                                SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);

        }

        /// <summary>
        /// 获取桌面壁纸图片路径
        /// </summary>
        /// <returns></returns>
        public static string GetWallPaper()
        {
            StringBuilder wallPaperPath = new StringBuilder(200);
            if (SystemParametersInfo(SPI_GETDESKWALLPAPER, 200, wallPaperPath, 0))
            {
                return wallPaperPath.ToString();
            }
            return "";
        }

    }
}

调用:


            string path = SystemHelper.GetWallPaper();
            string path2 = @"C:\Users\Administrator\Desktop\white.png";
            SystemHelper.SetWallPaper(path2);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值