说实话本人笨的要死 高手指点下 尽量说的详细点
MSDN里面的我都看了 可还是不懂
看别人的代码这个参数的值都是8位数
高4位是Y 低4位是X 这我都知道 可问题是如何得到的 如何把自己想在窗口单击的坐标 转换成那8位数
MSDN里面的我都看了 可还是不懂
看别人的代码这个参数的值都是8位数
高4位是Y 低4位是X 这我都知道 可问题是如何得到的 如何把自己想在窗口单击的坐标 转换成那8位数
调试欢乐多
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.MouseClick+=new MouseEventHandler(Form1_MouseClick);
} void Form1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("X :" + e.X.ToString() + "Y :" + e.Y.ToString()); } private void button1_Click(object sender, EventArgs e)
{
int x=58;
int y=32; //写法1
PostMessage(this.Handle, WM_LBUTTONDOWN, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);
PostMessage(this.Handle, WM_LBUTTONUP, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000); //简化下 其实就是Y坐标左移16位,然后再加上X坐标
PostMessage(this.Handle, WM_LBUTTONDOWN, 0, x + (y<<16));
PostMessage(this.Handle, WM_LBUTTONUP, 0, x+(y<<16)); }
uint WM_LBUTTONDOWN = 0x201;
uint WM_LBUTTONUP = 0x202;
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
}
}
我的测试通过。不过注意我的是WindowsApplication2,一般的是WindowsApplication1
{
IntPtr hl = new IntPtr();
hl = FindWindow(0, "溪流锁鼠工具 1.2");
int sd=WindowModule.PostMessage(hl, 0x201, 0, 370+(480<<16));
WindowModule.PostMessage(hl, 0x202, 1, 370 + (480 << 16));
}
窗口句柄跟踪证实得到了
坐标应该也正确但是我在QQ连连看里好使 这是为什么
用this.handle做句柄
代码就是上面那个 我写出的代码没错吧
你说可能 是那个程序屏蔽了消息 我就把目标窗口句柄参数改成this.handle 改成自己的窗口又试了
我把自己的窗口里按钮尺寸改成半个窗口 按钮的Click事件是messageBox.Show("成功")
一点效果都没有 就是点不到 而PostMessage返回值还是非0 那代表成功吧
http://www.cnblogs.com/rawman/archive/2009/02/21/1395508.html
PostMessage(this.Handle, WM_LBUTTONDOWN, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);
PostMessage(this.Handle, WM_LBUTTONUP, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000); //简化下 其实就是Y坐标左移16位,然后再加上X坐标
PostMessage(this.Handle, WM_LBUTTONDOWN, 0, x + (y<<16));
PostMessage(this.Handle, WM_LBUTTONUP, 0, x+(y<<16));
可行
谢谢
Imports System.Runtime.InteropServicesPublic Class Form1
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function <DllImport("user32.dll", EntryPoint:="FindWindowEx", SetLastError:=True)> _
Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As UInteger, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function <DllImport("user32.dll", EntryPoint:="SendMessage", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function <DllImport("user32.dll", EntryPoint:="SetForegroundWindow", SetLastError:=True)> _
Private Shared Sub SetForegroundWindow(ByVal hwnd As IntPtr)
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const BM_CLICK As UInteger = &HF5
'鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
Dim hwndCalc As IntPtr = FindWindow(Nothing, "计算器")
'查找计算器的句柄
If hwndCalc <> IntPtr.Zero Then
Dim hwndThree As IntPtr = FindWindowEx(hwndCalc, 0, Nothing, "5")
'获取按钮5 的句柄
Dim hwndPlus As IntPtr = FindWindowEx(hwndCalc, 0, Nothing, "*")
'获取按钮 * 的句柄
Dim hwndTwo As IntPtr = FindWindowEx(hwndCalc, 0, Nothing, "6")
'获取按钮6 的句柄
Dim hwndEqual As IntPtr = FindWindowEx(hwndCalc, 0, Nothing, "=")
'获取按钮= 的句柄
SetForegroundWindow(hwndCalc)
'将计算器设为当前活动窗口
'每次暂停1秒才能睇到结果..
System.Threading.Thread.Sleep(1000) SendMessage(hwndThree, BM_CLICK, 0, 0)
System.Threading.Thread.Sleep(1000) SendMessage(hwndPlus, BM_CLICK, 0, 0)
System.Threading.Thread.Sleep(1000) SendMessage(hwndTwo, BM_CLICK, 0, 0)
System.Threading.Thread.Sleep(1000) SendMessage(hwndEqual, BM_CLICK, 0, 0) System.Threading.Thread.Sleep(1000)
MessageBox.Show("不要告诉你没睇到结果!")
Else
MessageBox.Show("没有打开 [计算器]")
End If
End Sub
End Class