richtextbox 滚动画面会闪烁的问题。

netboool 2011-12-14 11:08:19
我在一个usercontrol里放置了一个richtextbox的继承类,richtextbox的背景设成了透明,使用以下api

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

protected override CreateParams CreateParams
{
get
{
CreateParams prams = base.CreateParams;
if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
{
prams.ExStyle |= 0x020; // transparent
prams.ClassName = "RICHEDIT50W";
}
return prams;
}
}

我把 usercontrol的代码也贴一下,

public partial class ContainerRTB : UserControl
{
private Bitmap _MemoryBM;
private Graphics _Graphy;

private Single _GridLines_Thickness;


public Single GridLines_Thickness
{
get { return _GridLines_Thickness; }
set { _GridLines_Thickness = value; }
}

private DashStyle _GridLines_Style;


public DashStyle GridLines_Style
{
get { return _GridLines_Style; }
set { _GridLines_Style = value; }
}
private Color _GridLines_Color;


public Color GridLines_Color
{
get { return _GridLines_Color; }
set { _GridLines_Color = value; }
}

private Color _Odd_Color;

public Color Odd_Color
{
get { return _Odd_Color; }
set { _Odd_Color = value; }
}
private Color _Even_Color;

public Color Even_Color
{
get { return _Even_Color; }
set { _Even_Color = value; }
}

public ContainerRTB()
{
InitializeComponent();

this.Invalidate();
//set doublebuffer as true
this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);//ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer |
this.SetStyle(ControlStyles.ResizeRedraw, true);

this.Margin = new Padding(0);
this.Padding = new Padding(0, 0, 0, 0);
UpdateSizeAndPosition();
_MKRichTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;

}

private void CreateBuffer()
{
_MemoryBM = new Bitmap(ClientRectangle.Width
, ClientRectangle.Height);
_Graphy = Graphics.FromImage(_MemoryBM);
}
protected override void OnSizeChanged(EventArgs e)
{
CreateBuffer();
this.Refresh();
base.OnSizeChanged(e);
this.Invalidate();
}

private void CreateBM(int offset)
{
Pen zPen = new Pen(_GridLines_Color, _GridLines_Thickness);

zPen.DashStyle = _GridLines_Style;
SolidBrush zBrush = new SolidBrush(this.ForeColor);
Point zPoint = new Point(0, 0);

Point zPointInParent = this.PointToScreen(this.ClientRectangle.Location);
Point zPointInMe = _MKRichTextBox.PointToScreen(new Point(0, 0));

int zParentInMe = zPointInParent.Y - zPointInMe.Y + 1 + offset;

TimeSpan zTimeSpan = new TimeSpan(DateTime.Now.Ticks);
zPoint = _MKRichTextBox.GetPositionFromCharIndex(0);
SolidBrush colorBrush = null;
for (int i = 0; i <= getLineCount(_ContentRectangle.Height, _MKRichTextBox.Height); i++)
{

if (i % 2 == 0)
{
colorBrush = new SolidBrush(_Even_Color);
}
else
{
colorBrush = new SolidBrush(_Odd_Color);
}

{
_Graphy.FillRectangle(colorBrush, new Rectangle(0, zPoint.Y + _Line_Height * (i - 1) + zParentInMe, _MKRichTextBox.Width , _Line_Height - 1));

_Graphy.DrawLine(zPen, new Point(0, zPoint.Y + _Line_Height * i + zParentInMe), new Point(_MKRichTextBox.Width , zPoint.Y + _Line_Height * i + zParentInMe));
}
if (_isScrolling && DateTime.Now.Ticks > zTimeSpan.Ticks + 500000)
{

_isScrolling = false;
_timer.Start();
break;
}

}

if ((getLineCount(_ContentRectangle.Height, _MKRichTextBox.Height) + 1) % 2 == 0)
{
colorBrush = new SolidBrush(_Even_Color);
}
else
{
colorBrush = new SolidBrush(_Odd_Color);
}

{
_Graphy.FillRectangle(colorBrush, new Rectangle(0, zPoint.Y + _Line_Height * getLineCount(_ContentRectangle.Height, _MKRichTextBox.Height) + zParentInMe, _MKRichTextBox.Width, _Line_Height - 1));
}
if (zPen != null)
{
zPen.Dispose();
}

if (colorBrush != null)
{
colorBrush.Dispose();
}

if (zBrush != null)
{
zBrush.Dispose();
}
}

private int getLineCount(int contentHeight, int rtbHeight)
{
int ret = Math.Max(contentHeight, rtbHeight);
if (_Line_Height != 0)
{
ret = ret / _Line_Height;
}
else
{
ret = 0;
}
return ret;
}

private void mkRichTextBox_VScroll(object sender, EventArgs e)
{

CreateBM(0);
this.BackgroundImage = _MemoryBM;

}

private void mkRichTextBox_ContentsResized(object sender, ContentsResizedEventArgs e)
{
_ContentRectangle = e.NewRectangle;
}

private void ContainerRTB_Load(object sender, EventArgs e)
{
CreateBM(0);
this.BackgroundImage = _MemoryBM;
this.Refresh();
}
}



如果把doublebuffer给打开就会出现切换窗口滚动条消失的 情况

如果把doublebuffer关闭,拖动滚动条闪烁就比较严重,但是即使doublebuffer打开,也会闪烁,各位老师,看看我的代码是不是有问题,怎样来解决闪烁的问题呢??
...全文
258 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Echo_Echo_. 2021-07-11
  • 打赏
  • 举报
回复

请问解决了吗 ,我也遇到了这个问题

Hogan Hu 2013-01-10
  • 打赏
  • 举报
回复
nobody nobody nobody!!!! 敢问楼主你问题解决了吗,我也是很头疼这个问题

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧