开发者

Activate window of hidden process

开发者 https://www.devze.com 2023-04-06 09:44 出处:网络
If the user tries to start another instance of my application, I want to activate the window of the process which is already running.

If the user tries to start another instance of my application, I want to activate the window of the process which is already running.

To find the process, I call Process.GetProcessesByName(), which gives me the System.Diagnostics.Process instance of the running instance of my application. However, I have hidden my process from the taskbar using

Form.ShowInTaskbar = false

This causes the Process.MainWindowHandle to be zero, so I can not access the current window of the running process.

Is there 开发者_如何学Pythonanother way I can activate the window of the already running process?


Yes, the ShowInTaskBar property is special. There are several other properties of the Form class that are in the same category. These properties are implemented by style flags specified in the native CreateWindowEx() winapi call. The WS_EX_APPWINDOW flag for ShowInTaskBar.

Which is a problem when you change these properties, the window has to be recreated. Winforms does this automatically for you but it has several side-effects. One of which is that the Handle property value changes. Making it impossible for the Process class to find the MainWindowHandle back.

You'll have to find the window back another way. Making EnumWindows work is definitely not easy for Winforms forms, you can't get a guessable window class name. Not changing the ShowInTaskBar property is certainly the better approach. Also consider using the WindowsFormsApplicationBase class, it makes this trivial with the OnStartupNextInstance method.

0

精彩评论

暂无评论...
验证码 换一张
取 消