C#句柄的操作(鼠标移动、键盘点击、复制粘贴)

这篇具有很好参考价值的文章主要介绍了C#句柄的操作(鼠标移动、键盘点击、复制粘贴)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

--模拟键盘点击--
[DllImport("user32.dll",EntryPoint = "keybd_event",SetLastError = true)]
public static extern void keybd_event(Keys bVk,byte bScan,uint dwFlags,uint dwExtraInfo);
public static void CtrlAAndCtrlC()
{
	Keybd_event(Keys.ControlKey,0,0,0);
	keybd_event(Keys.A,0,0,0);
	keybd_event(Keys.C,0,0,0);
	keybd_event(Keys.ControlKey,0,KEYEVENTF_KEYUP);
}

--鼠标移动到固定位置--
[DllImport("user32.dll")]
public static extern int SetCursorPos(int x, int y);

--鼠标点击--
[DllImport("user32.dll",EntryPoint = "PostMessage",CallingConvention = CallingConvention.Winapi)]
public static extern int PostMessage(IntPtr hwnd, int msg, uint wParam, uint IParm);

--最大最小关闭窗口--
[DllImport("user32.dll",CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

--将窗口显示到最前端--
[DllImport("user32.dll",EntryPoint = "EnumChildWindows")]
public static extern int EnumChildWindows(IntPtr hwndParent, CallBack callBack, int IParm);
public delegate bool CallBack(IntPtr hwnd, int IParam);

--获取窗体文本--
[DllImport("user32.dll",EntryPoint = "GetWindowText")]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder text, int maxLength);

--获取窗体Class文本--
[DllImport("user32.dll",EntryPoint = "GetClassText")]
public static extern int GetClassText(IntPtr hwnd, StringBuilder text, int maxLength);

----
[DllImport("user32.dll",EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex);

--设置文本--
[DllImport("user32.dll",EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder IParam);

--设置文本--
[DllImport("user32.dll",EntryPoint = "SetWindowText")]
public static extern int SetWindowText(IntPtr hwnd, string text);

--设置键盘点击--
[DllImport("user32.dll",EntryPoint = "PostMessageA", SetLastError = true)]
public static extern int PostMessageA(IntPtr hwnd, int msg, Keys wParm, uint IParm);

--导出进程--
[DllImport("user32.dll",EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

--让窗口置于所有页面最前端--
[DllImport("user32.dll",CharSet.Auto)]
public static extern bool SwitchToThisWindow(IntPtr hwnd, bool fAltTab);

--鼠标运动到位置并点击--
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

--模拟鼠标点击抬起--
public static void MouserDownAndUp(IntPtr hwnd, ushort x, ushort y)
{
	PostMessage(hwnd, WM_Lbutton, 0 (((uint)x) << 16 | y)));
	PostMessage(hwnd, WM_LBUTTONUP, 0 (((uint)x) << 16 | y)));
}

public const int WM_Lbutton = 0x0201;//鼠标左键按下
public const int WM_LBUTTONUP = 0x0202;//鼠标左键抬起
public const int WM_SETTEXT = 0x000C;//设置文本

--获取所有进程列表--
public static List<WindowInfo> GetChildWindows(IntPtr hwnd)
{
	var list = new List<WindowInfo>();
	StringBuilder sb = new StringBuilder(4048);
	car idx = 0;
	EnumChildWindows(hwnd, (IntPtr childHwnd, int IParam) => 
	{
		WindowInfo wi = new WindowInfo()
		{
			Hwnd = childHwnd,
			Idx = idx++,	
		};
		GetWindowText(childHwnd, sb, sb.Capacity);
		wi.WindowName = sb.ToString();
		GetClassName(childHwnd, sb, sb.Capacity);
		wi.ClassName = sb.ToString();
		wi.GWL_EXSTYLE = GetWindowLong(childHwnd,GWL_EXSTYLE).ToString();
		wi.GWL_HINSTANCE = GetWindowLong(childHwnd,GWL_HINSTANCE).ToString();
		wi.GWL_ID = GetWindowLong(childHwnd,GWL_ID).ToString();
		wi.GWL_STYLE = GetWindowLong(childHwnd,GWL_STYLE).ToString();
		list.Add(wi);
		return true;
		},0);
		return list;
}

public class WindowInfo
{
	public WindowInfo()
	{
		
	}
	public int Idx { get;set; };
	public IntPtr Hwnd { get;set; };
	public string WindowName { get;set; };
	public string ClassName { get;set; };
	public string GWL_EXSTYLE { get;set; };
	public string GWL_HINSTANCE { get;set; };
	public string GWL_ID { get;set; };
	public string GWL_STYLE h{ get;set; };
}


--复制剪贴板文本--
--必须开启线程,否则当程序多线程时候会复制不到文本--
[STAThread]
public static string GetCopyData()
{
	var copy = "";
	Thread thread = new Thread(
	delegate()
	{
		try
		{
			copy = Clipboard.GetText();
		}
		catch(Exception ex)
		{
			copy = ex.Tostring();
		}
	});
	thread.SetApartmentState(ApartmentState.STA);
	thread.Start();
	thread.Join();
	return copy;
}

文章来源地址https://www.toymoban.com/news/detail-502649.html

到了这里,关于C#句柄的操作(鼠标移动、键盘点击、复制粘贴)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包