C # Find other applications and open, display, hide, and close them,
In software development, third-party software is sometimes mandatory. In this case, the C # application needs to open, display, hide, and close third-party software.
The following lists several common methods.
Open the application. There are two simple usage options:
First:
Public enum ShowWindowCommands: int {SW_HIDE = 0, SW_SHOWNORMAL = 1, // display with the nearest size and position, activate SW_NORMAL = 2, SW_SHOWMINIMIZED = 3, SW_SHOWMAXIMIZED = 4, SW_MAXIMIZE = 5, SW_SHOWNOACTIVATE = 6, SW_SHOW = 7, SW_MINIMIZE = 8, SW_SHOWMINNOACTIVE = 9, SW_SHOWNA = 10, SW_RESTORE = 11, SW_SHOWDEFAULT = 12, SW_MAX = 13} [DllImport ("shell32.dll")] public static extern IntPtr ShellExecute (IntPtr hwnd, string lpszOp, string lpszFile, string lpszParams, string lpszDir, ShowWindowCommands release ); shellExecute (IntPtr. zero, "open", @ "D: \ Program Files \ OtherExe.exe", null, null, ShowWindowCommands. SW_SHOWMINIMIZED );
Second:
Process myProcess = new Process();myProcess.StartInfo.UseShellExecute = true;myProcess.StartInfo.FileName = @"D:\Program Files\OtherExe.exe";myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;myProcess.Start();
Sometimes, when we open other software, we don't want to show it, but only hide it when we open it, although in the above example myProcess. startInfo. windowStyle = ProcessWindowStyle. normal; it involves the display status of the window, but sometimes it is not displayed as expected, it may be that my level is limited, not used correctly ---.
Next I used another method to find, hide, and close the form.
[DllImport("user32.dll", EntryPoint = "FindWindow")]private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);[DllImport("User32.dll", EntryPoint = "SendMessage")]private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
Hide a form
IntPtr OtherExeWnd = new IntPtr (0); OtherExeWnd = FindWindow ("SunAwtFrame", null); // determine whether the form is valid if (OtherExeWnd! = IntPtr. zero) {Console. writeLine ("find window"); ShowWindow (OtherExeWnd, 0); // 0 indicates hiding window} else {Console. writeLine ("no window found ");}
Close form
IntPtr OtherExeWnd = new IntPtr (0); OtherExeWnd = FindWindow ("SunAwtFrame", null); // determine whether the form is valid if (OtherExeWnd! = IntPtr. Zero) {Console. WriteLine ("find window"); SendMessage (OtherExeWnd, 16, 0, 0); // close the window by sending messages
}
Else
{
Console. WriteLine ("no window found ");
}