# VB.Net ProcessWindowStyle porperty



## psyko12 (Aug 8, 2010)

```
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Public Class Form1
    Const SW_HIDE As Integer = 0
    Const SW_RESTORE As Integer = 9
    Dim hWnd As Integer
    Dim p As Process() = Process.GetProcessesByName("notepad")
    Declare Auto Function ShowWindow Lib "User32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowWindow(hWnd, SW_HIDE)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ShowWindow(hWnd, SW_RESTORE)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        hWnd = p(0).MainWindowHandle.ToInt32
    End Sub
```

So far this does what I want it to do, hide the application from the taskbar and will be able to show it again on screen.

But this works only if notepad(other apps) is alread running ahead of my program.
If I ran my program first and open up notepad it won't work.
How can I set it to work ?


----------



## FordGT90Concept (Aug 8, 2010)

Why not set Form.ShowInTaskbar to false?

If you are working with a dif app, the problem in your code, from what I can see, is because shwhid is a local variable, you need to move it to class-level.  Everytime Button2_Click is called, shwid will equal false.  The second line below it will always set it to true; thus, making the else code unreachable.


----------



## psyko12 (Aug 8, 2010)

FordGT90Concept said:


> Why not set Form.ShowInTaskbar to false?
> 
> If you are working with a dif app, the problem in your code, from what I can see, is because shwhid is a local variable, you need to move it to class-level.  Everytime Button2_Click is called, shwid will equal false.  The second line below it will always set it to true; thus, making the else code unreachable.



Prior to my post I've fixed it but I have one question hehe.. Sorry.

I'll just edit the 1st post for my question about the same code (fixed code).

EDIT: 1st post edited with fixed code but some monitoring problem.


----------



## FordGT90Concept (Aug 8, 2010)

Dim p As Process() = Process.GetProcessesByName("notepad")
...
hWnd = p(0).MainWindowHandle.ToInt32

You are getting the handle on Form1 instantiation.  If you want it to work whenever a button is pressed, you have to get the process, get the handle, and send the User32 command all in the sub routines.  Throw a Try...Catch around it to prevent it crashing if the process can't be found or handle is unavailable.


----------



## psyko12 (Aug 8, 2010)

FordGT90Concept said:


> Dim p As Process() = Process.GetProcessesByName("notepad")
> ...
> hWnd = p(0).MainWindowHandle.ToInt32
> 
> You are getting the handle on Form1 instantiation.  If you want it to work whenever a button is pressed, you have to get the process, get the handle, and send the User32 command all in the sub routines.  Throw a Try...Catch around it to prevent it crashing if the process can't be found or handle is unavailable.



Hmm so I've tried the Try and Catch thing It works so far... It doesn't crash if the process is not present, but how can I refresh the entire program again so that it will perform the process searching?


```
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            hWnd = p(0).MainWindowHandle.ToInt32
        Catch
            Me.Refresh()
        End Try
    End Sub
```


----------



## FordGT90Concept (Aug 8, 2010)

```
Private Function ApplicationHide(ByVal processname As String)
		Try
			ShowWindow(Process.GetProcessesByName(processname)(0).MainWindowHandle.ToInt32(), 0)
		End Try
	End Function
	Private Function ApplicationShow(ByVal processname As String)
		Try
			ShowWindow(Process.GetProcessesByName(processname)(0).MainWindowHandle.ToInt32(), 9)
		End Try
	End Function
```
Call ApplicationHide() and ApplicationShow() where needed.


----------



## psyko12 (Aug 9, 2010)

FordGT90Concept said:


> ```
> Private Function ApplicationHide(ByVal processname As String)
> Try
> ShowWindow(Process.GetProcessesByName(processname)(0).MainWindowHandle.ToInt32(), 0)
> ...



Hey Ford, Thank you for your patience with me. I really appreciate it as your help provides some stepping stones for my development with this language. Thank you very much!
Will try that out and experiment further 


EDIT: I have made it work but by a different logic/process

```
Option Strict On
Option Explicit On
Public Class Form1

  Dim hWnd As IntPtr
  Const SW_HIDE As Integer = 0
  Const SW_RESTORE As Integer = 9
  Declare Auto Function ShowWindow Lib "User32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim p As Process() = Process.GetProcessesByName("notepad")
    hWnd = p(0).MainWindowHandle
    ShowWindow(hWnd, SW_HIDE)
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    ShowWindow(hWnd, SW_RESTORE)
  End Sub
End Class
```

Hehe while I was trying your code I ran into a problem about:


> Error	3	Argument not specified for parameter 'processname' of 'Private Function ApplicationHide(processname As String) As Object'.	D:\PROGLANG\VS2010\Projects\testing lang\WindowsApplication1\WindowsApplication1\Form1.vb	22	9	WindowsApplication1



Well I will still use the Try/Catch function for error handling as you've suggested. Thanks 1000x


----------



## FordGT90Concept (Aug 9, 2010)

Button1_Click should work but Button2_Click could still error.


In my code, they should both be Subs and not Functions (they don't return anything).  Additionally, you would call them like: ApplicationHide("notepad") or ApplicationShow("notepad")

Note that if there is more than one instance of notepad open, it may target the wrong one.


----------



## psyko12 (Aug 9, 2010)

FordGT90Concept said:


> Button1_Click should work but Button2_Click could still error.
> 
> 
> In my code, they should both be Subs and not Functions (they don't return anything).  Additionally, you would call them like: ApplicationHide("notepad") or ApplicationShow("notepad")
> ...



Prior to your reply yea I've figured that I made a boo boo with call command forgetting to input the string for the process name.

Thanks!!!  This really helped alot also made error prompt when the proccess is not running via the Try and Catch commands.



EDIT:

I think there's still something wrong with application show... When I tried it it still doesn't show the only instance of notepad hidden by the 1st button who called application hide.

Was trying out your code. Or maybe I'm the one at fault.


----------



## FordGT90Concept (Aug 9, 2010)

It doesn't work because when SW_HIDE is sent, Notepad closes (as in notepad.exe is no longer running).  SW_RESTORE, therefore, never reaches the target.


```
Module Module1
    Public Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    Public Enum ShowWindowCommand As Integer
        SW_HIDE = 0             ' Hides the window and activates another window.
        SW_SHOWNORMAL = 1       ' Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
        SW_SHOWMINIMIZED = 2    ' Activates the window and displays it as a minimized window.
        SW_MAXIMIZE = 3         ' Maximizes the specified window.
        SW_SHOWMAXIMIZED = 3    ' Activates the window and displays it as a maximized window.
        SW_SHOWNOACTIVATE = 4   ' Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated.
        SW_SHOW = 5             ' Activates the window and displays it in its current size and position. 
        SW_MINIMIZE = 6         ' Minimizes the specified window and activates the next top-level window in the Z order.
        SW_SHOWMINNOACTIVE = 7  ' Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
        SW_SHOWNA = 8           ' Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated.
        SW_RESTORE = 9          ' Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
        SW_SHOWDEFAULT = 10     ' Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. 
        SW_FORCEMINIMIZE = 11   ' Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread.
    End Enum

    Sub Main()
        ApplicationVisible("notepad", ShowWindowCommand.SW_HIDE)
        ApplicationVisible("notepad", ShowWindowCommand.SW_RESTORE)
    End Sub
    Private Function ApplicationVisible(ByVal processname As String, ByVal command As ShowWindowCommand) As Boolean
        Try
            ShowWindow(Process.GetProcessesByName(processname)(0).MainWindowHandle, CType(command, Integer))
            Return True
        Catch
            Return False
        End Try
    End Function
End Module
```


----------



## psyko12 (Aug 9, 2010)

Hey man really thanks! I've also ran across that but didn't understood what to do with it! Now it clarifies everything. 

Thank you for your remarkable patience with me.


----------

