• 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
  • 相关阅读:
    docker安装部署skywalking
    (leetcode)单值二叉树
    智云通CRM:客户说“别人家的便宜”,如何有效回应?
    第八章《Java高级语法》第3节:位运算符
    sqlite3数据库
    【C++天梯计划】1.1 C++初识
    QT环境配置
    架构师的 36 项修炼第10讲:架构实战案例分析
    Serverless Devs 社区联合信通院邀请您参加 2022 中国 Serverless 用户调查
    普冉PY32F071单片机简单介绍,QFN64 48封装,支持 8 * 36 / 4 * 40 LCD
  • 原文地址:https://blog.csdn.net/weixin_44171249/article/details/128038204