向另一个应用程序发送键击

转自:http://blog.csdn.net/menglingjun/article/details/1789788

向另一个应用程序发送键击 

激活将接收键击的应用程序窗口,然后调用 Send 或 SendWait 方法。由于没有激活另一个应用程序的托管方法,因此必须使用本机 Windows 方法强制将焦点放在其他应用程序上。下面的代码示例使用平台调用来调用 FindWindow 和 SetForegroundWindow 方法,以激活计算器应用程序窗口,然后调用 SendWait 向计算器应用程序发出一系列计算。 

Visual Basic 复制代码 
' Get a handle to an application window. 
Declare Auto Function FindWindow Lib "USER32.DLL" ( _ 
ByVal lpClassName As String, _ 
ByVal lpWindowName As String) As IntPtr 

' Activate an application window. 
Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _ 
(ByVal hWnd As IntPtr) As Boolean 

' Send a series of key presses to the Calculator application. 
Private Sub button1_Click(ByVal sender As Object, _ 
ByVal e As EventArgs) Handles button1.Click 

' Get a handle to the Calculator application. The window class 
' and window name were obtained using the Spy++ tool. 
Dim calculatorHandle As IntPtr = FindWindow("SciCalc", "Calculator") 

' Verify that Calculator is a running process. 
If calculatorHandle = IntPtr.Zero Then 
MsgBox("Calculator is not running.") 
Return 
End If 

' Make Calculator the foreground application and send it 
' a set of calculations. 
SetForegroundWindow(calculatorHandle) 
SendKeys.SendWait("111") 
SendKeys.SendWait("*") 
SendKeys.SendWait("11") 
SendKeys.SendWait("=") 
End Sub

 

C# 复制代码 
// Get a handle to an application window. 
[DllImport("USER32.DLL")] 
public static extern IntPtr FindWindow(string lpClassName, 
string lpWindowName); 

// Activate an application window. 
[DllImport("USER32.DLL")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

// Send a series of key presses to the Calculator application. 
private void button1_Click(object sender, EventArgs e) 
{ 
// Get a handle to the Calculator application. The window class 
// and window name were obtained using the Spy++ tool. 
IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator"); 

// Verify that Calculator is a running process. 
if (calculatorHandle == IntPtr.Zero) 
{ 
MessageBox.Show("Calculator is not running."); 
return; 
} 

// Make Calculator the foreground application and send it 
// a set of calculations. 
SetForegroundWindow(calculatorHandle); 
SendKeys.SendWait("111"); 
SendKeys.SendWait("*"); 
SendKeys.SendWait("11"); 
SendKeys.SendWait("="); 
} 



C++ 复制代码 
// Get a handle to an application window. 
public: 
[DllImport("USER32.DLL")] 
static IntPtr FindWindow(String^ lpClassName, String^ lpWindowName); 
public: 
// Activate an application window. 
[DllImport("USER32.DLL")] 
static bool SetForegroundWindow(IntPtr hWnd); 

// Send a series of key presses to the Calculator application. 
private: 
void button1_Click(Object^ sender, EventArgs^ e) 
{ 
// Get a handle to the Calculator application. The window class 
// and window name were obtained using the Spy++ tool. 
IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator"); 

// Verify that Calculator is a running process. 
if (calculatorHandle == IntPtr::Zero) 
{ 
MessageBox::Show("Calculator is not running."); 
return; 
} 

// Make Calculator the foreground application and send it 
// a set of calculations. 
SetForegroundWindow(calculatorHandle); 
SendKeys::SendWait("111"); 
SendKeys::SendWait("*"); 
SendKeys::SendWait("11"); 
SendKeys::SendWait("="); 
} 



System.Windows.Forms.SendKeys
以下是   SendKeys   的一些特殊键代码表。   
  键   代码     
  BACKSPACE   {BACKSPACE}、{BS}   或   {BKSP}     
  BREAK   {BREAK}     
  CAPS   LOCK   {CAPSLOCK}     
  DEL   或   DELETE   {DELETE}   或   {DEL}     
  DOWN   ARROW(下箭头键)   {DOWN}     
  END   {END}     
  ENTER   {ENTER}   或   ~     
  ESC   {ESC}     
  HELP   {HELP}     
  HOME   {HOME}     
  INS   或   INSERT   {INSERT}   或   {INS}     
  LEFT   ARROW(左箭头键)   {LEFT}     
  NUM   LOCK   {NUMLOCK}     
  PAGE   DOWN   {PGDN}     
  PAGE   UP   {PGUP}     
  PRINT   SCREEN   {PRTSC}(保留,以备将来使用)     
  RIGHT   ARROW(右箭头键)   {RIGHT}     
  SCROLL   LOCK   {SCROLLLOCK}     
  TAB   {TAB}     
  UP   ARROW(上箭头键)   {UP}     
  F1   {F1}     
  F2   {F2}     
  F3   {F3}     
  F4   {F4}     
  F5   {F5}     
  F6   {F6}     
  F7   {F7}     
  F8   {F8}     
  F9   {F9}     
  F10   {F10}     
  F11   {F11}     
  F12   {F12}     
  F13   {F13}     
  F14   {F14}     
  F15   {F15}     
  F16   {F16}     
  数字键盘
加号   {ADD}     
  数字键盘减号   {SUBTRACT}     
  数字键盘乘号   {MULTIPLY}     
  数字
键盘除号   {DIVIDE}     

若要指定与   SHIFT、CTRL   和   ALT   键的任意组合一起使用的键,请在这些键代码之前加上以下一个或多个代码:   
    
  键   代码     
  SHIFT   +     
  CTRL   ^     
  ALT   %

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值