鼠标操作window窗体合集...更新中


1.根据句柄查找窗体

引自http://www.2cto.com/kf/201410/343342.html

使用SPY++工具获取窗体

  首先打开spy++工具,同时点击"查找窗口"按钮(望远镜),再点击"查找程序工具"中按钮拖拽至要查看的窗体中,点击"确定"按钮.

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 }

C# 系统应用之鼠标模拟技术及自动操作鼠标

c# 获取当前活动窗口句柄,获取窗口大小及位置

用Mouse_event()来控制鼠标操作

C# 调用windows api 操作鼠标、键盘、窗体合集...更新中的更多相关文章

  1. c# 判断窗体是否永在最前(TopMost),调用windows API

    许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...

  2. c#让窗体永在最前 调用windows api 将窗体设为topmost

    有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...

  3. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  4. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  5. 善于 调用Windows API

    前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...

  6. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

  7. 用C#调用Windows API向指定窗口发送按键消息

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  8. C#调用Windows API函数截图

    界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...

  9. C#中调用Windows API的要点 .

    介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...

随机推荐

  1. KVM -> 虚拟机磁盘管理_03

    1.KVM磁盘管理 1.KVM qcow2.raw.vmdk等镜像格式说明:http://blog.csdn.net/zhengmx100/article/details/53887162 raw: ...

  2. MonkeyRunner之MonkeyRecorder录制回放脚本(亲测可正常运行)

    MonkeyRunner可以录制和回放脚本 前置条件: 电脑连接手机,输入adb devices 看看返回是否手机设备列表(我是真机,模拟器也可以) 配置好安卓sdk和Python环境 step: 1 ...

  3. [转]Apache Commons IO入门教程

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  4. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  5. python抓取bing主页背景图片

    最初Python2写法: #!/usr/bin/env python # -*- coding:utf-8 -*- # -*- author:nancy -*- # python2抓取bing主页所有 ...

  6. RDLC 主从报表筛选

    今天继续学习RDLC报表的“参数传递”及“主从报表” 一.先创建DataSet,如下图: 二.创建一个报表rptDEPT.rdlc,显示部门T_DPET的数据 三.嵌入Default.aspx中,写在 ...

  7. .NetCore 下开发独立的(RPL)含有界面的组件包 (一)准备工作

    .NetCore 下开发独立的(RPL)含有界面的组件包 (一)准备工作 .NetCore 下开发独立的(RPL)含有界面的组件包 (二)扩展中间件及服 务 .NetCore 下开发独立的(RPL)含 ...

  8. #5【BZOJ4275】[ONTAK2015]Badania

    Description 给定三个数字串A,B,C,请找到一个A,B的最长公共子序列,满足C是该子序列的子串. Input 第一行包含一个正整数n(1<=n<=3000),表示A串的长度. ...

  9. 【ioi2011】Dancing elephants

    题解: 这题是lct并不难想 关键在于如何建图 如果把每个大象连向第一个不能处理的大象 那么cut操作要删除的就是一个点而不是边 所以可以采用先离散化, 之后对于存在的大象,用边连向第一个不能处理的大 ...

  10. 【Java】 剑指offer(58-1) 翻转单词顺序

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变 ...