Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show a Process using c#?

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

alt text http://img143.imageshack.us/img143/9380/minimize.png

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
  4. SetForegroundWindow
  5. SetWindowPos

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);

NOTE: Does the SAPI API has a command to minimize this minimize and maximize this window?

like image 881
aF. Avatar asked Jun 13 '10 12:06

aF.


3 Answers

As Tomas said, you should try to use the SW_HIDE and SW_SHOW messages.

You do that by knowing the Speech Recognition winwow name and then using something like this:

HWND hc = FindWindow("processname","Windowtitle"); 
ShowWindow(hc,SW_HIDE);
like image 84
Juan Nunez Avatar answered Nov 20 '22 08:11

Juan Nunez


The whole SetForegroundWindow/ShowWindow set of functions only work when stars align! :) It's usually a matter of calling functions in the right order. Sorry can't help specifically but this might provide some ideas

http://markribau.org/blog/2005/12/29/why-dont-focus-and-setforegroundwindow-work/

like image 31
hawk Avatar answered Nov 20 '22 09:11

hawk


Is the procoess still runing if you send it the SW_HIDE message? The application is certainly not using the standard style of GUI, so it may react to the message by closing itself.

If that's the case, you could try other tricks, such as moving the window to some invisible location (e.g. -1000, -1000), which should be also possible using the SetWindowPlacement method that you already imported.

like image 25
Tomas Petricek Avatar answered Nov 20 '22 07:11

Tomas Petricek