⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apicalls.cs

📁 c#写的聊天程序
💻 CS
字号:


namespace MySendClient
{
	using System;
	using System.Runtime.InteropServices;  
	
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	public class ApiCalls
	{
		public ApiCalls()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		

		//	在类定义主体中,以静态调用的方式加入对API的引用,本文以下的API调用为例:

		/// <summary>
		///		打开和关闭CD托盘.
		///		打开CD托盘:
		///		long lngReturn = ApiCalls.mciSendString("set CDAudio door open", strReturn, 127, 0);
		///		关闭CD托盘:
		///		long lngReturn = ApiCalls.mciSendString("set CDAudio door closed", strReturn, 127, 0);
		/// </summary>
		[DllImport("winmm.dll" , EntryPoint="mciSendString", CharSet=CharSet.Auto)]
		public  static extern int  mciSendString (string  lpstrCommand,string  lpstrReturnstring ,int  uReturnLength,int  hwndCallback);

       

		/// <summary>
		/// 显示和隐藏鼠标指针. 1显示,0隐藏
		/// </summary>
		[DllImport("user32.dll", EntryPoint="ShowCursor", CharSet=CharSet.Auto)]
		public  static extern int  ShowCursor(int  bShow);

 

		/// <summary>
		/// 清空回收站.
		/// ApiCalls.SHEmptyRecycleBin(Form.ActiveForm.Handle,"",0x00000000);
		/// </summary>
		[DllImport("shell32.dll", EntryPoint="SHEmptyRecycleBin", CharSet=CharSet.Auto)]
		public static extern long SHEmptyRecycleBin(IntPtr  hwnd, string pszRootPath, long dwFlags);

 

		/// <summary>
		/// 打开浏览器
		/// textBox1.Text中表示要访问的URL地址:
		///	Long lngReturn= ApiCalls.ShellExecute(Form.ActiveForm.Handle,"Open",textBox1.Text,"","",1);
		/// </summary>
		[DllImport("shell32.dll", EntryPoint="ShellExecute", CharSet=CharSet.Auto)]
		public  static extern int  ShellExecute(IntPtr hwnd,string  lpOperation,string  lpFile,string  lpParameters,string  lpDirectory,int  nShowCmd);

 

		/// <summary>
		/// 最大化窗口 X=3,最小化窗口 X=2,正常大小窗口 X=1;
		/// ApiCalls.ShowWindow(Form.ActiveForm.Handle,X);
		/// </summary>
		[DllImport("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Auto)]
		public  static extern int  ShowWindow(IntPtr hwnd,int  nCmdShow);

		#region 关机
		[StructLayout(LayoutKind.Sequential, Pack = 1)]
			internal struct TokPriv1Luid
		{
			public int Count;
			public long Luid;
			public int Attr;
		}

		[DllImport("kernel32.dll", ExactSpelling = true)]
		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;
		internal const int EWX_FORCE = 0x00000004;
		internal const int EWX_POWEROFF = 0x00000008;
		internal const int EWX_FORCEIFHUNG = 0x00000010;

		private static bool 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);
			return ok;
		}

		public static bool test()
		{
			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);
			return ok;
		}
	
		/// <summary>
		/// 注销
		/// </summary>
		public static void LogOff()
		{
			ApiCalls.DoExitWin(ApiCalls.EWX_LOGOFF);
		}

		//重启
		public static  void ReBoot()
		{
			ApiCalls.DoExitWin(ApiCalls.EWX_REBOOT); 
		}

		//关机
		public static void ShutDown()
		{
			ApiCalls.DoExitWin(ApiCalls.EWX_SHUTDOWN); 
		}
		#endregion 

		#region C#抓屏 
		//		public void SaveScreen()
		//		{
		//			Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
		//			Graphics gr1 = Graphics.FromImage(myImage);
		//			IntPtr dc1 = gr1.GetHdc();
		//			IntPtr dc2 = ApiCalls.GetWindowDC(ApiCalls.GetDesktopWindow());
		//			ApiCalls.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
		//			gr1.ReleaseHdc(dc1);
		//			myImage.Save("c:\\screenshot.jpg", ImageFormat.Jpeg);
		//		} 

		[DllImport("user32.dll")] 
		public extern static IntPtr GetDesktopWindow(); 
 

 
		[System.Runtime.InteropServices.DllImport("user32.dll")] 
		public static extern IntPtr GetWindowDC(IntPtr hwnd); 
 

 
		[System.Runtime.InteropServices.DllImport("gdi32.dll")] 
		public static extern UInt64 BitBlt 
			(IntPtr hDestDC, 
			int x,  
			int y,  
			int nWidth, 
			int nHeight, 
			IntPtr hSrcDC, 
			int xSrc, 
			int ySrc, 
			System.Int32 dwRop); 
		#endregion	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -