调用Windows API来实现一些.NET框架本身不直接支持的功能

cs 复制代码
	public static class ModifyProgressBarColor
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
		static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);

		//Note the second parameter in SetState, 1 = normal (green); 2 = error (red); 3 = warning (yellow).
		public static void SetState(this System.Windows.Forms.ProgressBar pBar, int state)
		{
			SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
		}
	}

	class WinAPI
	{


		//SetThreadExecutionState
		//The .NET framework classes don't provide a way to disable the screensaver or sleep mode, so we need to use Platform Invocation Services (P/Invoke) to call a Windows API function named, "SetThreadExecutionState". This function tells the operating system that the thread is in use, even if the user is not interacting with the computer. This can prevent the display from being hidden and stop the machine from being suspended automatically.
		//SetThreadExecutionState uses a series of flags to specify a new state for the current thread. We'll define these flags shortly using an enumeration with the Flags attribute. You can use the logical OR operator to combine several flags and specify multiple behaviours with a single call. The function returns a value made up from the same flags. The return value indicates the state before the changes that you requested, or returns null if there is an error.
		//The four flags that we are interested in are:
		//ES_DISPLAY_REQUIRED. This flag indicates that the display is in use. When passed by itself, the display idle timer is reset to zero once. The timer restarts and the screensaver will be displayed when it next expires.
		//ES_SYSTEM_REQUIRED. This flag indicates that the system is active. When passed alone, the system idle timer is reset to zero once. The timer restarts and the machine will sleep when it expires.
		//ES_CONTINUOUS. This flag is used to specify that the behaviour of the two previous flags is continuous. Rather than resetting the idle timers once, they are disabled until you specify otherwise. Using this flag means that you do not need to call SetThreadExecutionState repeatedly.
		//ES_AWAYMODE_REQUIRED. This flag must be combined with ES_CONTINUOUS. If the machine is configured to allow it, this indicates that the thread requires away mode. When in away mode the computer will appear to sleep as normal. However, the thread will continue to execute even though the computer has partially suspended. As this flag gives the false impression that the computer is in a low power state, you should only use it when absolutely necessary.


		[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
		private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

		[FlagsAttribute]
		private enum EXECUTION_STATE :uint
		{
			 ES_AWAYMODE_REQUIRED = 0x00000040,
			 ES_CONTINUOUS = 0x80000000,
			 ES_DISPLAY_REQUIRED = 0x00000002,
			 ES_SYSTEM_REQUIRED = 0x00000001
			 // Legacy flag, should not be used.
			 // ES_USER_PRESENT = 0x00000004
		}

		public static void SignalActvity()
		{
			try{SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED);}
			catch { }
		}

		// Clear EXECUTION_STATE flags to disable away mode and allow the system to idle to sleep normally.
		public static void SignalFree()
		{
			try{SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);}
			catch { }
		}



	}

这段代码主要包含两个部分的功能:

修改进度条颜色:

通过调用Windows API函数SendMessage来改变ProgressBar控件的状态,从而改变其颜色。

SetState方法接受一个ProgressBar对象和一个状态值作为参数,状态值1表示正常(绿色),2表示错误(红色),3表示警告(黄色)。

阻止系统进入睡眠模式:

使用SetThreadExecutionState API函数来阻止系统进入睡眠模式或关闭显示器。

SignalActvity方法通过设置特定的标志来告诉系统当前线程正在使用,从而阻止系统进入睡眠模式。

SignalFree方法则清除这些标志,允许系统正常进入睡眠模式。

这段代码展示了如何使用P/Invoke技术调用Windows API来实现一些.NET框架本身不直接支持的功能。

相关推荐
明月看潮生2 小时前
Windows_PyCharm Python语言开发环境构建
windows·python·青少年编程·pycharm·编程与数学
Rocket MAN3 小时前
在 Ubuntu 中配置 Samba 实现「特定用户可写,其他用户只读」的共享目录
网络·windows·ubuntu
胡耀超4 小时前
内网渗透技术全面指南——安全业务视角(基于《内网渗透技术 (吴丽进、苗春雨 主编;郑州、雷珊珊、王伦 副主编)》)
linux·windows·安全·web安全·网络安全·内网渗透·安全治理
weixin_464078075 小时前
.NET 多线程题目汇总
算法·.net
扛枪的书生7 小时前
AD 侦查-SMB_2
windows·渗透·kali·提权·域渗透
QX_hao11 小时前
【firewall-cmd】--的作用以及使用方法
服务器·网络·windows
love530love12 小时前
PyCharm 加载不了 conda 虚拟环境,不存在的
ide·windows·pycharm·conda
一起喝芬达201018 小时前
【Agent】使用 Python 结合 OpenAI 的 API 实现一个支持 Function Call 的程序,修改本机的 txt 文件
windows·python·microsoft
不懂英语的程序猿18 小时前
【Trea】Trea国际版|海外版下载
windows
LiLiYuan.20 小时前
关于Stream
java·开发语言·windows·python