Question Hiding and showing a window

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
I was just googleing this, and i came across this code:

VB.NET:
Dim p As New System.Diagnostics.Process
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = True
p.StartInfo.FileName = "cmd.exe"
p.Start()

Does that mean that i could show it again? if so, is there a code that can do this?

Also, if i wanted to get the window that the user is currently in, how would i be able to hide that?

thanks
 
If you set CreateNoWindow to True then you can't show it again because there is no window to show because none was created. If a window is created but the WindowStyle is set to Hidden then you can show it later using ShowWindow API. Have a look around and I'm sure you'll find examples of its use in .NET. You'll need to get the handle of the window you want to show though. Exactly how you'd do that with a console window I'm not sure. I'm fairly certain that you can't get it from the Process so you have to use other means, presumably involving the FindWindow API.
 
From going around forum to forum, i have put together this little bit of code:

VB.NET:
Public Class HideWindow
    Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Sub HideTopmost(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HideTopmost.Click
        Dim Handle As Integer
        Dim WindowName As String
        WindowName = GetForegroundWindow
        Const SW_Hide = 0
        Handle = FindWindow(WindowName, vbNullString)
        ShowWindow(Handle, SW_Hide)
    End Sub
End Class

But its just coming up with an overflow error...
I have been programming VB for almost a year, and i have a bit of knowledge about most things in VB, but i have never gone onto this side of hiding and showing non VB app windows.

any help would be much appreciated :)
 
You've used some VB6 declarations and some VB.NET. Windows API functions almost universally use 32-bit numbers. In VB6 the Integer type was 16-bit and the Long type was 32-bit. In VB.NET the Integer type is 32-bit and the Long type is 64-bit. Make sure you ALWAYS use VB.NET declarations in VB.NET. If the code you're using is from VB6 then just make every Long an Integer. If you're not sure then check PInvoke.net, which have declarations for most functions.
 
I have changed all the longs to integers, but now it does nothing at all...

VB.NET:
Public Class HideWindow
        Private Declare Function GetForegroundWindow Lib "user32" () As Integer
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer


    Private Sub HideTopmost(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HideTopmost.Click
        Dim Handle As Integer
        Dim WindowName As String
        WindowName = GetForegroundWindow
        Const SW_Hide = 0
        Handle = FindWindow(WindowName, vbNullString)
        ShowWindow(Handle, SW_Hide)
    End Sub
End Class

Any more info?
 
I have now changed the declarations, but it still does nothing!?!

VB.NET:
Public Class Form1

    Private Shared Function GetForegroundWindow() As IntPtr
    End Function
    Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
    End Function
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    Const SW_Hide = 0
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Handle As Integer
        Dim WindowName As String
        WindowName = GetForegroundWindow
        Handle = FindWindow(WindowName, vbNullString)
        ShowWindow(Handle, SW_Hide)
        MsgBox("Hidden")
    End Sub
End Class
 
OK, now I see that you're actually trying to hide the foreground window. Take a look at this:
VB.NET:
    Private Shared Function GetForegroundWindow() As [B][U]IntPtr[/U][/B]
    End Function
Now take a look at this:
VB.NET:
        Dim WindowName As [B][U]String[/U][/B]
        WindowName = GetForegroundWindow
What does GetForegroundWindow return? Is it the name of a window? It's type IntPtr, so it's hardly going to be assigned to a String variable.
 
You didn't answer my question. What does GetForegroundWindow return? I'm not talking about the type specifically but what the value represents. If you don't know, look it up.
 
An integer. My VB application is 11732518.

I am well and truly lost at the moment. could you please give me a nudge into the right direction
 
I have now edited my code further, and i have got this, a string of the current windows name, aswell as the intptr.

VB.NET:
Public Class Form1
    'Declare all of the imports
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    Private Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
    Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
    End Function
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    'Declare all of the imports

    Dim activewindow As String
    Const SW_Hide = 0

    'Function that gets the active window title
    Private Function GetActiveWindowTitle() As String
        Dim MyStr As String
        MyStr = New String(Chr(0), 100)
        GetWindowText(GetForegroundWindow, MyStr, 100)
        MyStr = MyStr.Substring(0, InStr(MyStr, Chr(0)) - 1)
        Return MyStr
    End Function

    Private Sub Hide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hide.Click
        activewindow = GetActiveWindowTitle()
        Dim Handle As IntPtr
        Dim WindowName As IntPtr
        WindowName = GetForegroundWindow
        WindName.Text = activewindow
        Handle = FindWindow(activewindow, vbNullString)
        ShowWindow(WindowName, SW_Hide)
        MsgBox("Hidden")
    End Sub
End Class

I hope you could understand that code. its a little messy.
 
An integer. My VB application is 11732518.

I am well and truly lost at the moment. could you please give me a nudge into the right direction
I specifically said I was asking what the value represents. Have you read the documentation for the GetForegroundWindow function? If not, why not?

getforegroundwindow - MSDN Search

You don't even have to read the actual documentation because the very first result in the list tells you exactly what I'm asking for. Now that you know what the value returned by GetForegroundWindow means, think about what it is that you're supposed to pass to ShowWindow.

showwindow - MSDN Search

Once you know what comes out of GetForegroundWindow and what goes into ShowWindow, ask yourself how they might be related. In fact, ask yourself if they might the very same thing.

In future, if you're using data types, members, functions or whatever in your code, don't do so blindly. Look them all up and read about them so that you get as much understanding as you can about their purpose.
 
Thanks for your help. Ive finally got the code to work:

VB.NET:
 Private Declare Function GetForegroundWindow Lib "user32" () As Int32
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Boolean
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Public Declare Function GetDesktopWindow Lib "user32" () As Int32
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    Private Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Int32
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long) As Int32
 
    Public Function GetTaskbarWindow() As Long
        GetTaskbarWindow = FindWindow("shell_traywnd", "")
    End Function

Const SW_Hide = 0
    Const SW_ShowNormal = 1
    Const HWND_TOPMOST = -1
    Const SWP_SHOWWINDOW = &H40

    Dim HandleToHide(200) As IntPtr
    Dim i = 0

    Dim ctrl As Boolean
    Dim alt As Boolean
    Dim h As Boolean
    Dim s As Boolean
    Dim m As Boolean

    Private Sub HotKeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetHotKeyPress.Tick
        ctrl = GetAsyncKeyState(Keys.ControlKey)
        alt = GetAsyncKeyState(Keys.Menu)
        h = GetAsyncKeyState(Keys.H)
        s = GetAsyncKeyState(Keys.S)
        m = GetAsyncKeyState(Keys.M)

        If ctrl And alt And h = True Then
            If i = 100 Then
                Exit Sub
            ElseIf i <= 99 Then

                If GetForegroundWindow = GetTaskbarWindow() Then
                    Exit Sub
                End If
                i = i + 1
                HandleToHide(i) = GetForegroundWindow()
                ShowWindow(HandleToHide(i), SW_Hide)

                WindName.Text = HandleToHide(i)
            End If
        ElseIf ctrl And alt And s = True Then
            If i = 0 Then
                Exit Sub
            ElseIf i >= 1 Then
                BringWindowToTop(HandleToHide(i))
                ShowWindow(HandleToHide(i), SW_ShowNormal)
                BringWindowToTop(HandleToHide(i))
                WindName.Text = HandleToHide(i)

                i = i - 1
            End If
     End If

Which is working great, except sometimes the window changes location / size and i cant get it to stop hiding the desktop if the hotkeys are used then. I have stopped it hiding the taskbar, but am having toubles with the desktop.

any other modifications would also be appreciated :)
 
Back
Top