为窗体添加 "最大化","最小化","还原"等 事件

                 为窗体添加 "最大化","最小化","还原"等 事件
                                                                             电子科技大学软件学院03级02班 周银辉

     在.Net3.0以前的版本中(Form类)都没有与窗口最大化、最小化等相关的事件, 这让人很郁闷. ( .Net3.0的Window类中添加了该事件"StateChanged "). 这里来重写Form类以便添加这几个事件.

1, 参数 WindowStateChangedEventArgs
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 包含窗口状态变化时的相关数据
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  WindowStateChangedEventArgs : EventArgs
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private readonly FormWindowState oldState;
InBlock.gif        
private readonly FormWindowState newState;
InBlock.gif
InBlock.gif        
public FormWindowState OldState
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return oldState;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public FormWindowState NewState
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return newState;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public WindowStateChangedEventArgs(FormWindowState oldState, FormWindowState newState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.oldState = oldState;
InBlock.gif            
this.newState = newState;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif    }

2, 继承Form类并添加事件WindowStateChanged
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Data;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  MDIHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class CustomForm : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//记录状态变化之前的状态
InBlock.gif
        private FormWindowState preWindowState = FormWindowState.Normal;
InBlock.gif
InBlock.gif        
public CustomForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
事件#region 事件
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 窗口状态改变时发生
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event EventHandler<WindowStateChangedEventArgs> WindowStateChanged;
InBlock.gif
InBlock.gif        
protected virtual void OnWindowStateChanged(WindowStateChangedEventArgs arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.WindowStateChanged != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.WindowStateChanged(this, arg);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重写的方法#region 重写的方法       
InBlock.gif
InBlock.gif        
protected override void WndProc(ref Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (m.Msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case 0x0005://change size: WM_SIZE
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        FormWindowState newState 
= FormWindowState.Normal;
InBlock.gif                        
switch (m.WParam.ToInt32())
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
case 0://SIZE_RESTORED
InBlock.gif
                                newState = FormWindowState.Normal;
InBlock.gif                                
break;
InBlock.gif                            
case 1://SIZE_MINIMIZED
InBlock.gif
                                newState = FormWindowState.Minimized;
InBlock.gif                                
break;
InBlock.gif                            
case 2://SIZE_MAXIMIZED
InBlock.gif
                                newState = FormWindowState.Maximized;
InBlock.gif                                
break;
InBlock.gif                            
default:
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
if (newState != this.preWindowState)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
this.OnWindowStateChanged(new WindowStateChangedEventArgs(this.preWindowState, newState));
InBlock.gif                            
this.preWindowState = newState;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.WndProc(ref m);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

其中最重要的部分是 protected override void WndProc(ref Message m) , 它捕获了发给窗体的消息, 关于消息的常量值可以在 winuser.h中找到,关于消息的具体含义可以在WindowsSDK中找到.
更多的, 你可以利用protected override void WndProc(ref Message m) 创建更多事件.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值