Add events such as "maximize", "minimize", and "Restore" to the form.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.