2

I want to show the windows form as a pop up window that appear on the top of all the opened other application's windows i have used Focus method but it didn't work. so i have tried :

using System.Diagnostics;
using System.Runtime.InteropServices;

// Sets the window to be foreground
[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);

// Activate or minimize a window
[DllImportAttribute("User32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;

private void ActivateApplication(string briefAppName)
{
    Process[] procList = Process.GetProcessesByName(briefAppName);

    if (procList.Length > 0)
    {
        ShowWindow(procList[0].MainWindowHandle, SW_RESTORE);
        SetForegroundWindow(procList[0].MainWindowHandle);
    }
}

as mentioned in a previous question on SO Here but i couldn't use it . the right answer poster said "Basically, call ShowWindow() then SetForegroundWindow()." but i didn't know what are the parameters for these methods

what exactly should i pass to the ShowWindow(); and the SetForegroundWindow(); methods?? any help?

Community
  • 1
  • 1
Hassanation
  • 866
  • 2
  • 14
  • 29

1 Answers1

2

Here is my solution:

private void ActivateApplication (string briefAppName) 
    {
        Process[] p=Process.GetProcessesByName (briefAppName);
        if (p.Length>0)
        {
            this.TopMost=true;
            ShowWindow (p[0].MainWindowHandle, 9);
            this.TopMost=false;
            this.Activate ();
        }
    }

Use .Activate () to focus the form and TopMost to change Always-on-top state of the form.

9 means Resotre window. If your window is already restored, ShowWindow function will do nothing. Look here for ShowWindow function's documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx