שתף באמצעות


Writing event handler for form minimize and restore

Question

Thursday, November 7, 2019 4:51 PM

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?

All replies (16)

Thursday, November 7, 2019 5:25 PM

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
    MyBase.WndProc(m)
End Sub

Thursday, November 7, 2019 6:30 PM

In order to receive the notifications, try adding this code to your form:

 

Protected Overrides Sub WndProc(ByRef m As Message)

 

    MyBase.WndProc(m)

 

    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.


Friday, November 8, 2019 12:06 AM | 1 vote

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.

Regards,

Ashidacchi -- http://hokusosha.com


Friday, November 8, 2019 12:26 AM

    Private Sub Form1_ClientSizeChanged(sender As Object, e As EventArgs) Handles Me.ClientSizeChanged

        Console.WriteLine(Me.WindowState)

    End Sub

https://awwshop.wikidot.com


Friday, November 8, 2019 12:42 AM

https://awwshop.wikidot.com


Friday, November 8, 2019 3:51 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 4:49 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 7:55 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 8:03 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?

Regards,

Ashidacchi -- http://hokusosha.com


Friday, November 8, 2019 11:33 PM

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.


Saturday, November 9, 2019 12:38 AM

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.

Regards,

Ashidacchi -- http://hokusosha.com


Saturday, November 9, 2019 2:48 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?

Regards,

Ashidacchi -- http://hokusosha.com


Saturday, November 9, 2019 3:10 AM

These two screen snips should help explain the issue.

https://people.well.com/user/ronks/pix/miscpix/1597TaskbarFirefoxW.jpg

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.

https://people.well.com/user/ronks/pix/miscpix/1596TaskbarRClockW.jpg

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

Regards,

Ashidacchi -- http://hokusosha.com


Friday, November 15, 2019 10:02 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.Invalidate()
            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,

Julie

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 MSDNFSF@microsoft.com.


Tuesday, November 19, 2019 3:47 AM

Thanks!  I have been drawn away by other work, but will get back to this as soon as it lets up.