Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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.
0
bamapookie
Asked:
bamapookie
  • 8
  • 5
1 Solution
 
S-TwilleyCommented:
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)
0
 
bamapookieAuthor Commented:
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.
0
 
S-TwilleyCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
S-TwilleyCommented:
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
0
 
bamapookieAuthor Commented:
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)
0
 
S-TwilleyCommented:
When i was hopping around the samples, I think I saw SetWindowPos being used to hide windows... I'll try and pull some code up (you need to use the flags so that it doesn't try to resize the window)... course you can look as well :P
0
 
bamapookieAuthor Commented:
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)
0
 
S-TwilleyCommented:
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
0
 
S-TwilleyCommented:
http://www.mentalis.org/apilist/IsWindowVisible.shtml

obviously change the "Long" for "integer"
0
 
S-TwilleyCommented:
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
0
 
bamapookieAuthor Commented:
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?
0
 
S-TwilleyCommented:
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)
0
 
Erick37Commented:
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?
0
 
bamapookieAuthor Commented:
Yes, it does.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now