我已经创建了自定义ListView控件以适应我的需要,并且我遇到了一个问题,导致ListView在窗体不显示任何内容时(不绘制任何东西,只是白色)第一次加载。自定义ListView控件在第一次显示时不会绘制
如果我调整窗体大小或单击我的控件(任何强制在ListView上重绘的东西),它会按预期显示。
作为一个方面说明,它曾经工作得很好,直到我今天做了一个小改动并重新构建了控件。我删除了我所做的所有更改并重新进行了重建,但问题仍然存在。任何想法为什么它不会显示(油漆)时,第一次加载窗体?
这是我用来做我自定义ListView控件中的自定义绘制...
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
Image image = e.Item.ImageList.Images[e.Item.ImageIndex];
Size textSize = new Size((int)e.Graphics.MeasureString(e.Item.Text, e.Item.Font).Width, (int)e.Graphics.MeasureString(e.Item.Text, e.Item.Font).Height);
//Get the area of the item to be painted
Rectangle bounds = e.Bounds;
bounds.X = 0;
bounds.Width = this.Width;
//Set the spacing on the list view items
int hPadding = 0;
int vPadding = 0;
IntPtr padding = (IntPtr)(int)(((ushort)(hPadding + bounds.Width)) | (uint)((vPadding + bounds.Height) << 16));
SendMessage(this.Handle, (uint)ListViewMessage.LVM_SETICONSPACING, IntPtr.Zero, padding);
//Set the positions of the image and text
int imageLeft = (bounds.Width/2) - (image.Width/2);
int imageTop = bounds.Top + 3;
int textLeft = (bounds.Width/2) - (textSize.Width/2);
int textTop = imageTop + image.Height;
Point imagePosition = new Point(imageLeft, imageTop);
Point textPosition = new Point(textLeft, textTop);
//Draw background
using (Brush brush = new SolidBrush(e.Item.BackColor))
e.Graphics.FillRectangle(brush, bounds);
//Draw selected
if (e.Item.Selected)
{
using (Brush brush = new SolidBrush(m_SelectedColor))
e.Graphics.FillRectangle(brush, bounds);
}
//Draw image
e.Graphics.DrawImage(image, imagePosition);
//Draw text
e.Graphics.DrawString(e.Item.Text, e.Item.Font, new SolidBrush(e.Item.ForeColor), textPosition);
}
我还设置了下面的事情在我的自定义控件的构造......
public MyListView()
{
this.DoubleBuffered = true;
this.OwnerDraw = true;
this.View = View.LargeIcon;
this.Cursor = Cursors.Hand;
this.Scrollable = false;
}
我也继承的ListView类...
public class MyListView : ListView
{
//All my source
}
是否hapen在标签页不可见,在第一坐?另外:你确定OnDrawItem没有被调用,或者它可能刚刚没有按照预期工作? (设置一个断点!) – TaW
我直接在窗体上。我曾经使用选项卡控制,但有问题,所以我逃避了这个方向。我怎么知道OnDrawItem是否是“被调用的”?我想这可能不会像预期的那样在第一次抽签中起作用......我只是无法弄清楚原因。如果在显示控件后SAME代码完美无缺,那么为什么不是第一次绘制呢? –
事实上,我认为OnDrawItem甚至没有出于某种原因解雇,直到我做了像强制调整大小等内容。 –