怎么向别的程序发送鼠标点击消息? 直接sendmessage ,调用api,可以自定义一个消息.另外程序中,实现ImessageFilter ,截获这个消息. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Montaque(Rain + Man=Rainman) 说的到是种不错的方法,不过不知道在C#里能不能实现。在vc++里实现没问题。 给段代码应用程序1using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace AppA{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form,IMessageFilter { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; 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() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "APP1"; this.Load += new System.EventHandler(this.Form1_Load); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { Application.AddMessageFilter(this); } public const Int32 WM_USER = 0x400; public const Int32 WM_KKK =WM_USER+ 123 ; #region IMessageFilter 成员 public bool PreFilterMessage(ref Message m) { // TODO: 添加 Form1.PreFilterMessage 实现 if(m.Msg==WM_KKK) { MessageBox.Show("I received a messag from other app" ); return true; } return false; } #endregion }} App 2, 发送消息using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Runtime.InteropServices;namespace AppB{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; 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.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(192, 112); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(400, 326); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "App2"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } [DllImport("user32.dll")] public static extern Int32 PostMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam ); [DllImport("user32.dll")] public static extern IntPtr FindWindow(String lpClassName , String lpWindowName ) ; private System.Windows.Forms.Button button1; public const Int32 WM_USER = 0x400; public const Int32 WM_KKK =WM_USER+ 123 ; private void button1_Click(object sender, System.EventArgs e) { //find windows IntPtr app1Handle=FindWindow(null,"APP1"); System.Diagnostics.Debug.Assert(!app1Handle.Equals(IntPtr.Zero)); PostMessage(app1Handle,WM_KKK,0,0); } private void Form1_Load(object sender, System.EventArgs e) { } }} Montaque的做法是,两个程序都是自己写的。问题是我想用我的程序点击别人的程序(例如浏览器)。 那别人的程序要做成 DLL 并且还要提供一个接口。这样的话,可以实现。不过,不知道你是不是我所说的那个意思。还是实现鼠标真实的点击?(叫鼠标动起来) chartcontrol添加滚动条,及导出图片 TreeView和ListBox相互绑定问题 如何在gridview中的combobox绑定数据库 datagridview 排序影响坐标 一个串口通讯的问题[类代码已经有],有个小问题帮忙改进! C#中SslStream测试ssl协议握手 C#的一个winform程序,希望大家能给点意见 string如何转换成single 用C#开发winform组件 同步连接与异步连接有什么区别,我不是很明白 table[i,j]==1 什么意思? 2个小问题!!!
不过不知道在C#里能不能实现。在vc++里实现没问题。
应用程序1using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace AppA
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form,IMessageFilter
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "APP1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
Application.AddMessageFilter(this);
}
public const Int32 WM_USER = 0x400;
public const Int32 WM_KKK =WM_USER+ 123 ; #region IMessageFilter 成员 public bool PreFilterMessage(ref Message m)
{
// TODO: 添加 Form1.PreFilterMessage 实现
if(m.Msg==WM_KKK)
{
MessageBox.Show("I received a messag from other app" );
return true;
}
return false;
} #endregion
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;namespace AppB
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 326);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "App2";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[DllImport("user32.dll")]
public static extern Int32 PostMessage(IntPtr hWnd,
Int32 Msg,
Int32 wParam,
Int32 lParam
);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String lpClassName , String lpWindowName ) ; private System.Windows.Forms.Button button1;
public const Int32 WM_USER = 0x400;
public const Int32 WM_KKK =WM_USER+ 123 ;
private void button1_Click(object sender, System.EventArgs e)
{
//find windows
IntPtr app1Handle=FindWindow(null,"APP1");
System.Diagnostics.Debug.Assert(!app1Handle.Equals(IntPtr.Zero));
PostMessage(app1Handle,WM_KKK,0,0);
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}