110,590
社区成员
发帖
与我相关
我的任务
分享
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
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);
}
}