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

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

1,
参数 WindowStateChangedEventArgs

/**//// <summary>
    
/// 包含窗口状态变化时的相关数据
    
/// </summary>
    public class WindowStateChangedEventArgs : EventArgs
    
{
        
private readonly FormWindowState oldState;
        
private readonly FormWindowState newState;

        
public FormWindowState OldState
        
{
            
get
            
{
                
return oldState;
            }
        }


        
public FormWindowState NewState
        
{
            
get
            
{
                
return newState;
            }
        }

        
public WindowStateChangedEventArgs(FormWindowState oldState, FormWindowState newState)
        
{
            
this.oldState = oldState;
            
this.newState = newState;
        }


    }


2,
继承Form类并添加事件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
    
{
        
//记录状态变化之前的状态
        private FormWindowState preWindowState = FormWindowState.Normal;

        
public CustomForm()
        
{
            InitializeComponent();
        }

        
事件#region 事件

        
/**//// <summary>
        
/// 窗口状态改变时发生
        
/// </summary>
        public event EventHandler<WindowStateChangedEventArgs> WindowStateChanged;

        
protected virtual void OnWindowStateChanged(WindowStateChangedEventArgs arg)
        
{
            
if (this.WindowStateChanged != null)
            
{
                
this.WindowStateChanged(this, arg);
            }
        }

        
#endregion


        
重写的方法#region 重写的方法       

        
protected override void WndProc(ref Message m)
        
{
            
switch (m.Msg)
            
{
                
case 0x0005://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
    }
}


其中最重要的部分是 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、付费专栏及课程。

余额充值