您的位置:程序门 -> .net技术 -> c#



求向后台程序发送键盘消息的代码(分不够再加)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求向后台程序发送键盘消息的代码(分不够再加)[已结贴,结贴人:geodetic]
发表于:2007-09-21 15:57:13 楼主
模拟键盘操作程序,程序处于后台,最好能够不需要通过setforegroundwindow、setactivewindow把程序切换到前台,就能向要控制的程序发送键盘消息。
(帮顶有分,分不够在加)
发表于:2007-09-21 15:59:531楼 得分:6
应该是可以的,可以使用sendmessage向指定窗口发送wm_keydown等任意消息,这个窗口不一定是当前的。
发表于:2007-09-21 16:47:592楼 得分:0
能给个例子吗?
比如说依次向后台运行的notepad发送“f1”、回车、“123”。
谢谢。
发表于:2007-09-21 16:52:283楼 得分:1
up
发表于:2007-09-21 17:28:054楼 得分:60
给你个例子
[dllimport( "user32.dll ")]
public   static   extern   int   sendmessage(intptr   hwnd,
uint   msg,   int   wparam,   string   lparam);
[dllimport( "user32.dll ")]
public   static   extern   intptr   findwindowex(intptr   hwndparent,
intptr   hwndchildafter,   string   lpszclass,   string   lpszwindow);
public   const   uint   wm_settext   =   0x000c;
private   void   button1_click(object   sender,   eventargs   e)
{
process   vprocess   =   process.start( "notepad.exe ");
while   (vprocess.mainwindowhandle   ==   intptr.zero)   vprocess.refresh();
intptr   vhandle   =   findwindowex(vprocess.mainwindowhandle,
intptr.zero,   "edit ",   null);
sendmessage(vhandle,   wm_settext,   0,   "zswang   路过 ");
}
发表于:2007-09-21 17:54:145楼 得分:1
学习中~~

友情up

偶还差7分,请搂住帮忙
发表于:2007-09-21 18:02:586楼 得分:1
關注
发表于:2007-09-21 18:10:597楼 得分:0
谢谢。
不过发现sendmessage会覆盖上一个sendmessage的内容,该怎么解决呢?
还有能否通过已经在运行的进程的程序路径找到进程的handle呢?(因为应用中的状况是不同路径中有两个同名程序,process.getprocessesbyname会得到两个process,而且他们的startinfo.filename,也没法通过比较这个属性知道要控制的是哪一个程序)
发表于:2007-09-21 18:19:058楼 得分:1
mark
发表于:2007-09-21 18:35:099楼 得分:5
private   void   button2_click(object   sender,   eventargs   e)
                {
                        this.richtextbox1.focus();   //焦点请求

                        for   (int   i   =   0;   i   <   10;   i++)
                        {

                                sendkeys.send(i.tostring());

                                system.threading.thread.sleep(100);

                                sendkeys.flush   ();

                        }

                }
发表于:2007-09-21 18:35:5010楼 得分:1
看错题目了``
发表于:2007-09-24 13:43:0511楼 得分:0

分不够我会另开新帖加
顺便请教一下sendmessage各参数可取值范围与含义,如:
        uint   msg=0x000c,
        wparam   wparam=0,
        lparam   lparam= "test "
向指定进程发送字符串。
如果想发送   tab、f1、alt、回车   等命令该怎么办?还有怎么防止sendmessage覆盖前面发送的消息?怎么依据程序路径找到已经运行的程序句柄(内存中可能有两个同名但路径不同的程序)?
谢谢。
发表于:2007-09-24 14:43:5812楼 得分:1
友情up一下
发表于:2007-09-24 15:48:2513楼 得分:1
学习         不过   api   不熟悉啊
发表于:2007-09-24 15:59:2114楼 得分:1
帮顶
发表于:2007-09-24 22:39:3115楼 得分:1
帮顶!
发表于:2007-09-25 09:23:1616楼 得分:20
消息参数的含义根据消息的不同而不同,每个消息的参数含义都要到msdn里去查,比如这个是wm_keyup   的

wparam
specifies   the   virtual-key   code   of   the   nonsystem   key.  

lparam
specifies   the   repeat   count,   scan   code,   extended-key   flag,   context   code,   previous   key-state   flag,   and   transition-state   flag,   as   shown   in   the   following   table.  
0-15
specifies   the   repeat   count   for   the   current   message.   the   value   is   the   number   of   times   the   keystroke   is   autorepeated   as   a   result   of   the   user   holding   down   the   key.   the   repeat   count   is   always   one   for   a   wm_keyup   message.
16-23
specifies   the   scan   code.   the   value   depends   on   the   oem.
24
specifies   whether   the   key   is   an   extended   key,   such   as   the   right-hand   alt   and   ctrl   keys   that   appear   on   an   enhanced   101-   or   102-key   keyboard.   the   value   is   1   if   it   is   an   extended   key;   otherwise,   it   is   0.
25-28
reserved;   do   not   use.
29
specifies   the   context   code.   the   value   is   always   0   for   a   wm_keyup   message.
30
specifies   the   previous   key   state.   the   value   is   always   1   for   a   wm_keyup   message.
31
specifies   the   transition   state.   the   value   is   always   1   for   a   wm_keyup   message.


楼主要做的事不太适合c#,用c++比较好
发表于:2007-09-25 13:22:5517楼 得分:0
谢谢楼上。
主要是考虑到这个东西的很多其它功能要大量应用.net   framework的东西,c++没有合适的库(没有用过c++/clr,不知道这种情况下c++/clr相对纯.net环境是否有什么特殊优势,比如对系统api的调用上)。
发表于:2007-09-27 15:59:4218楼 得分:0
问题没有完全解决,不好再开新帖,本帖可分配分数只有100,帮顶的兄弟抱歉了


快速检索

最新资讯
热门点击