0

I would like to minimize all applications except my own, but with the code below, my ow application does not show again.

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_LWIN = &H5B


Sub minimizeAllOthers()
Me.WindowState=WindowState.Minimized
keybd_event(VK_LWIN, 0, 0, 0)
keybd_event(77, 0, 0, 0)
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)

While Me.WindowState=WindowState.Minimized
Me.WindowState=WindowState.Normal
Me.windowstate=WindowState.Maximized
Threading.Thread.Sleep(400)
End Sub
geek1011
  • 495
  • 9
  • 19
  • Usually, the best approach in this kind of situations is Me.TopMost = true. The user would see the active window of your application independently upon what the other running applications are doing. – varocarbas Aug 24 '15 at 12:55
  • @varocarbas I am starting other windows from my app – geek1011 Aug 24 '15 at 14:35
  • Not sure if I understood your point, but Me.TopMost is much more "powerful" than Me.WindowState. If you use Me.TopMost, the active window of your application will certainly be on top of any other window. – varocarbas Aug 24 '15 at 14:48
  • @varocarbas `Me.TopMost` and `Me.WindowState` are completely differents ! – Drarig29 Aug 24 '15 at 14:50
  • @geek1011 See [my answer](http://stackoverflow.com/questions/32182546/minimize-all-other-applications-except-my-application#32184593) ! – Drarig29 Aug 24 '15 at 14:51
  • @Drarig29 Logically. But what the user is looking for (i.e., making the window of his application on top of all the other ones) cannot be always accomplished via WindowState. On the other hand, with Me.TopMost you are sure that the window of your application will be on top of the remaining ones. – varocarbas Aug 24 '15 at 14:54
  • @Drarig29 Your code does not address the OP problems (which are not completely clear, on the other hand). Note that your application might be maximised but not shown to the user (because the windows of other applications are on top of it). Your code only makes sure that the given application is always maximised (what is not what the OP wants), without caring about the remaining applications. – varocarbas Aug 24 '15 at 14:59
  • @varocarbas From what I understood, he doesn't want to bring his software to top (`TopMost`), he wants to minimize all the softwares except his own. So he uses the `LWin+M` shortcut (with `keybd_event`) and he would like to show his software. This is exactly what is done with [my answer](http://stackoverflow.com/questions/32182546/minimize-all-other-applications-except-my-application#32184593)... – Drarig29 Aug 24 '15 at 15:00
  • @Drarig29 As said from the start, the best approach to what the OP wants is usually TopMost (= not caring about the remaining windows, but making sure that your application is on top anyway). If he prefers to actually make sure that all the remaining windows are minimised, he would have have to go though all the windows of all the running applications (not what his code does) and force them to minimise; what is certainly not recommendable. In any case, your code avoids the OP's application to be minimised at all what does not seem to be what he is looking for. – varocarbas Aug 24 '15 at 15:03
  • @varocarbas I think you are wrong, but we can't be sure of that given that geek1011 is not here... You should look at his question, his code, and his single comment... "I would like to **minimize all applications except my own**". And his problem was that his code was showing his form during the animation. That's why I've added code to react after the form being minimized. – Drarig29 Aug 24 '15 at 15:05
  • @Drarig29 I read the question, understood the intention of the OP and proposed the most adequate solution for this situation (i.e., minimising all the other windows is not the most adequate solution, as far as you can accomplish the same goal by just making sure that yours is on top). In any case and as explained in my previous comment, your code provokes a likely-undesirable-for-the-OP side effect: it forces the application to always be maximised (= the user cannot minimise it under any circumstance). – varocarbas Aug 24 '15 at 15:25
  • @varocarbas Oh ! I see what you mean. I've edited this ;) By the way, what means "the OP" ? – Drarig29 Aug 24 '15 at 15:27
  • 1
    @Drarig29 the OP means the Original Poster. It is an acronym which is used in most of forums to refer to the person writing the initial post (or asking the question here). I have seen your modification, but am not sure that it delivers what is expected. Let's better wait for the OP's feedback. – varocarbas Aug 24 '15 at 15:36
  • Note also that simulating a keypress will cause problems if (1) the user disabled the Win+M hotkey, or (2) the user happens to be holding the shift key at the time you inject the input. Instead, minimize the apps yourself rather than trying to trick Explorer into doing it (because hotkeys are not part of the API). – Raymond Chen Aug 24 '15 at 17:35
  • @varocarbas I am trying to minimize all applications but my own because my app is supposed to stay behind all other windows, but I do not want anything covering it because it needs to launch other applications that should appear above it. – geek1011 Aug 25 '15 at 12:44
  • @Drarig29 See the comment above this one – geek1011 Aug 25 '15 at 12:44
  • Reaching such a goal is not easy and I recommend you to adapt your situation to the much more practical TopMost (setting it to true and false, depending upon when you want your application to be on top). If you want to have full control on all the other applications, you would have to do some research about Windows hooks, write a much better code (yours is still very far away from having a true control on all the running processes) and come back here in case of having any clearly-defined problem. – varocarbas Aug 25 '15 at 13:00
  • @varocarbas I really need another app to show in front of mine, there is no other way – geek1011 Aug 25 '15 at 13:11
  • @varocarbas I asked a question about showing a exe as a child window, but that does not workfor me anymore,because i need my app to still be clickable. – geek1011 Aug 25 '15 at 13:12
  • You shouldn't expect n running processes (each of them with m active windows) to behave exactly as you want with a so simplistic code. As said: do some research (about how to find out all the running processes, their active windows and to interact with them), as far as this is way too complex to be asked here (with a so simple code). On the other hand, if there is just one another relevant application (and you have access to its code), you might do things much easier by setting up some kind of communication between them (e.g., via temporary files) and using TopMost alternatively. Otherwise... – varocarbas Aug 25 '15 at 13:18
  • @varocarbas The other apps minimize fine, i just want my own app not to be minimized – geek1011 Aug 25 '15 at 13:32
  • I meant that if you are not considering TopMost only because eventually having to use another app on which you have also control; you might use TopMost in both of them!! In this way you would be able to always show the window you want (by setting up an communication between both applications, as proposed above). – varocarbas Aug 25 '15 at 13:41
  • There are only two possible approaches here (further talking about it will not change the reality): either rely on TopMost and don't care about the other running processes; or take care of all the running processes (by improving your code a lot). Just focusing on minimising/maximising your application will not solve your problem as far as the remaining running ones might be on top, hiding it. I guess that my contribution ends here (nothing else to say). – varocarbas Aug 25 '15 at 13:45

2 Answers2

1

To detect when a form has been minimized, here's a good solution (read the "To react after" part of the solution).

So here's a working code :

Dim Minimizing As Boolean = False
Dim Normalizing As Boolean = False

Sub minimizeAllOthers()
    Minimizing = True
    keybd_event(VK_LWIN, 0, 0, 0)
    keybd_event(77, 0, 0, 0)
    keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    minimizeAllOthers()
End Sub

Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
    If Minimizing AndAlso Me.WindowState = FormWindowState.Minimized Then
        Me.WindowState = FormWindowState.Normal
        If Normalizing Then
            Minimizing = False
        End If
        If Normalizing Then Normalizing = False Else Normalizing = True
    End If
End Sub

I hope it'll help and that I've correctly understood your problem...

Community
  • 1
  • 1
Drarig29
  • 1,902
  • 1
  • 19
  • 42
  • What `Minimized` means for you ? For me, [that's](https://dl.dropboxusercontent.com/s/pmlmeuyxedpvb2z/Capture%20d%27%C3%A9cran%202015-08-25%2014.48.44.png) a minimized software. I say that because it works perfect with me. – Drarig29 Aug 25 '15 at 13:07
  • It means what happens when you press the "-" button. I want my app to still show, but that does not happen, it just minimizes everything, then my app flashes on and off the screen, and ends up minimized too. – geek1011 Aug 25 '15 at 13:10
  • @geek1011 Can you add all your code in your question ? (With then `keybd_event` sub, the `Const VK_LWIN` and the `Const KEYEVENTF_KEYUP`) – Drarig29 Aug 25 '15 at 13:28
  • Where were you declaring the `Minimizing` and `Normalizing` booleans ? Retry with my edited code. – Drarig29 Aug 25 '15 at 13:45
  • It still does not work, i tried your code. Hope i'm not giving you too much trouble. – geek1011 Aug 25 '15 at 13:54
0

you can try this ^_^

Imports System.Runtime.InteropServices

Module ManipulateWindows

    Const SW_HIDE As Integer = 0
    Const SW_RESTORE As Integer = 1
    Const SW_MINIMIZE As Integer = 2
    Const SW_MAXIMIZE As Integer = 3

    <DllImport("User32")> _
    Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    End Function

    Public Sub Main()

        'iterate through all the open processes.
        For Each p As Process In Process.GetProcesses    

            'Get the Window Handle
            Dim hWnd as integer = CType(p.MainWindowHandle, Integer)

            'Write out the title of the main window for the process.
            System.Console.WriteLine(p.MainWindowTitle)

            'Minimize the Window
            ShowWindow(hWnd, SW_MINIMIZE)
        Next p    
    End Sub    
End Module
Adam
  • 147
  • 1
  • 13