在列表视图上绘画会禁用列表视图项目的重绘
这是什么原因呢?
我重写 OnPaintBackground 并绘制一个字符串。直到我在构造函数中调用它之后它才会显示:
this.SetStyle ( ControlStyles.UserPaint, true );
但是我在列表视图中看不到这些项目。
为什么以及如何解决这个问题?
编辑:代码
protected override void OnPaintBackground ( PaintEventArgs pevent )
{
base.OnPaintBackground ( pevent );
// Create string to draw.
String drawString = "76";
// Create font and brush.
Font drawFont = new Font ( "Arial", 36 );
SolidBrush drawBrush = new SolidBrush ( Color.Blue );
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF ( 150.0F, 150.0F );
// Draw string to screen.
pevent.Graphics.DrawString ( drawString, drawFont, drawBrush, drawPoint );
//pevent.Graphics.FillRectangle ( drawBrush, this.ClientRectangle );
}`enter code here`
What's the reason for this?
I override OnPaintBackground and draw a string. It doesn't show up until I call this in the constructor:
this.SetStyle ( ControlStyles.UserPaint, true );
But then I don't see the items in the listview.
Why and how to solve this?
EDIT: code
protected override void OnPaintBackground ( PaintEventArgs pevent )
{
base.OnPaintBackground ( pevent );
// Create string to draw.
String drawString = "76";
// Create font and brush.
Font drawFont = new Font ( "Arial", 36 );
SolidBrush drawBrush = new SolidBrush ( Color.Blue );
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF ( 150.0F, 150.0F );
// Draw string to screen.
pevent.Graphics.DrawString ( drawString, drawFont, drawBrush, drawPoint );
//pevent.Graphics.FillRectangle ( drawBrush, this.ClientRectangle );
}`enter code here`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在关于该主题的上一篇文章中所说,
OnPaint()
和UserPaint
不适用于 ListView。该绘画由底层控件处理,不能以这种方式拦截。这与其他控件不同,因此,当
ControlStyles.UserPaint
为true
时,不会告诉底层控件重绘自身。相反,所有绘图都会路由到OnPaintBackground()
和OnPaint()
方法,正如您所发现的那样,它们什么也不做。有两种方法可以满足您的要求(第二种比第一种更好):
第一种方法:拦截
WM_PAINT
,进行基本处理,然后绘制到列表视图上。像这样的事情:但是,当您绘制的内容位于列表视图认为包含控件内容的区域之外时,这会带来重绘问题 - 它不会触发绘制事件。
第二种方式:拦截CustomDraw通知(这与OwnerDraw不同),并监听
CDDS_POSTPAINT
阶段。在该阶段,您可以安全地在列表视图上绘图。您可以查看 ObjectListView 的代码来了解它是如何完成的。您也可以直接使用 ObjectListView 来省去很多麻烦:)
As I said in the last thread about this subject,
OnPaint()
andUserPaint
don't work with ListView. The painting is handled by the underlying control and cannot be intercepted in that fashion. This is different to other ControlsSo, when
ControlStyles.UserPaint
istrue
, the underlying control is not told to redraw itself. Instead, all drawing is routed to theOnPaintBackground()
andOnPaint()
methods, which -- as you have found -- do nothing.There are two ways to do what you asked (the second is better than the first):
First way: Intercept the
WM_PAINT
, do the base processing, and then draw onto the listview. Something like this:But this gives repaint problems when what you draw is outside the area that the listview thinks holds the control's contents -- it doesn't trigger paint events.
Second manner: Intercept the CustomDraw notification (this is not the same as OwnerDraw), and listen for the
CDDS_POSTPAINT
stage. In that stage you can safely draw onto the list view. You can look at the code of ObjectListView to see how it is done.You could also just save yourself a lot of bother and use an ObjectListView directly :)