如何手动触发一个事件

realstolz 2003-07-22 04:13:25
在C#程序中我想自己触发鼠标的点击事件

比如
我将鼠标移动到屏幕的某个位置
然后触发鼠标左击或右击动作
(假设该位置有一个按钮,则该按钮被按下触发相应的事件)

我应该怎么做呢?
3x
...全文
846 15 打赏 收藏 举报
写回复
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
realstolz 2003-08-04
  • 打赏
  • 举报
回复
真不好意思,当天没结果
后来我用了和上面chainet一样的方法
忘了来看,竟然有一样的:)
现在才处理,不好意思
chainet 2003-07-23
  • 打赏
  • 举报
回复
移动鼠标可以用Cursor.Position来实现.而模拟鼠标点击事件用mouse_event函数就行了!
下面的示例是我花了10分钟做的.
要点:
1.声明mouse_event函数(API函数).
2.声明mouse点击消息

下面是完整代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace AutoMouseClick
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4;


private System.Windows.Forms.Button button1;

private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.CheckBox checkBox1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 0;
this.button1.Text = "随机移动鼠标位置";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Interval = 3000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(272, 48);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(152, 24);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "是否移动后点击";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 101);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
timer1.Start();

}
[DllImport("user32.dll", EntryPoint="mouse_event")]
public static extern void mouse_event (
int dwFlags,
int dx,
int dy,
int cButtons,
int dwExtraInfo
);

private void timer1_Tick(object sender, System.EventArgs e)
{
Random rand = new Random();
Cursor.Position = new Point(rand.Next(1024),rand.Next(768));
if(checkBox1.Checked)
{
mouse_event(MOUSEEVENTF_LEFTDOWN,Cursor.Position.X,Cursor.Position.Y,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,Cursor.Position.X,Cursor.Position.Y,0,0);
}
}

}
}
qiujinwen 2003-07-22
  • 打赏
  • 举报
回复
搞半天你是要发消息到别的程序啊。。那你就要抓到另外那个程序的按钮的消息了,用sendmessage没错,HWND hWnd是窗体或控件的句柄,后面两个为消息参数。。
csxtu 2003-07-22
  • 打赏
  • 举报
回复
protected override void OnMouseDown(
MouseEventArgs mevent
);
csxtu 2003-07-22
  • 打赏
  • 举报
回复
这其实不难啊,你重载你的鼠标事件不就的了啊
没见过人家搞测试的经常,自己遍程序,触发事件的发生吗
realstolz 2003-07-22
  • 打赏
  • 举报
回复
我现在找到一个win32 API
我不太会用谁能看看

SendMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
可不可以实现插一个鼠标点击消息
4个参数是什么,返回是什么?

//谢谢
cnhgj 2003-07-22
  • 打赏
  • 举报
回复
UP
realstolz 2003-07-22
  • 打赏
  • 举报
回复
可能我得意思说得不清

我不可能得到button1的实例 那是别人程序上得控件

就好现我要鼠标自己移到word窗口点击里面的一个按钮
我可以计算出关闭按钮的位置,但我不能对他干什么他是别人的程序
我只能模拟发出鼠标按下的动作让windows把事件发个word窗口去处里

我想知道这个模拟动作该正么发
//谢谢回帖的好心人,谁能帮帮我
CMIC 2003-07-22
  • 打赏
  • 举报
回复
private void Form2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
this.button1.PerformClick();
}
}
realstolz 2003-07-22
  • 打赏
  • 举报
回复
鼠标的事件我是清楚地

但现在我想要出发地不思我得事件
也就是说不是我点鼠标
鼠标是自己动,到指定的位置,做按下的动作(其实我没有动鼠标)

我想问的就是我将鼠标移到那里后这么做这个动作

举个例子
就是假设屏幕上有一个气球,我要靠计算知道他的位置
气球是别人写的程序,玩的时候是玩家用鼠标点机
现在我得程序自动计算气球位置,然后移动,发出鼠标按下的动作

这个过程中玩家什么也不着,所以不会触发我程序里写的任何事件
我也不可能调气球程序的相应事件,她使别人写的

大概是这个意思
在java程序里我可以用上面的方法做,其实他不知道出发了什么他只是模拟鼠标按下动作
是windows内部发出事件触发对应程序的handle

heroabi 2003-07-22
  • 打赏
  • 举报
回复
c#里有鼠标的move up down ,用以判断鼠标的移动,按下和抬起触发的事件,你是想问下思想还是要一些示例?
Chiun 2003-07-22
  • 打赏
  • 举报
回复
取得该按钮的handle,发送buttondown/buttonup消息给他便是了。
liduke 2003-07-22
  • 打赏
  • 举报
回复
1、判断该区域是否有控件存在
2、有,触发它的事件;没有,执行鼠标事件
realstolz 2003-07-22
  • 打赏
  • 举报
回复
但如果那个程序不是我写的那
我要的是一个鼠标按下的动作
如果当前位置又能相应点击事件的东西它自己触发
有点像掩饰程序的工作

//我知道java里面是这样做的
//
//import java.awt.Robot;
//
//Robot robot = new Robot();
//robot.mouseMove(startx+16*(j-1)+8,starty+16*(i-1)+8);
//robot.mousePress(InputEvent.BUTTON1_MASK);
qiujinwen 2003-07-22
  • 打赏
  • 举报
回复
检测鼠标区域是否有按钮。如果有按钮就执行按钮里面的事件。如果没按钮就执行鼠标里面的事件。不需要取消鼠标的事件触发的
发帖
C#

10.9w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-22 04:13
社区公告

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