|
■No95758 (ドフトエフスキー さん) に返信 > 1.マルチモニターの座標取得 仮想デスクトップをどうするべきか…。 https://www.softel.co.jp/blogs/tech/archives/5317
> 上記1〜6を実行すれば > ボタン一つで毎回自分好みのウインドウ状態にできるように思います
手抜き実装なので、ウィンドウの状態(最小化など)の確認や ウィンドウの位置を覚えさせる処理などは作りこんでいませんが、参考までに。
System.Windows.Forms.Form に、Button を 2 つと ListBox / ListView を 1 つずつ貼り、参照設定に [System.Windows] [UIAutomationClient] [UIAutomationTypes] [WindowsBase] を加えておいてください。
Option Strict On Option Explicit On Public Class Form1 Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged Button2.Enabled = ListView1.SelectedItems.Count > 0 End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Button1.Text = "列挙" Button2.Text = "移動" Button2.Enabled = False ListView1.View = View.Details ListView1.FullRowSelect = True ListView1.GridLines = True ListView1.Columns.Clear() ListView1.Columns.Add("PID") ListView1.Columns.Add("Process") ListView1.Columns.Add("Name") ListView1.Columns.Add("HWND") ListView1.Columns.Add("Bounds") ListView1.Columns.Add("FrameworkId") ListView1.Columns.Add("ClassName") End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ListBox1.DataSource = System.Windows.Forms.Screen.AllScreens.Select(Function(scr) scr.DeviceName & " - " & scr.Bounds.ToString() & If(scr.Primary, "(メインモニタ)", "")).ToList()
Dim rawView = System.Windows.Automation.TreeWalker.RawViewWalker Dim aeForm = System.Windows.Automation.AutomationElement.FromHandle(Me.Handle) Dim sibling = rawView.GetNextSibling(aeForm)
ListView1.BeginUpdate() ListView1.Items.Clear() Dim procs As New Dictionary(Of Integer, String)() Do Until sibling Is Nothing With sibling.Current Dim pid = .ProcessId If Not procs.ContainsKey(pid) Then Using p = Process.GetProcessById(pid) procs.Add(pid, p.ProcessName) End Using End If Dim item = ListView1.Items.Add(pid.ToString()) item.Tag = sibling item.SubItems.Add(procs(pid)) item.SubItems.Add(.Name) item.SubItems.Add("&H" & .NativeWindowHandle.ToString("X8")) item.SubItems.Add(.BoundingRectangle.ToString()) item.SubItems.Add(.FrameworkId) item.SubItems.Add(.ClassName) End With sibling = rawView.GetNextSibling(sibling) Loop ListView1.EndUpdate() End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim element = TryCast(ListView1.SelectedItems(0).Tag, System.Windows.Automation.AutomationElement) If element Is Nothing Then MessageBox.Show("UI 要素が未選択です。", "要素なし", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return End If Dim currentName As String = Nothing Try currentName = element.Current.Name Catch MessageBox.Show("この要素には現在アクセスできません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End Try
Dim obj As Object = Nothing If element.TryGetCurrentPattern(System.Windows.Automation.TransformPattern.Pattern, obj) Then Dim result = MessageBox.Show(currentName & vbCrLf & "(100, 80)-(300, 230) に移動させます。よろしいですか?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) If result = System.Windows.Forms.DialogResult.Cancel Then Return End If
Dim transPattern = DirectCast(obj, System.Windows.Automation.TransformPattern) Try transPattern.Move(100, 80) transPattern.Resize(300 - 100, 230 - 80) Catch ex As InvalidOperationException MessageBox.Show("この要素は現在移動できません。", "移動失敗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try Else MessageBox.Show("この要素の移動はサポートされていません。", "移動失敗", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Sub End Class
|