Skin TextBox

public class MyText : TextBox
    {
        public const int IME_CMODE_FULLSHAPE = 0x8;
        public const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
        private Image m_img = null;
        public Image Img
        {
            get { return m_img; }
            set { m_img = value; }
        }
        bool isfrom = false;
        int height = 0;
        bool hot = false;
        public PictureBox border = null;
        /// <summary>
        /// 返回底图
        /// </summary>
        public Image GetImg
        {
            get
            {
                return m_img;
            }
        }
        /// <summary>
        /// 文本框控件
        /// </summary>
        /// <param name="parent">父控件</param>
        /// <param name="rect">范围</param>
        /// <param name="img">底图</param>
        /// <param name="istop">是否顶部</param>
        /// <param name="tab">是否回车键切换tab位置</param>
        public MyText(System.Windows.Forms.Control parent, Rectangle rect, Image img = null, bool istop = true, bool tab = true)
        {
            Font = new System.Drawing.Font(parent.Font.Name, 12);
            this.BackColor = Color.White;
            Location = new Point(rect.X, rect.Y);
            Size = new Size(rect.Width, rect.Height - 10);
            BorderStyle = BorderStyle.None;
            isfrom = istop;
            m_img = img;
            border = new PictureBox();
            border.BackgroundImage = Tool.EditImg[0];
            border.Location = new Point(rect.X - 3, rect.Y - 4);
            height = rect.Size.Height;
            border.Size = new Size(rect.Width + 6, height);
            border.Paint += OnDraw;
            parent.Controls.Add(border);
            border.BringToFront();
            if (tab)
            {
                KeyPress += (object s, KeyPressEventArgs ev) =>
                {
                    if (ev.KeyChar == '\r')
                    {
                        System.Windows.Forms.SendKeys.Send("{Tab}");
                    }
                };
            }
            GotFocus += (object s, EventArgs ev) =>
            {
                IntPtr HIme = Tool.ImmGetContext(this.Handle);
                if (Tool.ImmGetOpenStatus(HIme))  //如果输入法处于打开状态
                {
                    int iMode = 0;
                    int iSentence = 0;
                    bool bSuccess = Tool.ImmGetConversionStatus(HIme, ref iMode, ref iSentence);  //检索输入法信息
                    if (bSuccess)
                    {
                        if ((iMode & IME_CMODE_FULLSHAPE) > 0)   //如果是全角
                            Tool.ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE);  //转换成半角
                    }

                }
                border.BackgroundImage = Tool.EditImg[2];
            };
            LostFocus += (object s, EventArgs ev) =>
            {
                border.BackgroundImage = Tool.EditImg[0];
            };
            MouseEnter += (object s, EventArgs ev) =>
            {
                if(!this.Focused)
                    border.BackgroundImage = Tool.EditImg[1];
            };
            MouseLeave += (object s, EventArgs ev) =>
            {
                if (!this.Focused)
                    border.BackgroundImage = Tool.EditImg[0];
            };
        }
        void OnDraw(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (m_img != null)
            {
                if (isfrom)
                    g.DrawImage(m_img, new Rectangle(0, 0, Size.Width + 6, height), new Rectangle(Location.X, Location.Y, Size.Width + 6, height), GraphicsUnit.Pixel);
                else
                    g.DrawImage(m_img, new Rectangle(0, 0, Size.Width + 6, height), new Rectangle(Location.X + Parent.Location.X, Location.Y + Parent.Location.Y, Size.Width + 6, height), GraphicsUnit.Pixel);
            }
            else
            {
                SolidBrush b = new SolidBrush(BackColor);
                g.FillRectangle(b, 0, 0, Size.Width + 6, height);
                b.Dispose();
            }
            if (border.BackgroundImage != null)
                Tool.DrawImage(g, border.BackgroundImage, new Rectangle(0, 0, Size.Width + 6, height), new Point(0, 0));
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值