c#winform-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);
        }