Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to activate the window of the previous startup program when the Win32 program starts

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Win32 program when starting how to activate the previous startup program window, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

UWP programs are born with a single instance. Of course, the new API (10.0.17134) also provides multi-instance functionality from the beginning. However, traditional Win32 programs have to control the single instance on their own.

Activate the window of the previous process

We can find the process instance that has been started before by the process name, and if we find it, activate its window.

[STAThread] static void Main (string [] args) {var current = Process.GetCurrentProcess (); var process = Process.GetProcessesByName (current.ProcessName) .FirstOrDefault (x = > x.Id! = current.Id); if (process! = null) {var hwnd = process.MainWindowHandle; ShowWindow (hwnd, 9); return;} / / starts its own main window, omitting this part of the code. } [DllImport ("user32.dll")] private static extern int ShowWindow (IntPtr hwnd, uint nCmdShow)

You must think that 9 is very strange, it is the value of several different nCmdShow:

Hide

Minimized

Maximized

Restore

In addition, the window found may not be active at this time. For example, in Windows 10, this window may be on another desktop. Then we need to add additional code to display it.

After the previous ShowWindow, you can activate it to the front by calling SetForegroundWindow again. If you are on another desktop, you will switch to the corresponding desktop.

[DllImport ("USER32.DLL")] public static extern bool SetForegroundWindow (IntPtr hWnd); var hwnd = process.MainWindowHandle;ShowWindow (hwnd, 9); SetForegroundWindow (hwnd)

Locate and activate the window

The above methods are suitable for ordinary main windows. However, it does not take effect when the window is not the main window of the process, or when ShowInTaskBar is set to false (when the window handle changes).

Therefore, we need to find the window in other ways.

[STAThread] static void Main (string [] args) {var hwnd = FindWindow (null, "title bar text of that window"); if (hwnd! = IntPtr.Zero) {ShowWindow (hwnd, 9); return;} / / starts your own main window, omitting this part of the code. } [DllImport ("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName); Thank you for reading this article carefully. I hope the article "how to activate the window of the previous program when the Win32 program starts" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report