如何给另外一个进程发送消息?
比如,启动一个计算器,然后,发送3,*,8,最后在计算器上显示24。
参考文章:
int wm_keydown = 0x100;
foreach ( process p in process.getprocesses(system.environment.machinename) )
{ intptr handle = p.mainwindowhandle;
if(p.startinfo.filename=="calc.exe")
{
sendmessage(handle,wm_keydown,0,keys.numpad1);
}
[dllimport("user32.dll", charset=charset.auto)]
public static extern intptr sendmessage(intptr hwnd, int umsg, int wparam, int lparam);
1.启动计算器,得到
2.获得活动窗口的线程号,获得与自己程序的窗口相关的线程号
3.将两个线程的输入联系起来
4,sendkey
5.线程分离.
这是以前作的一个例子,有一些对你的这个问题没有用,自己挑挑吧。在我的计算机上已经运行通过,看到那个24了。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
using system.text;
using system.diagnostics;
namespace activewindow
{
/// <summary>
/// summary description for form1.
/// </summary>
public class form1 : system.windows.forms.form
{
[dllimport("user32.dll")]
private static extern int getwindowtext(intptr hwnd, stringbuilder title, int size);
[ dllimport("user32.dll") ]
private static extern intptr getforegroundwindow();
[dllimport("user32.dll")]
private static extern intptr getwindowthreadprocessid(intptr hwnd, intptr processid);
[dllimport("user32.dll")]
private static extern intptr attachthreadinput(intptr idattach, intptr idattachto, int fattach);
[dllimport("user32.dll")]
private static extern intptr getfocus();
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.label label3;
private system.windows.forms.label captionwindowlabel;
private system.windows.forms.label idwindowlabel;
private system.windows.forms.button button1;
private system.windows.forms.timer timer1;
private system.windows.forms.button button2;
private system.componentmodel.icontainer components;
private void getactivewindow()
{
process p = process.start("calc.exe");
intptr activewindowhandle = p.mainwindowhandle;
//获得活动窗口的线程号
intptr handle1 = getwindowthreadprocessid(this.handle,intptr.zero);
intptr handle2 = getwindowthreadprocessid(activewindowhandle,intptr.zero);
if (activewindowhandle != this.handle)
{
//将两个线程的输入联系起来
attachthreadinput(handle1,handle2,1);
sendkeys.send("3");
sendkeys.send("*");
sendkeys.send("8");
sendkeys.send("=");
//线程分离
attachthreadinput(handle1,handle2,0);
}
}
public form1()
{
//
// required for windows form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
//
}
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.components = new system.componentmodel.container();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.captionwindowlabel = new system.windows.forms.label();
this.idwindowlabel = new system.windows.forms.label();
this.button1 = new system.windows.forms.button();
this.timer1 = new system.windows.forms.timer(this.components);
this.button2 = new system.windows.forms.button();
this.suspendlayout();
//
// label1
//
this.label1.location = new system.drawing.point(174, 9);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(143, 19);
this.label1.tabindex = 0;
this.label1.text = "active window detail";
//
// label2
//
this.label2.location = new system.drawing.point(20, 46);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(123, 19);
this.label2.tabindex = 1;
this.label2.text = "window caption : ";
//
// label3
//
this.label3.location = new system.drawing.point(20, 102);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(123, 18);
this.label3.tabindex = 2;
this.label3.text = "window handle :";
//
// captionwindowlabel
//
this.captionwindowlabel.location = new system.drawing.point(143, 46);
this.captionwindowlabel.name = "captionwindowlabel";
this.captionwindowlabel.size = new system.drawing.size(287, 47);
this.captionwindowlabel.tabindex = 3;
//
// idwindowlabel
//
this.idwindowlabel.location = new system.drawing.point(143, 102);
this.idwindowlabel.name = "idwindowlabel";
this.idwindowlabel.size = new system.drawing.size(128, 18);
this.idwindowlabel.tabindex = 4;
//
// button1
//
this.button1.location = new system.drawing.point(225, 148);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(62, 28);
this.button1.tabindex = 5;
this.button1.text = "exit";
this.button1.click += new system.eventhandler(this.button1_click);
//
// timer1
//
this.timer1.enabled = true;
this.timer1.tick += new system.eventhandler(this.timer1_tick);
//
// button2
//
this.button2.location = new system.drawing.point(304, 144);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(128, 32);
this.button2.tabindex = 6;
this.button2.text = "button2";
this.button2.click += new system.eventhandler(this.button2_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(512, 200);
this.controls.addrange(new system.windows.forms.control[] {
this.button2,
this.button1,
this.idwindowlabel,
this.captionwindowlabel,
this.label3,
this.label2,
this.label1});
this.name = "form1";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "window information";
this.most = true;
this.resumelayout(false);
}
#endregion
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void timer1_tick(object sender, system.eventargs e)
{
//getactivewindow();
}
private void button1_click(object sender, system.eventargs e)
{
this.close();
}
private void button2_click(object sender, system.eventargs e)
{
getactivewindow();
}
}
}
Posted in noname. Edit