Add events such as "maximize", "minimize", and "Restore" to the form.
Zhou yinhui, class 03, class 02, software College, University of Electronic Science and Technology
In. in versions earlier than net3.0 (Form class), there were no events related to window maximization, minimization, and so on. This was depressing. (. the event "statechanged" is added to the window class of net3.0 "). here we will rewrite the Form class to add these events.
1. Parameter windowstatechangedeventargs/**/ /// <Summary>
///Contains related data when the window status changes
/// </Summary>
Public Class Windowstatechangedeventargs: eventargs
{
Private Readonly Formwindowstate oldstate;
Private Readonly Formwindowstate newstate;
Public Formwindowstate oldstate
{< br> Get
{< br> return oldstate;
}
}
Public Formwindowstate newstate
{< br> Get
{< br> return newstate;
}
}
Public Windowstatechangedeventargs (formwindowstate oldstate, formwindowstate newstate)
{
This. Oldstate=Oldstate;
This. Newstate=Newstate;
}
}
2. inherit the Form class and add the event windowstatechanged Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. drawing;
Using System. Data;
Using System. text;
Using System. Windows. forms;
Namespace Mdihelper
{
Public Partial Class Customform: Form
{
// Status before status change
Private Formwindowstate prewindowstate = Formwindowstate. normal;
Public Customform ()
{
Initializecomponent ();
}
Event # Region Event
/**/ /// <Summary>
///Window status changed
/// </Summary>
Public Event Eventhandler < Windowstatechangedeventargs > Windowstatechanged;
Protected Virtual Void Onwindowstatechanged (windowstatechangedeventargs Arg)
{
If ( This . Windowstatechanged ! = Null )
{
This. Windowstatechanged (This, ARG );
}
}
# Endregion
Override Method # Region Override Method
Protected Override Void Wndproc ( Ref Message m)
{
Switch (M. msg)
{
Case Zero X 0005 : // Change size: wm_size
{
Formwindowstate newstate = Formwindowstate. normal;
Switch (M. wparam. toint32 ())
{
Case 0 : // Size_restored
Newstate = Formwindowstate. normal;
Break ;
Case 1 : // Size_minimized
Newstate = Formwindowstate. minimized;
Break ;
Case 2 : // Size_maximized
Newstate = Formwindowstate. maximized;
Break ;
Default :
Break ;
}
If (Newstate ! = This . Prewindowstate)
{
This . Onwindowstatechanged ( New Windowstatechangedeventargs ( This . Prewindowstate, newstate ));
This . Prewindowstate = Newstate;
}
}
Break ;
Default :
Break ;
}
Base . Wndproc ( Ref M );
}
# Endregion
}
}
The most important part is protected override void wndproc (ref message m), which captures messages sent to the form. The constant values of messages can be found in winuser. h. The specific meaning of the message can be found in windowssdk.
More, you can use protected override void wndproc (ref message m) to create more events.