This function sets the display state of a window generated by a different thread.

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;
Using System.Diagnostics;
Using System.Reflection;

Namespace WindowsFormsApplication1
{
Static Class Program
{
<summary>
<summary>
This function sets the display state of a window generated by a different thread.
</summary>
<param name= "hWnd" > Window handle </param>
<param name= "Cmdshow" > Specifies how the window is displayed. To view a list of allowed values, consult the description section of the Showwlndow function. </param>
<returns> If the function is originally visible, the return value is not 0, and if the function is originally hidden, the return value is zero. </returns>
[DllImport ("User32.dll")]
private static extern bool Showwindowasync (IntPtr hWnd, int cmdshow);
<summary>
The function creates a thread setting for the specified window to the foreground and activates the window. The keyboard input turns to the window and changes the various visual marks for the user. The system assigns a slightly higher permission to the thread that creates the foreground window than the other threads.
</summary>
<param name= "hWnd" > the window handle that will be activated and transferred into the foreground. </param>
<returns> If the window is in the foreground, the return value is not 0, and the return value is zero if the window is not set in the foreground. </returns>
[DllImport ("User32.dll")]
private static extern bool SetForegroundWindow (IntPtr hWnd);
Private Const int ws_shownormal = 1;
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Process instance = Runninginstance ();
if (instance = = null)
{
Form1 frm = new Form1 ();
Application.Run (New Form1 ());
}
Else
{
Handlerunninginstance (instance);
}


}
public static Process Runninginstance ()
{
Process current = Process.getcurrentprocess ();
Process[] processes = Process.getprocessesbyname (current. ProcessName);
foreach (process process in processes)
{
if (process. Id! = Current. ID)
{
if (assembly.getexecutingassembly (). Location.replace ("/", "\ \") = = Current. Mainmodule.filename)
{
return process;
}
}
}
return null;
}

<summary>
Displays the programs that have been run.
</summary>
public static void Handlerunninginstance (Process instance)
{
Showwindowasync (instance. Mainwindowhandle, Ws_shownormal); displayed, can be commented out
SetForegroundWindow (instance. Mainwindowhandle); Put it on the front.
}

}
}

This function sets the display state of a window generated by a different thread.

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.