How to catch a form minimize event?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I want to minimize my program to the tray when minimized. But there's no
"minimize" event. So how can I catch this event and use it?
 
Hi Terry!

Terry said:
I want to minimize my program to the tray when minimized. But
there's no "minimize" event. So how can I catch this event and use
it?

You have to override WndProc:

Const WM_SYSCOMMAND As Int32 = &H112
Const SC_MINIMIZE As Int32 = &HF020
Const SC_RESTORE As Int32 = &HF120

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND Then
If m.WParam.ToInt32 = SC_MINIMIZE Then
'User clicked "minimize"
Debug.WriteLine("Minimizing...")
ElseIf m.WParam.ToInt32 = SC_RESTORE Then
'Restoring
Debug.WriteLine("Restoring...")
End If
End If
MyBase.WndProc(m)
End Sub

Cheers

Arne Janning
 
Hi Terry. Use can use Resize event. Example:

Dim f As Form
f = sender

'Check if the form is minimized
If f.WindowState = FormWindowState.Minimized Then
' the form has been minimized, insert your code here
End If

Regards, Thi.
 
Terry,
In addition to the other comments.

I normally simply handle the Resize event.

Something like:

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
Select Case Me.WindowState
Case FormWindowState.Normal
MessageBox.Show("Form was restored",
Application.ProductName)
Case FormWindowState.Minimized
MessageBox.Show("Form was minimized",
Application.ProductName)
Case FormWindowState.Maximized
MessageBox.Show("Form was maximized",
Application.ProductName)
End Select
End Sub



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I want to minimize my program to the tray when minimized. But there's no
| "minimize" event. So how can I catch this event and use it?
|
|
 
Hi Jay!
I normally simply handle the Resize event.

Something like:

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
Select Case Me.WindowState
Case FormWindowState.Normal
MessageBox.Show("Form was restored",
Application.ProductName)

What happens if the user really resizes the form?

Cheers

Arne Janning
 
Arne Janning said:
What happens if the user really resizes the form?

A messagebox is shown and resizing stops... In .NET 2.0 forms have
additional 'ResizeBegin' and 'ResizeEnd' events.
 
Hi Herfried!
A messagebox is shown and resizing stops...
Agreed.

In .NET 2.0 forms have
additional 'ResizeBegin' and 'ResizeEnd' events.

So now you can decide whether you want to show that "Restoring"-MessageBox
at the beginning or the end of resizing ;-)

SCNR

Cheers

Arne Janning
 
Arne,
(I thought I sent this last night, if this is a double post)

The window state will continue to be Normal.

If you prefer:

| > Case FormWindowState.Normal
| > MessageBox.Show("Form was restored or resized",
| > Application.ProductName)

;-)



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi Jay!
|
| Jay B. Harlow [MVP - Outlook] wrote:
| > I normally simply handle the Resize event.
| >
| > Something like:
| >
| > Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
| > MyBase.OnResize(e)
| > Select Case Me.WindowState
| > Case FormWindowState.Normal
| > MessageBox.Show("Form was restored",
| > Application.ProductName)
|
| What happens if the user really resizes the form?
|
| Cheers
|
| Arne Janning
|
|
 
Herfried,
I would have expected Resizing & Resized events, so as to comply with the
"Framework Design Guidelines", although ResizeBegin & ResizeEnd sound
slightly better...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| >> I normally simply handle the Resize event.
| >>
| >> Something like:
| >>
| >> Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
| >> MyBase.OnResize(e)
| >> Select Case Me.WindowState
| >> Case FormWindowState.Normal
| >> MessageBox.Show("Form was restored",
| >> Application.ProductName)
| >
| > What happens if the user really resizes the form?
|
| A messagebox is shown and resizing stops... In .NET 2.0 forms have
| additional 'ResizeBegin' and 'ResizeEnd' events.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 
Back
Top