C # Find other applications and open, display, hide, and close them,

Source: Internet
Author: User

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 ");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.