textbox在c#中怎麼添加背景圖片

來源:互聯網
上載者:User

     最近考慮做個動感日記,其中一個需求就是像163郵箱裡面,可以設定背景信紙。因此考慮寫一個設定背景圖片的控制項,

網上找到一個方法。可以借鑒一下,所以貼出來了。

http://topic.csdn.net/u/20080102/11/577b2d8d-4b75-473e-b533-55d4e8881d9c.html

 

===========================從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);
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.