C# Winform NativeWindow实现窗体、控件子类化,消息拦截,处理

 代码

using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    /// <summary>
    /// 消息事件委托
    /// </summary>
    /// <param name="m">消息</param>
    /// <returns>是否已被处理,是否过滤掉消息</returns>
    public delegate Boolean OnMessageEventHandle(Message m);

    /// <summary>
    /// Window Control WbdProc Hook
    /// </summary>
    public class WndProcHook : NativeWindow,IDisposable
    {
        /// <summary>
        /// 根据一个控件 创建一个Hook对象
        /// </summary>
        /// <param name="control"></param>
        public WndProcHook(Control control)
        {
            if (control.IsDisposed)
            {
                throw new Exception("窗口对象已被销毁。");
            }
            if (control.IsHandleCreated)
            {
                base.AssignHandle(control.Handle);
            }
            else
            {
                control.HandleCreated += Control_HandleCreated;
            }
            control.HandleDestroyed += Control_HandleDestroyed;
        }

        private void Control_HandleDestroyed(object sender, EventArgs e)
        {
            base.ReleaseHandle();
        }

        private void Control_HandleCreated(object sender, EventArgs e)
        {
            base.AssignHandle(((Control)sender).Handle);
        }

        /// <summary>
        /// 消息处理事件
        /// </summary>
        public event OnMessageEventHandle OnMessage;

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            Boolean IsHandle = false;
            if (OnMessage != null)
            {
                IsHandle = OnMessage.Invoke(m);
            }
            if (!IsHandle)
            {
                base.WndProc(ref m);
            }
        }

        /// <summary>
        /// 对象销毁
        /// </summary>
        public void Dispose()
        {
            this.ReleaseHandle();
        }
    }



}

举个栗子


namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private WndProcHook hookWndProc  { get; set; }

        private void Form2_Load(object sender, EventArgs e)
        {
            hookWndProc = new WndProcHook(this);
            hookWndProc.OnMessage += HookWndProc_OnMessage;
        }

        private bool HookWndProc_OnMessage(Message m)
        {
            return false;    
        }


        private void button1_Click(object sender, EventArgs e)
        {
            hookWndProc.Dispose();
        }
    }
}

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值