• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

VB.Net ProcessWindowStyle porperty

Joined
Jun 5, 2007
Messages
2,167 (0.33/day)
Location
Metro Manila, Philippines
System Name Zangief (Reborn) 2.0 AM4 not dead yet!
Processor AMD Ryzen R7 5700X3D (tweaked PBO)
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F53d
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x16GB G.Skill Trident Z Neo RGB @ 3600mhz CL 18 -> running 3800mhz @ 1.37v
Video Card(s) Asus RTX 3080 Rog Strix Gaming OC
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 4tb WD Blue / 512GB Adata XPG Pro SX8200
Display(s) Acer Predator XB271HU |Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD | Steelseries Arctis Nova Pro Wireless
Power Supply Seasonic Focus GX V4 850 Watts ATX 3.1
Mouse Logitech G903
Keyboard AKKO 5098N (Otemu Cocoa Brown Lubed) | Tecware Spectre 96 (Akko Lavander Purple V3 lubed)
Software Windows 11 Pro 24H2
Code:
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 ?
 
Last edited:
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.
 
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.
 
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.
 
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?

Code:
 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
 
Code:
	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.
 
Code:
	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.

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
Code:
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 :)
 
Last edited:
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.
 
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.

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.

:respect:

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.
 
Last edited:
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.

Code:
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
 
Last edited:
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.
 
Back
Top