how get program to front from taskbar c#

1.3k views Asked by At

can someone tell me how I can bring my program to front from taskbar (I don`t know is good name) or what is name this event in winform/wpf in c#. Problem is sometimes (not all the time) when I minimalie my program and click again then can show it but sometimes is still under another programs and I want make code when I click icon my program then always bring to front.

Example paint

1

There are 1 answers

0
Jacobr365 On BEST ANSWER

Try using the SetForegroundWindow() and ShowWindow() methods The import for them are

[DllImport("User32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

Declare an an constant called ActiveResize (or whatever name you want. like this. internal const int ActiveResize = 3; More size codes here.

Then generate an Activated event that will look something like this

private void YOURPROGRAM_Activated(object sender, EventArgs e)
{
    Process currentProcess = Process.GetCurrentProcess();
    IntPtr hWnd = currentProcess.MainWindowHandle;
    if (hWnd != User32.InvalidHandleValue)
    {
        User32.SetForegroundWindow(hWnd);
        User32.ShowWindow(hWnd, ActiveResize);
    }
}

What this does is when you the form is made active in any way the form gets an activated message. When it gets the message this code is then run which forces the window to be set to the foreground. Hope this helps.