Share via


HOW TO:啟動應用程式並且將按鍵傳送至該應用程式 (Visual Basic)

更新:2007 年 11 月

這個範例會使用 Shell 函式啟動計算機應用程式,再使用 My.Computer.Keyboard.SendKeys 方法傳送按鍵輸入,將兩個數字相乘。

範例

Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
' Send the keystrokes to the Calculator application.
My.Computer.Keyboard.SendKeys("22", True)
My.Computer.Keyboard.SendKeys("*", True)
My.Computer.Keyboard.SendKeys("44", True)
My.Computer.Keyboard.SendKeys("=", True)
' The result is 22 * 44 = 968.

這個程式碼範例也可以在 IntelliSense 程式碼片段中找到。在程式碼片段選擇器中,這個程式碼片段位於 [Windows Form 應用程式 > 表單] 中。如需詳細資訊,請參閱 HOW TO:在您的程式碼中插入程式碼片段 (Visual Basic)

穩固程式設計

如果找不到具有要求之處理序識別項的應用程式,則會引發 ArgumentException 例外狀況。

安全性

呼叫 Shell 函式需要完全信任關係 (SecurityException 類別)。

請參閱

參考

My.Computer.Keyboard.SendKeys 方法

Shell 函式

AppActivate 函式