tagCANDY CGI VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re: VBで作った別アプリを制御
投稿日: 2017/08/02(Wed) 19:01
投稿者魔界の仮面弁士
> 質問内容 自分で作成した他のVBアプリ(同じもの複数2〜9個)の特定のボタンを同時に押すアプリを作りたい。そんな事ってできますか?

アプリの作り次第ではありますが、たとえばこういう手はあります。

'参照設定:
' UIAutomationClient
' UIAutomationTypes
Imports System.Windows.Automation
Public Class Form1
    Private procs As New List(Of Process)()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '電卓を10個起動しておき、起動完了まで待機
        Dim tasks As New List(Of Task)()
        For n = 1 To 10
            Dim p = Process.Start("CALC.EXE")
            procs.Add(p)
            tasks.Add(Task.Factory.StartNew(AddressOf p.WaitForInputIdle))
        Next
        Task.WaitAll(tasks.ToArray())
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'フォームが閉じられたら電卓も閉じる
        For Each p In procs
            Try
                p.CloseMainWindow()
                p.Dispose()
            Catch
            End Try
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '電卓群の「5」ボタンを取得
        Dim buttonName As String = "5"
        Dim condition As New AndCondition(
            New PropertyCondition(AutomationElement.NameProperty, buttonName),
            New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))
        Dim buttons = procs.SelectMany(
            Function(p) AutomationElement.FromHandle(p.MainWindowHandle).FindAll(
                            TreeScope.Element Or TreeScope.Descendants, condition
                        ).Cast(Of AutomationElement)()
            ).Select(
                Function(p) TryCast(p.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
            ).ToArray()

        '「5」ボタン群を一斉にクリック
        Array.ForEach(buttons, Sub(b) b.Invoke())
    End Sub

End Class



あるいは、適当なプロセス間通信で自作アプリ側にコマンドを投げるようにし、
コマンドの受信側となる自作アプリ側に
 「特定のコマンドが渡されたら、該当ボタンの PerfomeClick メソッドを呼び出す」
という機能を用意しておくとか。

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。