洒家的窝~~

博客园 首页 联系 订阅 管理

问题是这样的,好像是TextBoxRichTextBox的BackColor属性并不能接受Color.Transparent。所以我分别给TextBoxRichTextBox控件找到了各自的解决方案

这不是一个完美的解决方案,初学者可能会很难理解他。如果你对面向对象程序设计和C#很在行,那么这篇文章对你来说肯定是小菜一碟

 

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

 

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( 00this.ClientRectangle.Width, 
              
this.ClientRectangle.Height );
 
            CaptureWindow( 
thisref 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 , 0this.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();
}

 

posted on 2009-04-05 18:34  tianxu0836  阅读(2778)  评论(0编辑  收藏  举报