当前位置:编程学习 > C#/ASP.NET >>

重写WndProc时无法截取message的信息

我想为textbox文本框添加一个用于限制字节数的属性,于是我添加了一个组件重写了WndProc,继承的是Dxperience组件里的TextEdit,System.Windows.Forms.Message总是截取不到信息或总是相同的信息(0).
这是我用的代码:
出处:网上;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TextBoxEx
{
    public partial class TextBoxEx : TextEdit
    {
        public TextBoxEx()
        {
            InitializeComponent();
        }

        public TextBoxEx(IContainer container)
        {
            container.Add(this);
            
            InitializeComponent();
        }
        private const int WM_CHAR = 0x0087;
        private const int WM_PASTE = 0x0302;
        private string str = "";
        private int m_MaxByteLength = 0;
        [Description("获取或设置用户可在文本控件中输入或粘贴的最大字节数。0为允许无限长度")]
        public int MaxByteLength
        {
            get { return m_MaxByteLength; }
            set { m_MaxByteLength = value; }
        }
        /// <summary>
        /// 判读即将输入的文本长度是否溢出
        /// </summary>
        /// <param name="text">文本</param>
        /// <returns>是否溢出</returns>
        public bool CheckByteLengthFlow(string text)
        {
            int tlen = GetByteLength(this.Text);//文本框原有文本长度
            int len = GetByteLength(text);//输入的字符的长度
            int slen = GetByteLength(this.SelectedText);//文本框选中文本的长度
            return (m_MaxByteLength < (tlen - slen + len));
        }
        
        /// <summary>
        /// 计算文本字节长度,区分多字节字符
        /// </summary>
        /// <param name="text">文本</param>
        /// <returns>文本字节长度</returns>
        public int GetByteLength(string text)
        {
            return System.Text.Encoding.Default.GetBytes(text).Length;
        }
        protected override void WndProc(ref Message m)
        {
            DevExpress.XtraEditors.XtraMessageBox ss;
            DevExpress.XtraEditors.XtraMessageBoxArgs sss;
           string s3= sss.Text.ToString();
           DevExpress.XtraEditors.XtraMessageBoxForm ssss;
            string s4= ssss.Handle.ToString();
            DevExpress.XtraEditors.XtraForm s5;
            
            //如果该属性没有设置,则允许输入 
            if (m_MaxByteLength == 0)
            {
                base.WndProc(ref m);
                return;
            }
            switch (m.Msg)
            {
                case WM_CHAR:
                    //int dd=(int)mm.WParam;
                    int i = (int)m.WParam;
                    bool isBack = (i == (int)System.Windows.Forms.Keys.Back);
                    bool isDelete = (i == (int)System.Windows.Forms.Keys.Delete);//实际上这是一个"."键 
                    bool isCtr = (i == 24) || (i == 22) || (i == 26) || (i == 8);
                    if (isBack || isDelete || isCtr)
                    {
                        //控制键不作处理 
                    }
                    else
                    {
                        char c = (char)i;
                            if (CheckByteLengthFlow(str))
                            {
                                break;
                            }
                    }
                    base.WndProc(ref m);
                    break;
                case WM_PASTE:
                    System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();//取剪贴板对象 
                    if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Text)) //判断是否是Text 
                    {
                        string text = (string)iData.GetData(System.Windows.Forms.DataFormats.Text);//取数据 
                        if (CheckByteLengthFlow(text))
                        {
                            m.Result = (IntPtr)0;//不可以粘贴 
                            break;
                        }
                    }
                    base.WndProc(ref m);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
      }
    } --------------------编程问答-------------------- protected override void WndProc(ref Message m)
        {
//LOG, 打出来看看,就这么扔段代码,能看出来的没几个
            DevExpress.XtraEditors.XtraMessageBox ss;  --------------------编程问答-------------------- http://www.cnblogs.com/lemony/archive/2007/04/10/707474.html --------------------编程问答-------------------- wuyq11您好,那个是继承的System.Windows.Forms.TextBox  但是我现在继承的是DevExpress.XtraEditors.TextEdit,这样子就不行了 ,不知道为什么?还望您给解答一下小弟感恩不尽! --------------------编程问答-------------------- TextEdit有个MaskBox,是不是应该截取他的消息处理? --------------------编程问答-------------------- winform中有这个限制长度的属性
补充:.NET技术 ,  组件/控件开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,