Richtextbox控件透明加入背景

http://www.codeproject.com/cs/miscctrl/TransparentTextBox.asp
如何让Richtextbox控件透明:透明后你自己在后边设定背景图片吧,不过一般都很晃动
也有另外的方法来设置透明,用定时器刷新,如下地址:
http://www.codeproject.com/cs/miscctrl/AlphaBlendedTextControls.asp

RichTextBox

class TransparentControl : Control
{
   public TransparentControl()
   {
      base.SetStyle( ControlStyles.UserPaint, true );
      base.SetStyle( ControlStyles.DoubleBuffer, true );
      base.SetStyle( ControlStyles.SupportsTransparentBackColor, true );
   }
}

class TransparentRichTextBox : RichTextBox
{
   public TransparentRichTextBox()
   {
      base.ScrollBars = RichTextBoxScrollBars.None;
   }

   override protected CreateParams CreateParams
   {
      get
      {
         CreateParams cp = base.CreateParams;
         cp.ExStyle |= 0x20;
         return cp;
      }
   }

   override protected void OnPaintBackground( PaintEventArgs e )
   {
   }
}

TextBox

Collapse
public class TransparentTextBox : TextBox
{
   PictureBox pictureBox = new PictureBox();

   public TransparentTextBox()
   {
      pictureBox.Dock = DockStyle.Fill;
      this.Controls.Add( pictureBox ); 
   }
   protected override void WndProc( ref Message m )
   {
      base.WndProc( ref m );
      switch( m.Msg )
      {
         case Win32.WM_PAINT:

            Bitmap bmpCaptured = 
              new Bitmap( this.ClientRectangle.Width, this.ClientRectangle.Height );
            Bitmap bmpResult = 
              new Bitmap( this.ClientRectangle.Width,this.ClientRectangle.Height );
            Rectangle r = 
              new Rectangle( 0, 0, this.ClientRectangle.Width, 
              this.ClientRectangle.Height );
 
            CaptureWindow( this, ref bmpCaptured ); 
            this.SetStyle( ControlStyles.SupportsTransparentBackColor, true );
            this.BackColor = Color.Transparent;

            ImageAttributes imgAttrib = new ImageAttributes();

            ColorMap[] colorMap = new ColorMap[ 1 ];

            colorMap[ 0 ] = new ColorMap();

            colorMap[ 0 ].OldColor = Color.White;

            colorMap[ 0 ].NewColor = Color.Transparent;

            imgAttrib.SetRemapTable( colorMap ); 

            Graphics g = Graphics.FromImage( bmpResult );

            g.DrawImage( bmpCaptured, r, 0 , 0, this.ClientRectangle.Width, 
                this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib );

            g.Dispose();

            pictureBox.Image = ( Image )bmpResult.Clone(); 
         break;


         case Win32.WM_HSCROLL:

         case Win32.WM_VSCROLL:

            this.Invalidate(); // repaint
           // if you use scrolling then add these two case statements

         break;
   }
}

private static void CaptureWindow( Control control, ref Bitmap bitmap )
{
   Graphics g = Graphics.FromImage( bitmap );
   int i = ( int )( Win32.PRF_CLIENT | Win32.PRF_ERASEBKGND );
   IntPtr iPtr = new IntPtr( 14 );
   IntPtr hdc = g.GetHdc();
   Win32.SendMessage( control.Handle, Win32.WM_PRINT, hdc, iPtr ); 
   g.ReleaseHdc( hdc );
   g.Dispose();
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值