我设置了这个窗体的背景图片(不是背景色)      上面有很多的textbox,但是里面是白白的,很难看。      怎么可以让它像lable一样(lable可设置颜色是透明,从而随着窗体背景图片而变化)      ~~~>O<~~~   新手好多问题啊~~~      大家帮我想想办法吧~~      3Q~~~

解决方案 »

  1.   

    设置Form的TransparencyKey属性,将TextBox的BackColor设置为相同的值。
    注意这个颜色应该避开窗体上不应该透明的部分的颜色。表情么?点回复框上方的“管理UBB插件”按钮,加载表情。
      

  2.   


    ◐o◑~~~我照着你说的做了,textbox变成镂空的了不是我FORM的背景图片
      

  3.   

    而且还出现了一个问题    我是做登陆页面的,窗体加载时,默认焦点在用户名那个textbox    可是我要输入密码的话,我鼠标点一下密码的textbox,因为是镂空的,所以直接切换出去了
      

  4.   

    给你个正确答案:把下面代码原样复制到你的*.CS文件中.
    TextBox类 用  Public.NewTextBox 代替
    一切就OK了
      

  5.   


    namespace Public
    {
     public class win32
     {
       
      public const int  WM_MOUSEMOVE      =              0x0200;
      public const int  WM_LBUTTONDOWN     =             0x0201;
      public const int  WM_LBUTTONUP       =             0x0202;
      public const int  WM_RBUTTONDOWN     =             0x0204;
      public const int  WM_LBUTTONDBLCLK   =             0x0203;
      public const int  WM_MOUSELEAVE      =             0x02A3;
      public const int WM_PAINT     =                   0x000F;
      public const int WM_ERASEBKGND   =                0x0014;
      public const int WM_PRINT         =               0x0317;
      public const int WM_HSCROLL       =              0x0114;
      public const int WM_VSCROLL       =              0x0115;
      public const int EM_GETSEL              = 0x00B0;
      public const int EM_LINEINDEX           = 0x00BB;
      public const int EM_LINEFROMCHAR        = 0x00C9;
      public const int EM_POSFROMCHAR         = 0x00D6;  [System.Runtime.InteropServices.DllImport("USER32.DLL", EntryPoint= "PostMessage")]
      public static extern bool PostMessage(IntPtr hwnd, uint msg,
       IntPtr wParam, IntPtr lParam);  // Put this declaration in your class   //IntPtr
      [System.Runtime.InteropServices.DllImport("USER32.DLL", EntryPoint= "SendMessage")]
      public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam,
       IntPtr lParam);
      
      [System.Runtime.InteropServices.DllImport("USER32.DLL", EntryPoint= "GetCaretBlinkTime")]
      public static extern uint GetCaretBlinkTime();
      const int WM_PRINTCLIENT = 0x0318;  const long PRF_CHECKVISIBLE=0x00000001L;
      const long PRF_NONCLIENT = 0x00000002L;
      const long PRF_CLIENT  = 0x00000004L;
      const long PRF_ERASEBKGND = 0x00000008L;
      const long PRF_CHILDREN = 0x00000010L;
      const long PRF_OWNED  = 0x00000020L;  public static bool CaptureWindow(System.Windows.Forms.Control control, ref System.Drawing.Bitmap bitmap)
      {
       //This function captures the contents of a window or control   Graphics g2 = Graphics.FromImage(bitmap);   int meint = (int)(PRF_CLIENT | PRF_ERASEBKGND); //| PRF_OWNED ); //  );
       System.IntPtr meptr = new System.IntPtr(meint);   System.IntPtr hdc = g2.GetHdc();
       win32.SendMessage(control.Handle,win32.WM_PRINT,hdc,meptr);   g2.ReleaseHdc(hdc);
       g2.Dispose();   return true;
       
      }
     }
    }
      

  6.   

    namespace Public
    {
        public class NewTextBox : System.Windows.Forms.TextBox
        {
            #region private variables
            private uPictureBox myPictureBox;
            private bool myUpToDate = false;
            private bool myCaretUpToDate = false;
            private Bitmap myBitmap;
            private Bitmap myAlphaBitmap;
            private int myFontHeight = 10;
            private System.Windows.Forms.Timer myTimer1;
            private bool myCaretState = true;
            private bool myPaintedFirstTime = false;
            private Color myBackColor = Color.White;
            private int myBackAlpha = 10;
            private System.ComponentModel.Container components = null;        #endregion // end private variables
            #region public methods and overrides
            public NewTextBox()
            {
                InitializeComponent();
                this.BackColor = myBackColor;
                this.SetStyle(ControlStyles.UserPaint, false);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                myPictureBox = new uPictureBox();
                this.Controls.Add(myPictureBox);
                myPictureBox.Dock = DockStyle.Fill;
            }
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                this.myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height);
                this.myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(this.Width,this.Height);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnKeyDown(KeyEventArgs e)
            {
                base.OnKeyDown(e);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnKeyUp(KeyEventArgs e)
            {
                base.OnKeyUp(e);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                base.OnKeyPress(e);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnMouseUp(MouseEventArgs e)
            {
                base.OnMouseUp(e);
                this.Invalidate();
            }
            protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
            {
                base.OnGiveFeedback(gfbevent);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnMouseLeave(EventArgs e)
            {
                Point ptCursor = Cursor.Position;            Form f = this.FindForm();
                ptCursor = f.PointToClient(ptCursor);
                if (!this.Bounds.Contains(ptCursor))
                    base.OnMouseLeave(e);
            }
            protected override void OnChangeUICues(UICuesEventArgs e)
            {
                base.OnChangeUICues(e);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void OnGotFocus(EventArgs e)
            {
                base.OnGotFocus(e);
                myCaretUpToDate = false;
                myUpToDate = false;
                this.Invalidate();
                myTimer1 = new System.Windows.Forms.Timer(this.components);
                myTimer1.Interval = (int)win32.GetCaretBlinkTime(); //  usually around 500;            myTimer1.Tick += new EventHandler(myTimer1_Tick);
                myTimer1.Enabled = true;
            }        protected override void OnLostFocus(EventArgs e)
            {
                base.OnLostFocus(e);
                myCaretUpToDate = false;
                myUpToDate = false;
                this.Invalidate();
                myTimer1.Dispose();
            }
            protected override void OnFontChanged(EventArgs e)
            {
                if (this.myPaintedFirstTime)
                    this.SetStyle(ControlStyles.UserPaint, false);            base.OnFontChanged(e);            if (this.myPaintedFirstTime)
                    this.SetStyle(ControlStyles.UserPaint, true);
                myFontHeight = GetFontHeight();
                myUpToDate = false;
                this.Invalidate();
            }        protected override void OnTextChanged(EventArgs e)
            {
                base.OnTextChanged(e);
                myUpToDate = false;
                this.Invalidate();
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == win32.WM_PAINT)
                {
                    myPaintedFirstTime = true;
                    if (!myUpToDate || !myCaretUpToDate)
                        GetBitmaps();
                    myUpToDate = true;
                    myCaretUpToDate = true;                if (myPictureBox.Image != null) myPictureBox.Image.Dispose();
                    myPictureBox.Image = (Image)myAlphaBitmap.Clone();
                }
                else if (m.Msg == win32.WM_HSCROLL || m.Msg == win32.WM_VSCROLL)
                {
                    myUpToDate = false;
                    this.Invalidate();
                }            else if (m.Msg == win32.WM_LBUTTONDOWN
                 || m.Msg == win32.WM_RBUTTONDOWN
                 || m.Msg == win32.WM_LBUTTONDBLCLK
                 )
                {
                    myUpToDate = false;
                    this.Invalidate();
                }            else if (m.Msg == win32.WM_MOUSEMOVE)
                {
                    if (m.WParam.ToInt32() != 0)
                    {
                        myUpToDate = false;
                        this.Invalidate();
                    }
                }
            }
      

  7.   

    代码太长,提示一次不能太多,回贴不能连续超过三次。你写个邮箱,给你发过去。
    把包括下面的代码,共三个贴,合并起来
           protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose(disposing);
            }        #endregion
            #region public property overrides        public new BorderStyle BorderStyle
            {
                get { return base.BorderStyle; }
                set
                {
                    if (this.myPaintedFirstTime)
                        this.SetStyle(ControlStyles.UserPaint, false);                base.BorderStyle = value;                if (this.myPaintedFirstTime)
                        this.SetStyle(ControlStyles.UserPaint, true);                this.myBitmap = null;
                    this.myAlphaBitmap = null;
                    myUpToDate = false;
                    this.Invalidate();
                }
            }        public new Color BackColor
            {
                get
                {
                    return Color.FromArgb(base.BackColor.R, base.BackColor.G, base.BackColor.B);
                }
                set
                {
                    myBackColor = value;
                    base.BackColor = value;
                    myUpToDate = false;
                }
            }
            public override bool Multiline
            {
                get { return base.Multiline; }
                set
                {
                    if (this.myPaintedFirstTime)
                        this.SetStyle(ControlStyles.UserPaint, false);                base.Multiline = value;                if (this.myPaintedFirstTime)
                        this.SetStyle(ControlStyles.UserPaint, true);                this.myBitmap = null;
                    this.myAlphaBitmap = null;
                    myUpToDate = false;
                    this.Invalidate();
                }
            }
            #endregion    //end public property overrides
            #region private functions and classes        private int GetFontHeight()
            {
                Graphics g = this.CreateGraphics();
                SizeF sf_font = g.MeasureString("X", this.Font);
                g.Dispose();
                return (int)sf_font.Height;
            }
            private void GetBitmaps()
            {            if (myBitmap == null
                 || myAlphaBitmap == null
                 || myBitmap.Width != Width
                 || myBitmap.Height != Height
                 || myAlphaBitmap.Width != Width
                 || myAlphaBitmap.Height != Height)
                {
                    myBitmap = null;
                    myAlphaBitmap = null;
                }            if (myBitmap == null)
                {
                    myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height);
                    myUpToDate = false;
                }
                if (!myUpToDate)
                {
                    //Capture the TextBox control window                this.SetStyle(ControlStyles.UserPaint, false);                win32.CaptureWindow(this, ref myBitmap);                this.SetStyle(ControlStyles.UserPaint, true);
                    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                    this.BackColor = Color.FromArgb(myBackAlpha, myBackColor);            }
                //--            Rectangle r2 = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
                ImageAttributes tempImageAttr = new ImageAttributes();
                //Found the color map code in the MS Help            ColorMap[] tempColorMap = new ColorMap[1];
                tempColorMap[0] = new ColorMap();
                tempColorMap[0].OldColor = Color.FromArgb(255, myBackColor);
                tempColorMap[0].NewColor = Color.FromArgb(myBackAlpha, myBackColor);            tempImageAttr.SetRemapTable(tempColorMap);            if (myAlphaBitmap != null)
                    myAlphaBitmap.Dispose();
                myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height);            Graphics tempGraphics1 = Graphics.FromImage(myAlphaBitmap);            tempGraphics1.DrawImage(myBitmap, r2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, tempImageAttr);            tempGraphics1.Dispose();            //----            if (this.Focused && (this.SelectionLength == 0))
                {
                    Graphics tempGraphics2 = Graphics.FromImage(myAlphaBitmap);
                    if (myCaretState)
                    {
                        //Draw the caret
                        Point caret = this.findCaret();
                        Pen p = new Pen(this.ForeColor, 3);
                        tempGraphics2.DrawLine(p, caret.X, caret.Y + 0, caret.X, caret.Y + myFontHeight);
                        tempGraphics2.Dispose();
                    }            }        }        private Point findCaret()
            {
                Point pointCaret = new Point(0);
                int i_char_loc = this.SelectionStart;
                IntPtr pi_char_loc = new IntPtr(i_char_loc);            int i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero);
                pointCaret = new Point(i_point);            if (i_char_loc == 0)
                {
                    pointCaret = new Point(0);
                }
                else if (i_char_loc >= this.Text.Length)
                {
                    pi_char_loc = new IntPtr(i_char_loc - 1);
                    i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero);
                    pointCaret = new Point(i_point);                Graphics g = this.CreateGraphics();
                    String t1 = this.Text.Substring(this.Text.Length - 1, 1) + "X";
                    SizeF sizet1 = g.MeasureString(t1, this.Font);
                    SizeF sizex = g.MeasureString("X", this.Font);
                    g.Dispose();
                    int xoffset = (int)(sizet1.Width - sizex.Width);
                    pointCaret.X = pointCaret.X + xoffset;                if (i_char_loc == this.Text.Length)
                    {
                        String slast = this.Text.Substring(Text.Length - 1, 1);
                        if (slast == "\n")
                        {
                            pointCaret.X = 1;
                            pointCaret.Y = pointCaret.Y + myFontHeight;
                        }
                    }            }
                return pointCaret;
            }
            private void myTimer1_Tick(object sender, EventArgs e)
            {
                //Timer used to turn caret on and off for focused control            myCaretState = !myCaretState;
                myCaretUpToDate = false;
                this.Invalidate();
            }
            private class uPictureBox : PictureBox
            {
                public uPictureBox()
                {
                    this.SetStyle(ControlStyles.Selectable, false);
                    this.SetStyle(ControlStyles.UserPaint, true);
                    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    this.SetStyle(ControlStyles.DoubleBuffer, true);                this.Cursor = null;
                    this.Enabled = true;
                    this.SizeMode = PictureBoxSizeMode.Normal;
                }            protected override void WndProc(ref Message m)
                {
                    if (m.Msg == win32.WM_LBUTTONDOWN
                     || m.Msg == win32.WM_RBUTTONDOWN
                     || m.Msg == win32.WM_LBUTTONDBLCLK
                     || m.Msg == win32.WM_MOUSELEAVE
                     || m.Msg == win32.WM_MOUSEMOVE)
                    {
                        win32.PostMessage(this.Parent.Handle, (uint)m.Msg, m.WParam, m.LParam);
                    }                else if (m.Msg == win32.WM_LBUTTONUP)
                    {
                        this.Parent.Invalidate();
                    }
                    base.WndProc(ref m);
                }
            }   // End uPictureBox Class
            #endregion  // end private functions and classes
            #region Component Designer generated code
            private void InitializeComponent()
            {
                components = new System.ComponentModel.Container();
            }
            #endregion
            #region New Public Properties
            [
            Category("Appearance"),
            Description("The alpha value used to blend the control's background. Valid values are 0 through 255."),
            Browsable(true),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)
            ]
            public int BackAlpha
            {
                get { return myBackAlpha; }
                set
                {
                    int v = value;
                    if (v > 255)
                        v = 255;
                    myBackAlpha = v;
                    myUpToDate = false;
                    Invalidate();
                }
            }
            #endregion
        }
    }  
      

  8.   

    用richtextbox不就行了吗using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace CusLib
    {
        public partial class TransparentRichTextBox : RichTextBox
        {
            public TransparentRichTextBox()
            {
                base.ScrollBars = RichTextBoxScrollBars.None;
                SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
                SetStyle(ControlStyles.Opaque, false);
                UpdateStyles();
            }        override protected CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= 0x20;
                    return cp;
                }
            }        override protected void OnPaintBackground(PaintEventArgs e)
            {
            }
        }
    }