C# 关于API函数PostMessage()的鼠标单击坐标参数lParam

opq211211 2010-04-05 08:06:18
说实话本人笨的要死 高手指点下 尽量说的详细点
MSDN里面的我都看了 可还是不懂
看别人的代码这个参数的值都是8位数
高4位是Y 低4位是X 这我都知道

可问题是如何得到的 如何把自己想在窗口单击的坐标 转换成那8位数
...全文
2921 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyxrw 2011-03-06
  • 打赏
  • 举报
回复

Imports System.Runtime.InteropServices

Public 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
qjf309 2011-01-15
  • 打赏
  • 举报
回复
xingyuebuyu

//写法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));
可行
谢谢
opq211211 2010-04-11
  • 打赏
  • 举报
回复
这回 我明白了 非常感谢
xingyuebuyu 2010-04-09
  • 打赏
  • 举报
回复
你没理解PostMessage的作用,它是给IntPtr hWnd,句柄所代表的对象发送消息的,它的第一个参数this.handle 是指窗体本身,那你的按钮是另外一个控件,它的句柄和窗体是不一样的.所以你即便指便的位置那里有一个按钮也是不行的,相当于你是给按钮所处位置下面的窗体发送左键单击消息.你要想给按钮发送消息,就要使用按钮的句柄.

参考下面的对计算器进行操作的,将SendMessage改为PostMessage既可
http://www.cnblogs.com/rawman/archive/2009/02/21/1395508.html
opq211211 2010-04-07
  • 打赏
  • 举报
回复
用PostMessage 点击目标窗口里的一个按钮 就这么简单 目的只是测试这个函数
代码就是上面那个 我写出的代码没错吧
你说可能 是那个程序屏蔽了消息 我就把目标窗口句柄参数改成this.handle 改成自己的窗口又试了
我把自己的窗口里按钮尺寸改成半个窗口 按钮的Click事件是messageBox.Show("成功")
一点效果都没有 就是点不到 而PostMessage返回值还是非0 那代表成功吧

xingyuebuyu 2010-04-07
  • 打赏
  • 举报
回复
用我的代码进行测试?
你的目的是什么?把你的代码发出来看看
opq211211 2010-04-07
  • 打赏
  • 举报
回复
我自己的程序不可能屏蔽这个消息 为什么一样不好使
用this.handle做句柄
opq211211 2010-04-07
  • 打赏
  • 举报
回复
。。。。。。。
哦 这个我只在QQ连连看里测试成功
但我还是要谢谢你
xingyuebuyu 2010-04-07
  • 打赏
  • 举报
回复
有可能是它把这个消息进行屏蔽了。这不是代码的问题。
opq211211 2010-04-07
  • 打赏
  • 举报
回复
private void button11_Click(object sender, EventArgs e)
{
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连连看里好使 这是为什么
xingyuebuyu 2010-04-07
  • 打赏
  • 举报
回复
把你的测试代码贴出来看看?
我的测试通过。不过注意我的是WindowsApplication2,一般的是WindowsApplication1
opq211211 2010-04-07
  • 打赏
  • 举报
回复
为什么我测试点击按钮会没反应
叶子 2010-04-05
  • 打赏
  • 举报
回复
PostMessage的lParam 参数用MAKELPARAM来生成
xingyuebuyu 2010-04-05
  • 打赏
  • 举报
回复
using System;
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);
}
}

110,590

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧