Link to home
Start Free TrialLog in
Avatar of bamapookie
bamapookie

asked on

Checking the window state of a running process.

What is the best way to check and change the window state of a process
given either a System.Diagnostics.Process or a MainWindowHandle?  I was
using the following commands, but hide is not working for some reason.

ShowWindow(prTarget.MainWindowHandle, SW.HIDE) ' This doesn't always work
SendMessage(prTarget.MainWindowHandle, WM.SYSCOMMAND, SC.MAXIMIZE, 0)
SendMessage(prTarget.MainWindowHandle, WM.SYSCOMMAND, SC.MINIMIZE, 0)
SendMessage(prTarget.MainWindowHandle, WM.SYSCOMMAND, SC.RESTORE, 0)

Note that prTarget is of type System.Diagnostics.Process.
SW, WM, and SC are enums with the appropriately defined values.
Avatar of S-Twilley
S-Twilley

can you post up your API declaration for Show/SendMessage...

also.. i think these should be:

ShowWindow(prTarget.MainWindowHandle.ToInt32, SW.HIDE) ' This doesn't always work
SendMessage(prTarget.MainWindowHandle.ToInt32, WM.SYSCOMMAND, SC.MAXIMIZE, 0)
SendMessage(prTarget.MainWindowHandle.ToInt32, WM.SYSCOMMAND, SC.MINIMIZE, 0)
SendMessage(prTarget.MainWindowHandle.ToInt32, WM.SYSCOMMAND, SC.RESTORE, 0)
Avatar of bamapookie

ASKER

The API declaration for the functions is:

<System.Runtime.InteropServices.DllImport("User32.dll")> _
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")> _
Private Shared Function SendMessage(ByVal Handle As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

The last three entries (SendMessage) work fine, but there is no value corresponding to HIDE in SendMessage.
well, from memory, I always thought that that the API's were declared with Integer's and not IntPtrs... might not make a difference tho:

<System.Runtime.InteropServices.DllImport("User32.dll")> _
Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("User32.dll")> _
Private Shared Function SendMessage(ByVal Handle As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

=======

That would be using the code i gave above as well

=======

I'll double check which constant you should be using... and post in a moment
Well... .  just a quick look, one solution had   5 to show... and 0 to hide.

I'll see if there is an alternative as well
Well, I'm not opposed to changing it to Int32, but the windows are getting the commands (They just behave funny, see below).  Also, I swiped the API declaration from MSDN Documentation.

About my app:
The app I'm writing is supposed to monitor a bunch of running processes.  If they die, my app restarts them.  It works, and that's fine, but now I'm adding in a few new features (ie, the ability to hide/show the apps.)

About the funny behavior:
The particular app that my app was designed to monitor does not play nice with the ShowWindow command.  It works fine with the SendMessage command, but there is no hide constant for SendMessage. (btw, I pulled my values for WM, SC, and SW from winuser.h)  Using ShowWindow, the app minimizes to an icon BEHIND the start menu if I use SW.MINIMIZE and does nothing if I use SW.HIDE.  Other apps work fine with these commands (I tested with notepad, charmap, and calc)
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll look too.  I remember seeing something about that in winuser.h.  Do you know if there is a way to query the window's state (ie hidden, min, max, etc)
I don't know how to get whether a window is maximized or not... you could test the size and see if it hits around the corners of the screens... I remember an API for getting visible....  I think it was something like IsVisible or somethin, I'll have a look
http://www.mentalis.org/apilist/IsWindowVisible.shtml

obviously change the "Long" for "integer"
http://www.mentalis.org/apilist/IsZoomed.shtml
determines whether its maximized

http://www.mentalis.org/apilist/IsIconic.shtml
determines whether its minimized

==============================

not sure if these work though
This may moot all the previous discussion: Is there a way to get a System.Windows.Forms.Form from a System.Diagnostics.Process, or from the Processes MainWindowHandle?
can you wrap a window into a windows form object to expose properties?

no.. they can be made of completely different classes....  some windows you can get access to, like IE instances... but for the most part, you have to use APIs to get properties from windows

(As far as i know)
Does this window you are trying to hide or minimize have a Minimize button?  That is, is the window style include the ability for the user to minimize the window?
Yes, it does.