How to hide/show a Process using c#?

Posted by aF on Stack Overflow See other posts from Stack Overflow or by aF
Published on 2010-06-13T12:09:33Z Indexed on 2010/06/13 12:12 UTC
Read the original article Hit count: 274

Filed under:
|
|
|
|

Hello,

While executing my program, I want to hide/minimize Microsoft Speech Recognition Application:

alt text

and at the end I want to show/maximize using c#!

This process is not started by me so I can't give control the process startInfo.

I've tried to use user32.dll methods such as:

  1. ShowWindow
  2. AnimatedWindows
  3. AnimatedWindows

With all of them I have the same problem.

I can hide the windows (althought I have to call one of the methods two times with SW_HIDE option), but when I call the method with a SW_SHOW flag, it simply doesn't shows..

How can I maximize/show after hiding the process?

Thanks in advance!

Here is some pieces of the code, now implemented to use SetWindowPlacement:

{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPlacement(IntPtr hWnd,
       [In] ref WINDOWPLACEMENT lpwndpl);
    [DllImport("user32.dll")]
    public static extern Boolean ShowWindowAsync(IntPtr hWnd, Int32 nCmdShow);
    [DllImport("user32.dll")]
    public static extern Boolean SetForegroundWindow(IntPtr hWnd);        
    [DllImport("user32.dll")]
    public static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    [DllImport("user32.dll")]
    public static extern Boolean AnimateWindow(IntPtr hWnd, uint dwTime, uint dwFlags);
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, uint dwAttribute, IntPtr pvAttribute, IntPtr lol);
//Definitions For Different Window Placement Constants
const UInt32 SW_HIDE = 0;
const UInt32 SW_SHOWNORMAL = 1;
const UInt32 SW_NORMAL = 1;
const UInt32 SW_SHOWMINIMIZED = 2;
const UInt32 SW_SHOWMAXIMIZED = 3;
const UInt32 SW_MAXIMIZE = 3;
const UInt32 SW_SHOWNOACTIVATE = 4;
const UInt32 SW_SHOW = 5;
const UInt32 SW_MINIMIZE = 6;
const UInt32 SW_SHOWMINNOACTIVE = 7;
const UInt32 SW_SHOWNA = 8;
const UInt32 SW_RESTORE = 9;

public sealed class AnimateWindowFlags
{
    public const int AW_HOR_POSITIVE = 0x00000001;
    public const int AW_HOR_NEGATIVE = 0x00000002;
    public const int AW_VER_POSITIVE = 0x00000004;
    public const int AW_VER_NEGATIVE = 0x00000008;
    public const int AW_CENTER = 0x00000010;
    public const int AW_HIDE = 0x00010000;
    public const int AW_ACTIVATE = 0x00020000;
    public const int AW_SLIDE = 0x00040000;
    public const int AW_BLEND = 0x00080000;
}

public struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public System.Drawing.Point ptMinPosition;
    public System.Drawing.Point ptMaxPosition;
    public System.Drawing.Rectangle rcNormalPosition;
}


            //this works

            param = new WINDOWPLACEMENT();
            param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
            param.showCmd = (int)SW_HIDE;
            lol = SetWindowPlacement(theprocess.MainWindowHandle, ref param);


            // this doesn't work

            WINDOWPLACEMENT param = new WINDOWPLACEMENT();
            param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
            param.showCmd = SW_SHOW;
            lol = GetWindowPlacement(theprocess.MainWindowHandle, ref param);

© Stack Overflow or respective owner

Related posts about c#

Related posts about process