c#中怎样给textbox添加背景图片

=======================从textbox类继承处理绘制背景消息===================
    class mytextbox : textbox
    {
        const int wm_erasebkgnd = 0x0014;
 
        private image backimage;
 
        [displayname("背景图片。")]
        public image backimage
        {
            get return backimage; }
            set { backimage = value; }
        }
 
        protected void onerasebkgnd(graphics gs)
        {
            gs.fillrectangle(brushes.white, 0, 0, this.width, this.height); //填充为白色,防止图片太小出现重影
            if (backimage != null) gs.drawimage(backimage, 0, 0); //绘制背景。
            gs.dispose();
        }
 
        protected override void wndproc(ref message m)
        {
            if (m.msg == wm_erasebkgnd) //绘制背景
            {
                onerasebkgnd(graphics.fromhdc(m.wparam));
                m.result = (intptr)1;
            }
            base.wndproc(ref m);
        }
    }
 
==================窗口类里设置控件的字体颜色等============================
 
        const int wm_ctlcoloredit = 0x0133;
        const int transparent = 0x1;
        const int null_brush = 0x5;
 
        [dllimport("gdi32")]
        static extern int setbkmode(intptr hdc, int bkmode);
        [dllimport("gdi32")]
        static extern int settextcolor(intptr hdc, int color);
        [dllimport("gdi32")]
        static extern intptr getstockobject(int fnobject);
 
        protected override void wndproc(ref message m)
        {
            if (m.msg == wm_ctlcoloredit && m.lparam == mytextbox1.handle)//类型为edit(textbox)
            {
                setbkmode(m.wparam, transparent);//设置背景透明
                settextcolor(m.wparam,0xff);     //字体颜色为红色
                m.result = getstockobject(null_brush);
                return;
            }
            else base.wndproc(ref m);
       }
posted @ 2015-08-30 14:49  撷一程  阅读(2445)  评论(0编辑  收藏  举报