C#和C常用的API操作窗口的代码积累

IntPtr awin = MouseHookHelper.FindWindow("WeChatMainWndForPC", "微信");
if (awin == IntPtr.Zero)
{MessageBox.Show("没有找到窗体");return;
}
2.获取窗体坐标信息
MouseHookHelper.RECT rect = new MouseHookHelper.RECT();
MouseHookHelper.GetWindowRect(awin, ref rect);
int width = rect.Right - rect.Left;             //窗口的宽度
int height = rect.Bottom - rect.Top;            //窗口的高度
int x = rect.Left;
int y = rect.Top;
3.设置为当前窗体
MouseHookHelper.SetForegroundWindow(awin);
MouseHookHelper.ShowWindow(awin,MouseHookHelper.SW_SHOWNOACTIVATE);//4、5
4.点击某个坐标
LeftMouseClick(new MouseHookHelper.POINT()
{X = ppp.MsgX,Y = ppp.MsgY
});private static void LeftMouseClick(MouseHookHelper.POINT pointInfo)
{//先移动鼠标到指定位置MouseHookHelper.SetCursorPos(pointInfo.X, pointInfo.Y);//按下鼠标左键MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTDOWN,pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);//松开鼠标左键MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTUP,pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);}
5.复制粘贴操作
//复制到剪贴板
Clipboard.SetText("test");
//从剪贴板获取数据
Clipboard.GetText();
//粘贴
SendKeys.SendWait("^V");
//回车键
SendKeys.Send("{Enter}");
6.钩子的使用
private void button1_Click(object sender, EventArgs e)
{if (hHook == 0){MyProcedure = new MouseHookHelper.HookProc(this.MouseHookProc);//这里挂节钩子hHook = MouseHookHelper.SetWindowsHookEx(WH_MOUSE_LL, MyProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);if (hHook == 0){MessageBox.Show("请以管理员方式打开");return;}button1.Text = "卸载钩子";}else{bool ret = MouseHookHelper.UnhookWindowsHookEx(hHook);if (ret == false){MessageBox.Show("请以管理员方式打开");return;}hHook = 0;button1.Text = "安装钩子";}
}private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{MouseHookHelper.MouseHookStruct MyMouseHookStruct = (MouseHookHelper.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookHelper.MouseHookStruct));if (nCode < 0){return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);}else{String strCaption = "x = " + MyMouseHookStruct.pt.X.ToString("d") + "  y = " + MyMouseHookStruct.pt.Y.ToString("d");this.Text = strCaption;return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);}
}
7.MouseHookHelper代码public class MouseHookHelper{#region 根据句柄寻找窗体并发送消息[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//参数1:指的是类名。参数2,指的是窗口的标题名。两者至少要知道1个public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);#endregion#region 获取窗体位置[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left;                             //最左坐标public int Top;                             //最上坐标public int Right;                           //最右坐标public int Bottom;                        //最下坐标}#endregion#region 设置窗体显示形式public enum nCmdShow : uint{SW_NONE,//初始值SW_FORCEMINIMIZE,//:在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。SW_MIOE,//:隐藏窗口并激活其他窗口。SW_MAXIMIZE,//:最大化指定的窗口。SW_MINIMIZE,//:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。SW_RESTORE,//:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。SW_SHOW,//:在窗口原来的位置以原来的尺寸激活和显示窗口。SW_SHOWDEFAULT,//:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。SW_SHOWMAXIMIZED,//:激活窗口并将其最大化。SW_SHOWMINIMIZED,//:激活窗口并将其最小化。SW_SHOWMINNOACTIVATE,//:窗口最小化,激活窗口仍然维持激活状态。SW_SHOWNA,//:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOACTIVATE,//:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOMAL,//:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。}public const int SW_HIDE = 0;public const int SW_SHOWNORMAL = 1;public const int SW_SHOWMINIMIZED = 2;public const int SW_SHOWMAXIMIZED = 3;public const int SW_MAXIMIZE = 3;public const int SW_SHOWNOACTIVATE = 4;public const int SW_SHOW = 5;public const int SW_MINIMIZE = 6;public const int SW_SHOWMINNOACTIVE = 7;public const int SW_SHOWNA = 8;public const int SW_RESTORE = 9;[DllImport("User32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport("User32.dll")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);#endregion#region 控制鼠标移动//移动鼠标 public const int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标左键抬起 public const int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标右键按下 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;//模拟鼠标右键抬起 public const int MOUSEEVENTF_RIGHTUP = 0x0010;//模拟鼠标中键按下 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//模拟鼠标中键抬起 public const int MOUSEEVENTF_MIDDLEUP = 0x0040;//标示是否采用绝对坐标 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;[Flags]public enum MouseEventFlag : uint{Move = 0x0001,LeftDown = 0x0002,LeftUp = 0x0004,RightDown = 0x0008,RightUp = 0x0010,MiddleDown = 0x0020,MiddleUp = 0x0040,XDown = 0x0080,XUp = 0x0100,Wheel = 0x0800,VirtualDesk = 0x4000,Absolute = 0x8000}//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)][DllImport("user32.dll")]public static extern bool SetCursorPos(int X, int Y);[DllImport("user32.dll")]public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);#endregion#region 获取坐标钩子[StructLayout(LayoutKind.Sequential)]public class POINT{public int X;public int Y;}[StructLayout(LayoutKind.Sequential)]public class MouseHookStruct{public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;}public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//安装钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//卸载钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(int idHook);//调用下一个钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);#endregion}

第二段代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//引用新命名空间
using System.Runtime.InteropServices;  //StructLayoutnamespace MouseAction
{public partial class Form1 : Form{public Form1(){InitializeComponent();}//结构体布局 本机位置[StructLayout(LayoutKind.Sequential)]struct NativeRECT{public int left;public int top;public int right;public int bottom;}//将枚举作为位域处理[Flags]enum MouseEventFlag : uint //设置鼠标动作的键值{Move = 0x0001,               //发生移动LeftDown = 0x0002,           //鼠标按下左键LeftUp = 0x0004,             //鼠标松开左键RightDown = 0x0008,          //鼠标按下右键RightUp = 0x0010,            //鼠标松开右键MiddleDown = 0x0020,         //鼠标按下中键MiddleUp = 0x0040,           //鼠标松开中键XDown = 0x0080,XUp = 0x0100,Wheel = 0x0800,              //鼠标轮被移动VirtualDesk = 0x4000,        //虚拟桌面Absolute = 0x8000}//设置鼠标位置[DllImport("user32.dll")]static extern bool SetCursorPos(int X, int Y);//设置鼠标按键和动作[DllImport("user32.dll")]static extern void mouse_event(MouseEventFlag flags, int dx, int dy,uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型[DllImport("user32.dll")]static extern IntPtr FindWindow(string strClass, string strWindow);//该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnParent=Null从桌面窗口查找[DllImport("user32.dll")]static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,string strClass, string strWindow);[DllImport("user32.dll")]static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);//定义变量const int AnimationCount = 80;private Point endPosition;private int count;private void button1_Click(object sender, EventArgs e){NativeRECT rect;//获取主窗体句柄// IntPtr ptrTaskbar = FindWindow("WindowsForms10.Window.8.app.0.2bf8098_r11_ad1", null);IntPtr ptrTaskbar = FindWindow("WindowsForms10.Window.8.app.0.141b42a_r10_ad1", null);//WindowsForms10.Window.8.app.0.141b42a_r10_ad1if (ptrTaskbar == IntPtr.Zero){MessageBox.Show("No windows found!");return;}//获取窗体中"button1"按钮//IntPtr ptrStartBtn = FindWindowEx(ptrTaskbar, IntPtr.Zero, null, "button1");//IntPtr ptrStartBtn = FindWindowEx(ptrTaskbar, IntPtr.Zero, null, "button1");//IntPtr ptrStartBtn = FindWindowEx(ptrTaskbar, IntPtr.Zero, null, "button1");IntPtr ptrStartBtn = FindWindowEx(ptrTaskbar, IntPtr.Zero, null, "开始");if (ptrStartBtn == IntPtr.Zero){MessageBox.Show("No button found!");return;}//获取窗体大小GetWindowRect(new HandleRef(this, ptrStartBtn), out rect);endPosition.X = (rect.left + rect.right) / 2;endPosition.Y = (rect.top + rect.bottom) / 2;//判断点击按钮if (checkBox1.Checked){MessageBox.Show("dasdas");//选择"查看鼠标运行的轨迹"this.count = AnimationCount;movementTimer.Start();}else{SetCursorPos(endPosition.X, endPosition.Y);mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);textBox1.Text = String.Format("{0},{1}", MousePosition.X, MousePosition.Y);}}//Tick:定时器,每当经过多少时间发生函数private void movementTimer_Tick(object sender, EventArgs e){int stepx = (endPosition.X - MousePosition.X) / count;int stepy = (endPosition.Y - MousePosition.Y) / count;count--;if (count == 0){movementTimer.Stop();mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);}textBox1.Text = String.Format("{0},{1}", MousePosition.X, MousePosition.Y);mouse_event(MouseEventFlag.Move, stepx, stepy, 0, UIntPtr.Zero);}}
}

第三段代码块

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> //ShellExecuteA()//打开某个网址:website (使用默认浏览器)
void open_web(char *website)
{ShellExecuteA(0,"open", website,0,0,1);
}//模拟鼠标点击  (x,y)是要点击的位置
void click(int x, int y)
{//将鼠标光标移动到 指定的位置     例子中屏幕分辨率1600x900  在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, x*65535/1600, y*65535/900, 0, 0);Sleep(50);//稍微延时50ms mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//鼠标左键按下 mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//鼠标左键抬起}//模拟键盘输入 keybd_event(要按下的字符,0,动作,0);动作为0是按下,动作为2是抬起
void input()
{char user[]="1234567890123";//账号 char pwd[]="1234567890";//密码 click(823,392); //点击"用户名输入框"的位置  int i;//输入账号 for(i=0;i<sizeof(user);i++){keybd_event(user[i],0,0,0);keybd_event(user[i],0,2,0);Sleep(30); }//tab键 对应的编号是0x09  让密码输入框 获取焦点 keybd_event(0x09,0,0,0);//按下 keybd_event(0x09,0,2,0); //松开 Sleep(30);   //输入密码 for(i=0;i<sizeof(pwd);i++){keybd_event(pwd[i],0,0,0);keybd_event(pwd[i],0,2,0);Sleep(30);}//模拟按下tab键 让登录按钮获取焦点 click(824,530);//点击"登录按钮" Sleep(30);
}//将chrome.exe进程杀掉,在例子中尚未使用
void close()
{system("taskkill  /f  /im chrome.exe");
}int main(int argc,char *argv[])
{open_web("https://www.baidu.com/");//打开某个网址 Sleep(4000);//延时4秒,等待网页打开完毕,再进行其它操作。根据实际情况(浏览器打开速度,网速) click(1454, 126);//点击"登录"(1454,126) Sleep(150);click(712,658);//点击"用户名登录"Sleep(150);input();//模拟鼠标动作,键盘输入 return 0;
}#include <stdio.h>
#include <windows.h> //ShellExecute() int main(int argc, char *argv[])
{ShellExecute(0, "open", "C:\\Users\\newuser\\Desktop\\串口助手.exe",0, 0, 1);//最后的参数是控制最大化、最小化printf("Hello World!\n");return 0;
}//例子中屏幕分辨率1600x900  在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, x*65535/1600, y*65535/900, 0, 0);mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//鼠标左键按下
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//鼠标左键抬起keybd_event('9',0,0,0);//按下按键 ‘9’
keybd_event('9',0,2,0);//抬起按键 ‘9’或  0x39keybd_event(0x39,0,0,0);//按下按键 ‘9’
keybd_event(0x39,0,2,0);//抬起按键 ‘9’

C#和C常用的API操作窗口的代码积累相关推荐

  1. 常用的一组API操作

    常用的一组API操作   Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ...

  2. HDFS常用命令和客户端API操作

    注意 1)记得快照,快照,快照一下 ECS服务器怎么快照?  ECS服务器搭建hadoop伪分布式_CBeann的博客-CSDN博客_宝塔部署hadoop 2)参考资料(视频)的问题 上面网盘的视频, ...

  3. 大数据技术之_20_Elasticsearch学习_01_概述 + 快速入门 + Java API 操作 + 创建、删除索引 + 新建、搜索、更新删除文档 + 条件查询 + 映射操作

    大数据技术之_20_Elasticsearch学习_01 一 概述 1.1 什么是搜索? 1.2 如果用数据库做搜索会怎么样? 1.3 什么是全文检索和 Lucene? 1.4 什么是 Elastic ...

  4. 易语言常用WINdows API分类查询

    WINdows API分类 1.API之网络函数 2.API之消息函数 3.API之文件处理函数 4.API之打印函数 5.API之文本和字体函数 6.API之菜单函数 7.API之位图.图标和光栅运 ...

  5. Selenium经典API操作

    Selenium经典API操作 三种等待方式 1.强制等待--sleep(等待时间) time库中的sleep()函数 不管怎么样,让等几秒就等几秒 真正测试的时候不需要死等,只要页面刷新出元素了就可 ...

  6. 大数据技术之_20_Elasticsearch学习_01_概述 + 快速入门 + Java API 操作 + 创建、删除索引 + 新建、搜索、更新删除文档 + 条件查询 + 映射操作...

    一 概述1.1 什么是搜索?1.2 如果用数据库做搜索会怎么样?1.3 什么是全文检索和 Lucene?1.4 什么是 Elasticsearch?1.5 Elasticsearch 的适用场景1.6 ...

  7. 五、Java中常用的API(通过包进行分类)————异常、多线程和Lambda表达式

    之前已经介绍了java.lang包下的相关类,今天将要补充两个常用的API:java.lang.Throwable和java.lang.Thread 一.异常(java.lang.Throwable) ...

  8. VC++常用数据类型及其操作详解(非常经典,共同分享)

    友情提示: 为了方便你更好的学习和阅读,也更好的体现尊重原创作者的劳动成果,请您直接查看转载原本链接: http://snailflying.blog.hexun.com/8219350_d.html ...

  9. Elastic search入门到集群实战操作详解(原生API操作、springboot整合操作)-step1

    Elastic search入门到集群实战操作详解(原生API操作.springboot整合操作)-step2 https://blog.csdn.net/qq_45441466/article/de ...

最新文章

  1. 三极管在ad中的原理图库_555时基电路内部结构及其工作原理
  2. 红盟过客提到的 CCIE 必读书籍
  3. linux看目录用的哪个磁盘,linux查看目录大小及硬盘大小
  4. 【风控模型】融合模型Bagging构建信用评分卡模型
  5. uml活动图 各个功能的操作流程和分支_UML学习系列教程08------九大基本图05---活动图(Activity Diagram)(重点理解和流程图的区别)...
  6. .Net组件程序设计之线程、并发管理(二)
  7. 使用 OAuth2-Server-php 搭建 OAuth2 Server
  8. Spring-Boot 2.1.x和主要的bean定义
  9. 济宁医学院计算机专业好就业吗,山东这3所医学院实力强,就业率高,中等生可捡漏...
  10. A-Z排序控件的实现
  11. java nio doug_深入的聊聊 Java NIO
  12. 影响数百万人的21个经典全英文演讲,看完英语水平暴增!赶紧收藏
  13. ioc spring 上机案例_Spring的IoC入门案例
  14. 我在用的浏览器插件利器
  15. 合并表格中同一列中相同的内容
  16. PhotoShop如何给字体添加下划线
  17. 龙芯2f平台下 Debain 6编译Lighttpd并支持C语言cgi脚本编程
  18. c语言空格eof什么意思,eof在c语言中表示什么?
  19. 华为OD机试 - 租车骑绿岛
  20. Spring-全面详解(基础知识)

热门文章

  1. mysql is null走索引_Mysql数据库索引IS NUll ,IS NOT NUll ,!= 是否走索引
  2. python一维列表的定义_数据结构-Python 列表(List)
  3. python集合的操作_Python集合操作方法详解
  4. 自行车实现无人驾驶,背后究竟有何“天机”?
  5. 什么?物联网方向也能发论文了?
  6. HDLBits答案(19)_Verilog有限状态机(6)
  7. 明日之后服务器维修会补偿什么,明日之后:服务器修复后官方发来补偿,玩家居然怀疑奖励不真实?...
  8. android 画布裁剪,一种基于Android系统对UI控件进行轮廓剪裁及美化的方法与流程...
  9. c语言枚举入门,C语言入门之枚举与位运算(1)
  10. python 导出大量数据到excel_怎么在python中将大量数据导出到Excel文件