Writing event handler for form minimize and restore RRS feed

  • Question

  • Using Visual Basic in VS 2017; I am looking for a way to handle the event of a form being minimized, and also of being restored (this particular from cannot be maximized); but under Form I don't see any property, method, or event to help with that. I assume it is possible, but how?

    Thursday, November 7, 2019 4:51 PM

All replies

  • A way :

    Public Const WM_SYSCOMMAND As Integer = &H112
    Public Const SC_MINIMIZE As Integer = &HF020
    Public Const SC_RESTORE As Integer = &HF120
    Protected Overrides Sub WndProc(ByRef m As Message)
        If (m.Msg = WM_SYSCOMMAND AndAlso CInt(m.WParam) = SC_RESTORE) Then
            Console.Beep(2000, 10)
        ElseIf (m.Msg = WM_SYSCOMMAND AndAlso CInt(m.WParam) = SC_MINIMIZE) Then
            Console.Beep(5000, 10)           
        End If
    End Sub

    Thursday, November 7, 2019 5:25 PM
  • In order to receive the notifications, try adding this code to your form:


    Protected Overrides Sub WndProc(ByRef m As Message)




        Const WM_SIZE = &H5


        Const SIZE_RESTORED = 0

        Const SIZE_MINIMIZED = 1


        Select Case m.Msg

            Case WM_SIZE

                Select Case m.WParam

                    Case SIZE_MINIMIZED

                        MsgBox("Window Minimized")

                        ' . . .

                    Case SIZE_RESTORED

                        MsgBox("Window Restored")

                        ' . . .

                End Select

        End Select


    End Sub


    To ignore the first “Restored” notification, set a Boolean flag inside the Load event.

    Thursday, November 7, 2019 6:30 PM
  • Hi ronks,

    I suppose you can use Form.SizeChanged event like this.
    Public Class Form1
    	' --- for ignoring event when Form is loading
    	Private IgnoreEvent As Boolean = True
    	' --- Event: Form.SizeChanged
    	Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
    		If (Me.IgnoreEvent) Then
    			Me.IgnoreEvent = False
    			Exit Sub
    		End If
    		' ---
    		Select Case Me.WindowState
    			Case = WindowState.Minimized
    				MsgBox("Form is Minimized")
    			Case = WindowState.Normal
    				MsgBox("Form is Normal(restored)")
    			Case = WindowState.Maximized
    				Me.WindowState = FormWindowState.Normal
    		End Select
    	End Sub
    End Class
    If [MaximizeBox = False] with your form, the 3rd Case statement (Case = WindowState.Maximized) is NOT necessary.


    Ashidacchi --

    • Marked as answer by ronks Friday, November 8, 2019 3:47 AM
    • Unmarked as answer by ronks Friday, November 8, 2019 4:48 AM
    Friday, November 8, 2019 12:06 AM
  •     Private Sub Form1_ClientSizeChanged(sender As Object, e As EventArgs) Handles Me.ClientSizeChanged
        End Sub

    Friday, November 8, 2019 12:26 AM


    Friday, November 8, 2019 12:42 AM
  • Thanks for all the suggestions; I think they would all work.

    I like Ashidacchi's suggestion best; I have modified it to handle the Me.Resize event.

    Friday, November 8, 2019 3:51 AM
  • Oops; I spoke too soon.

    Unfortunately, Resize and some other events I looked at occur *after* the minimize or restore. What I want to do is alter the color of a control *before* the program's thumbnail is sent to the taskbar.

    Friday, November 8, 2019 4:49 AM
  •     Unfortunately, Resize and some other events I looked at occur *after* the minimize or restore. What I want to do is alter the color of a control *before* the program's thumbnail is sent to the taskbar. [Right click to shift]

    use WM_SYSCOMMAND method
    Friday, November 8, 2019 7:55 AM
  • Hi ronks,

    Can you explain the meaging of "What I want to do is alter the color of a control *before* the program's thumbnail is sent to the taskbar."

    What do you mean by "the color of control"?
    Is it [Minimized] button, or Form itself?


    Ashidacchi --

    Friday, November 8, 2019 8:03 AM
  • I should back up and explain. I have a very simple VB program. Eventually I may build on it, but for now it simply consists of a label with text in it that shows the time of day. The title bar also shows the TOD, and a timer updates the display once a second.
    When a running program is minimized, the Windows taskbar displays the title bar and a thumbnail of the window at the time it was minimized.
    The contents of the title bar are updated but the thumbnail is not.
    The result is that the two values diverge, with the thumbnail frozen and the (less prominent) title bar up to date.
    I was going to solve that crudely by setting the color of the text to be the same as the color of the label. The foreground (text) would be invisible and only the title bar would seem display a value. (Of course the proper colors would be restored when the user clicked on the taskbar icon.)
    But since the thumbnail is captured before the event handler runs, changes made to the colors have no effect. That seems to be the case whether I use the Resize event or override WndProc.
    I suspect my approach is a blind alley, and I will need to look instead at something like taskBarItemInfo.Overlay.
    Friday, November 8, 2019 11:33 PM
  • Hi ronks, (Good morning from Japan)

    Thank you for your detailed explanation.

    But I can hardly distinguish "the title bar" and "the thumbnail" on the Taskbar. I am poor in English.

    Could you insert a screenshot in your post or share it via cloud storage such as OneDrive, Dropbox, etc? That will help me to understand your issue.


    Ashidacchi --

    Saturday, November 9, 2019 12:38 AM
  • Hi ronks,

    I'd like to confirm about your description.
    Do you mean "NotifyIcon" by "thumbnail"?

    I've made a sample and run and minimized it.
    In the Taskbar, title and NotifyIcon is shown like the above.
    I suppose you mean a clock image with date&time is "title" and a clock image at the right most is "thumbnail".  Is this correct?


    Ashidacchi --

    Saturday, November 9, 2019 2:48 AM
  • These two screen snips should help explain the issue.

    shows a portion of the Windows 10 taskbar along the bottom of the screen, and above it a downsized image of the Firefox browser which is pointed at a Google map.

    shows the taskbar and a small image of my clock program. Within it, the title bar at the top reads 6:44:34. That is correct

    But the much larger image below it was frozen at the time the window was minimized at 6:43:14. It will not change, and it can be misleading to the user.

    I would like to update that larger image when the timer ticks.

    But if that is not possible, I would like to hide the frozen numbers in larger image by setting the foreground and background colors to be the same.

    Saturday, November 9, 2019 3:10 AM
  • Hi,

    You need to provide more information.
    Which Control the window is, that appears near the notify icon?

    If you create another Form and make it show when the notify icon is clicked.


    Ashidacchi --

    Saturday, November 9, 2019 3:36 AM
  • Hi,

    I guess maybe this is not related to Visual Stdio. I tested other software. When I minimized the software to the taskbar, if I just moved on the taskbar without clicking, except the title, the current page status will not change. But when you don't minimize the software, if you move it on the taskbar without clicking it, it will normally display the current state.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Me.WindowState = FormWindowState.Normal Or Me.WindowState = FormWindowState.Maximized Then
                Label1.BackColor = Label1.ForeColor
                Me.WindowState = FormWindowState.Minimized
            End If
        End Sub

    The strange thing is that according to the above code, the desired result cannot be completed before the minimization.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, November 15, 2019 10:02 AM
  • Thanks!  I have been drawn away by other work, but will get back to this as soon as it lets up.
    Tuesday, November 19, 2019 3:47 AM