C#/.net WinForm如何做一个背景透明的RichTextBox
我在网上搜到一个重写控件的代码,虽然背景透明了,但是很不稳定,我需要对这个RichTextBox进行大小、位置的改动,甚至滚动内容,这时控件就会闪烁,双重缓冲试过了,只会...
我在网上搜到一个重写控件的代码,虽然背景透明了,但是很不稳定,我需要对这个RichTextBox进行大小、位置的改动,甚至滚动内容,这时控件就会闪烁,双重缓冲试过了,只会让背景变黑。。。求大神有什么好办法不让他闪烁吗,或者有别的办法重写一个RichTextBox??谢谢~~
那个重写的代码:
class TransparentRichTextBox : RichTextBox
{
override protected CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
override protected void OnPaintBackground(PaintEventArgs e)
{
}
} 展开
那个重写的代码:
class TransparentRichTextBox : RichTextBox
{
override protected CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
override protected void OnPaintBackground(PaintEventArgs e)
{
}
} 展开
展开全部
如何让Richtextbox控件透明:透明后你自己在后边设定背景图片吧,不过一般都很晃动
也有另外的方法来设置透明,用定时器刷新,
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(); }
也有另外的方法来设置透明,用定时器刷新,
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(); }
更多追问追答
追问
我不设置背景图片,我只是放在窗体里可以继承窗体内的背景,不一定是图片,可能是窗体自绘的一些背景,就跟其他PictureBox设置背景色为TransParent一样,有透明度的图片,会显示周围一圈为原窗体的背景。
追答
那就自己写个空间就行了,没有现成的
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-06-09
展开全部
... ... 这问题多试试看是有bug还是别的原因啦,解决起来应该需要点时间但不是很难的吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个简单
追问
简单那你说怎么做啊?就知道骗经验
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询