现在遇到了个问题
描述如下: 先前做了个应用程序,点击按钮就执行相关的操作,现在的要求是在做个辅助程序,用来启动该应用程序,并执行主窗口上的某个按钮的事件.
我现在模拟一下,先做了个应用程序,代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <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>
public void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(64, 160);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(168, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "extern";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} public void button1_Click(object sender, System.EventArgs e)
{
this.button1.Text="外部调用!";
MessageBox.Show("外部调用成功!");
}
}
}
然后呢,现在我要求做个辅助的程序,来调用该应用程序的button1 的单击事件,现在的问题怎么调用
写了一半,不知道怎么写了?
如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <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(72, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 205);
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)

System.Diagnostics.ProcessStartInfo myProInf=new ProcessStartInfo(); myProInf.FileName="WindowsApplication2"; myProInf.WorkingDirectory=@"C:\Documents and Settings\Administrator\桌面\WindowsApplication2\bin\Release"; Process hao; hao=System.Diagnostics.Process.Start(myProInf); System.IntPtr  jiang=hao.Handle;              MessageBox.Show( hao.Container.Components.ToString());

}
}
}

解决方案 »

  1.   

    WindowsApplication3里边单击按钮调用WindowsApplication2的button1的事件,怎么实现?
      

  2.   

    补充说明一下,WindowsApplication2只是模拟的一个应用程序,且不能修改源程序的,希望高手帮助一下!
      

  3.   

    小弟问了个同事,说实现方式是这样的:
    1 先得到要调用的窗口
    2 然后再该窗口里边找到子窗口(button是窗口)
    3 调用该按钮的事件 
    那怎么写呢?
      

  4.   

    把Button做的事情写在一个过程中,如OnClickButton,这样可以供外部调用