[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
None.
The hWnd parameter can be obtained by the Handle property of a Form, or by
MainWindowHandle on a Process.
The corresponding unmanaged return type is a 4-byte Win32 'BOOL', so mark the method with the MarshalAsAttribute(UnmanagedType.Bool).
Please add some!
// Want only one instance of an application?
[DllImport("User32.Dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.Dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("User32.Dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
/// <summary>
/// Main entry point for the application.
/// </summary>
[STAThread]
static int
Main
(
string[] arguments
)
{
// Check for running instance
Process thisProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(thisProcess.ProcessName);
if (processes.Length > 1)
{
// One is us (which will end); the other we activate
int index = 0;
if (processes[index].Id == thisProcess.Id)
index = 1;
IntPtr hWnd = processes[index].MainWindowHandle;
if (IsIconic(hWnd))
ShowWindowAsync(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
// Exit
return 0;
}
// Normal startup code continues here...
}
Do you know one? Please contribute it!