1010using Kamishibai ;
1111using Microsoft . Extensions . DependencyInjection ;
1212using Windows . Graphics . Capture ;
13- using Windows . Win32 . Foundation ;
1413using WindowTranslator . Extensions ;
1514using WindowTranslator . Modules . Main ;
1615using WindowTranslator . Properties ;
@@ -24,16 +23,18 @@ public partial class StartupViewModel
2423 private readonly IPresentationService presentationService ;
2524 private readonly IServiceProvider serviceProvider ;
2625 private readonly IMainWindowModule mainWindowModule ;
26+ private readonly IVirtualDesktopManager desktopManager ;
2727 private readonly ObservableCollection < MenuItemViewModel > attachingWindows ;
2828 private IWindow ? logView ;
2929
3030 public IEnumerable < MenuItemViewModel > TaskBarIconMenus { get ; }
3131
32- public StartupViewModel ( IPresentationService presentationService , IServiceProvider serviceProvider , IMainWindowModule mainWindowModule )
32+ public StartupViewModel ( IPresentationService presentationService , IServiceProvider serviceProvider , IMainWindowModule mainWindowModule , IVirtualDesktopManager desktopManager )
3333 {
3434 this . presentationService = presentationService ;
3535 this . serviceProvider = serviceProvider ;
3636 this . mainWindowModule = mainWindowModule ;
37+ this . desktopManager = desktopManager ;
3738 this . attachingWindows = new ( this . mainWindowModule . OpenedWindows . Select ( CreateMenu ) ) ;
3839 this . mainWindowModule . OpenedWindows . CollectionChanged += OpenedWindows_CollectionChanged ;
3940 this . TaskBarIconMenus =
@@ -100,7 +101,7 @@ public async Task RunAsync()
100101 }
101102 return ;
102103 }
103- p = FindProcessByWindowTitle ( item . DisplayName ) ;
104+ p = FindProcessByWindowTitle ( item . DisplayName , item . Size ) ;
104105 if ( p is null )
105106 {
106107 this . presentationService . ShowMessage ( string . Format ( Resources . UnknownWindow , item . DisplayName ) , icon : Kamishibai . MessageBoxImage . Error , owner : window ) ;
@@ -152,23 +153,56 @@ private async Task OpenLogWindowAsync()
152153 public static void Exit ( )
153154 => Application . Current . Shutdown ( ) ;
154155
155- private static ProcessInfo ? FindProcessByWindowTitle ( string targetTitle )
156+ private ProcessInfo ? FindProcessByWindowTitle ( string targetTitle , Windows . Graphics . SizeInt32 targetSize )
156157 {
157- var hWnd = FindWindowEx ( HWND . Null , HWND . Null , null , null ) ;
158+ ProcessInfo ? result = null ;
159+ ProcessInfo ? candidate = null ;
158160
159- while ( hWnd != IntPtr . Zero )
161+ EnumWindows ( ( hWnd , _ ) =>
160162 {
163+ if ( IsIgnoreWindow ( hWnd ) || ! this . desktopManager . IsWindowOnCurrentVirtualDesktop ( hWnd ) )
164+ {
165+ return true ;
166+ }
167+
161168 var windowTitle = GetWindowText ( hWnd ) ;
162- if ( windowTitle = = targetTitle )
169+ if ( windowTitle ! = targetTitle )
163170 {
164- _ = GetWindowThreadProcessId ( hWnd , out var processId ) ;
165- var p = Process . GetProcessById ( processId ) ;
166- return new ProcessInfo ( windowTitle , processId , hWnd , p . ProcessName ) ;
171+ return true ;
167172 }
168173
169- hWnd = FindWindowEx ( HWND . Null , hWnd , null , null ) ;
170- }
171- return null ;
174+ if ( GetWindowThreadProcessId ( hWnd , out var processId ) == 0 )
175+ {
176+ return true ;
177+ }
178+
179+ Process p ;
180+ try
181+ {
182+ p = Process . GetProcessById ( processId ) ;
183+ }
184+ catch ( ArgumentException )
185+ {
186+ return true ;
187+ }
188+
189+ // ウィンドウサイズを取得
190+ var ( width , height ) = GetWindowSizeForWgcCompare ( hWnd ) ;
191+ // サイズが完全一致する場合は即座に結果を設定して終了
192+ if ( width == targetSize . Width && height == targetSize . Height )
193+ {
194+ result = new ProcessInfo ( windowTitle , processId , hWnd , p . ProcessName ) ;
195+ return false ; // 列挙を終了
196+ }
197+
198+ // タイトルは一致するが、サイズが異なる場合は候補として保持
199+ candidate ??= new ProcessInfo ( windowTitle , processId , hWnd , p . ProcessName ) ;
200+
201+ return true ;
202+ } , IntPtr . Zero ) ;
203+
204+ // 完全一致が見つかった場合はそれを返し、そうでなければ候補を返す
205+ return result ?? candidate ;
172206 }
173207
174208 private record ProcessInfo ( string Title , int PID , IntPtr WindowHandle , string Name ) ;
0 commit comments