Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions ContextMenuManager/BluePointLilac.Methods/ExternalProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,36 @@
//注册表编辑器窗口多开
process = Process.Start("regedit.exe", "-m");
process.WaitForInputIdle();
hMain = process.MainWindowHandle;

// 等待主窗口句柄可用,最多等待5秒
int retries = MAX_WINDOW_WAIT_RETRIES;
while (retries-- > 0)
{
process.Refresh();
hMain = process.MainWindowHandle;
if (hMain != IntPtr.Zero) break;
Thread.Sleep(100);
}

if (hMain == IntPtr.Zero) return;
}

ShowWindowAsync(hMain, SW_SHOWNORMAL);
SetForegroundWindow(hMain);
var hTree = FindWindowEx(hMain, IntPtr.Zero, "SysTreeView32", null);
var hList = FindWindowEx(hMain, IntPtr.Zero, "SysListView32", null);

// 等待树视图和列表视图控件就绪,最多等待5秒
IntPtr hTree = IntPtr.Zero;
IntPtr hList = IntPtr.Zero;
int retries2 = MAX_CHILD_WINDOW_WAIT_RETRIES;
while (retries2-- > 0)
{
hTree = FindWindowEx(hMain, IntPtr.Zero, "SysTreeView32", null);
hList = FindWindowEx(hMain, IntPtr.Zero, "SysListView32", null);
if (hTree != IntPtr.Zero && hList != IntPtr.Zero) break;
Thread.Sleep(100);
}

if (hTree == IntPtr.Zero || hList == IntPtr.Zero) return;

SetForegroundWindow(hTree);
SetFocus(hTree);
Expand Down Expand Up @@ -224,7 +247,7 @@
UseShellExecute = true
})?.Dispose();
}
catch (Exception ex1) when (

Check warning on line 250 in ContextMenuManager/BluePointLilac.Methods/ExternalProgram.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex1' is declared but never used

Check warning on line 250 in ContextMenuManager/BluePointLilac.Methods/ExternalProgram.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex1' is declared but never used
ex is Win32Exception ||
ex is InvalidOperationException ||
ex is UnauthorizedAccessException)
Expand Down Expand Up @@ -295,6 +318,8 @@
private const int WM_CHAR = 0x102;
private const int VK_HOME = 0x24;
private const int VK_RIGHT = 0x27;
private const int MAX_WINDOW_WAIT_RETRIES = 50; // 等待主窗口最多5秒 (50 * 100ms)
private const int MAX_CHILD_WINDOW_WAIT_RETRIES = 50; // 等待子窗口最多5秒 (50 * 100ms)

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
Expand Down
Loading