Windows Forms - Controlando ou acessando Aplicações Externas
Você pode abrir um Excel, bloco de notas, word etc. e através de uma aplicação interagir com este
item externo enviando ordens como se fosse um usuário convencional.
Contudo, você não poderá utilizar o equipamento durante esta 'automação' porque ele já está sendo
usado.
Imports System.Threading
Imports System.Diagnostics
Module AplicacoesExternas
Chamando uma aplicação externa
''' <summary>
''' Esta rotina chama e executa uma app instalada no windows
''' Ex : app = "Age of Mythology" abre e executa o jogo
''' </summary>
''' <param name="app"></param>
''' <returns></returns>
Public Function ChamaExecutaAppExterna(app As String) As Boolean
Try
Interaction.AppActivate(app)
Thread.Sleep(500) '1 segundo
Application.DoEvents()
Return True
Catch ex As Exception
Return False
End Try
End Function
Maximizando a janela da aplicação externa 1
Public Function MaximizarAppExterna(app As String)
'ex : "aomx"
Dim a As String
Try
MaximizeExeByName(app)
Thread.Sleep(300) 'MiliSegundos
Application.DoEvents()
'For Each ObjProcess As Process In Process.GetProcessesByName(a)
' AppActivate(ObjProcess.Id)
' Exit Sub
'Next
'MsgBox("Ativar : Não achei : " + a)
Return True
Catch ex As Exception
a = ex.Message
End Try
Return False
End Function
Private Const SW_SHOWMAXIMIZED As Integer = 3
Private Const SW_SHOWMINIMIZED As Integer = 6
Private Const SW_SHOWNORMAL As Integer = 1
Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer
Maximizando a janela da aplicação externa 2
''' <summary>
''' Retorna true se conseguiu maximizar a app
''' </summary>
''' <param name="ExeNameWithoutPathOrExtension"></param>
''' <returns></returns>
Public Function MaximizeExeByName(ByVal ExeNameWithoutPathOrExtension As String) As Boolean
Dim ps() As Process = Process.GetProcessesByName(ExeNameWithoutPathOrExtension)
If ps.Length > 0 Then
ShowWindow(ps(0).MainWindowHandle, SW_SHOWMAXIMIZED)
SetForegroundWindow(ps(0).MainWindowHandle)
Return True
Else
Return False
End If
End Function
Minimizando a janela da aplicação externa
Public Function MinimizaExeByName(ByVal ExeNameWithoutPathOrExtension As String) As Boolean
Dim ps() As Process = Process.GetProcessesByName(ExeNameWithoutPathOrExtension)
If ps.Length > 0 Then
ShowWindow(ps(0).MainWindowHandle, SW_SHOWMINIMIZED)
SetForegroundWindow(ps(0).MainWindowHandle)
Return True
Else
Return False
End If
End Function
Exibindo ou dando Foco a janela da aplicação externa
Public Function ExibeJanelaAppExtNormal(ByVal ExeNameWithoutPathOrExtension As String) As Boolean
Dim ps() As Process = Process.GetProcessesByName(ExeNameWithoutPathOrExtension)
If ps.Length > 0 Then
ShowWindow(ps(0).MainWindowHandle, SW_SHOWNORMAL)
SetForegroundWindow(ps(0).MainWindowHandle)
Return True
Else
Return False
End If
End Function
Enviando teclas a aplicação externa
Public Function EnviaTecla(tecla As String) As Boolean
Dim a As String
Dim b As Boolean 'true=tecla enviada, false não enviou
a = UCase(tecla)
b = False
Select Case a
Case "SHIFT"
My.Computer.Keyboard.SendKeys("+", True)
b = True
Case "ALT"
My.Computer.Keyboard.SendKeys("%", True)
b = True
Case "CONTROL"
My.Computer.Keyboard.SendKeys("^", True)
b = True
Case "ENTER"
My.Computer.Keyboard.SendKeys("~", True)
'My.Computer.Keyboard.SendKeys("{ENTER}", True)
b = True
Case "ESC"
My.Computer.Keyboard.SendKeys("{ESC}", True)
b = True
Case "INS" 'INSERT
My.Computer.Keyboard.SendKeys("{INS}", True)
b = True
Case "BACKSPACE"
My.Computer.Keyboard.SendKeys("{BACKSPACE}", True)
b = True
Case "BREAK"
My.Computer.Keyboard.SendKeys("{BREAK}", True)
b = True
Case "CAPSLOCK"
My.Computer.Keyboard.SendKeys("{CAPSLOCK}", True)
b = True
Case "DELETE"
My.Computer.Keyboard.SendKeys("{DEL}", True)
'My.Computer.Keyboard.SendKeys("{DELETE}", True)
b = True
Case "END"
My.Computer.Keyboard.SendKeys("{END}", True)
b = True
Case "HELP"
My.Computer.Keyboard.SendKeys("{HELP}", True)
b = True
Case "HOME"
My.Computer.Keyboard.SendKeys("{HOME}", True)
b = True
Case "NUMLOCK"
My.Computer.Keyboard.SendKeys("{NUMLOCK}", True)
b = True
Case "PGDN"
My.Computer.Keyboard.SendKeys("{PGDN}", True)
b = True
Case "PGUP"
My.Computer.Keyboard.SendKeys("{PGUP}", True)
b = True
Case "PRTSC" 'Print Screen - (reserved for future use) - FUNCIONA ?
My.Computer.Keyboard.SendKeys("{PRTSC}", True)
b = True
Case "SCROLLLOCK" 'Print Screen - (reserved for future use) - FUNCIONA ?
My.Computer.Keyboard.SendKeys("{SCROLLLOCK}", True)
b = True
Case "TAB" 'Print Screen - (reserved for future use) - FUNCIONA ?
My.Computer.Keyboard.SendKeys("{TAB} ", True)
b = True
Case "SETABAIXO"
My.Computer.Keyboard.SendKeys("{DOWN}", True)
b = True
Case "SETACIMA"
My.Computer.Keyboard.SendKeys("{UP}", True)
b = True
Case "SETAESQUERDA"
My.Computer.Keyboard.SendKeys("{LEFT}", True)
b = True
Case "SETADIREITA"
My.Computer.Keyboard.SendKeys("{RIGHT}", True)
b = True
Case "F1"
My.Computer.Keyboard.SendKeys("{F1}", True)
b = True
Case "F2"
My.Computer.Keyboard.SendKeys("{F2}", True)
b = True
Case "F3"
My.Computer.Keyboard.SendKeys("{F3}", True)
b = True
Case "F4"
My.Computer.Keyboard.SendKeys("{F4}", True)
b = True
Case "F5"
My.Computer.Keyboard.SendKeys("{F5}", True)
b = True
Case "F6"
My.Computer.Keyboard.SendKeys("{F6}", True)
b = True
Case "F7"
My.Computer.Keyboard.SendKeys("{F7}", True)
b = True
Case "F8"
My.Computer.Keyboard.SendKeys("{F8}", True)
b = True
Case "F9"
My.Computer.Keyboard.SendKeys("{F9}", True)
b = True
Case "F10"
My.Computer.Keyboard.SendKeys("{F10}", True)
b = True
Case "F11"
My.Computer.Keyboard.SendKeys("{F11}", True)
b = True
Case "F12"
My.Computer.Keyboard.SendKeys("{F12}", True)
b = True
Case "F13"
My.Computer.Keyboard.SendKeys("{F13}", True)
b = True
Case "F14"
My.Computer.Keyboard.SendKeys("{F14}", True)
b = True
Case "F15"
My.Computer.Keyboard.SendKeys("{F15}", True)
b = True
Case "F16"
My.Computer.Keyboard.SendKeys("{F16}", True)
b = True
Case "+"
My.Computer.Keyboard.SendKeys("{ADD}", True)
b = True
Case "-"
My.Computer.Keyboard.SendKeys("{SUBTRACT}", True)
b = True
Case "*"
My.Computer.Keyboard.SendKeys("{MULTIPLY}", True)
b = True
Case "/"
My.Computer.Keyboard.SendKeys("{DIVIDE}", True)
b = True
End Select
Return b
End Function
End Module