rwinkler
asked on
custom Listbox double buffering
I'm using a custom listbox (which uses System.Windows.Forms.ListB ox) and am trying to double buffer it in order to stop the accursed flicker. My test case uses 4 different items in the list. In OnDrawItem, I create a new Bitmap of the appropriate size and then create a Graphics from the Bitmap. Then I "draw" the correct string from the event. Finally, I draw the Bitmap to the ListBox and then dispose of the graphics objects and bitmap (I've tried without disposing them, with the same results). Only the first item gets drawn. Debug.WriteLine code tells me that OnDrawItem is getting called with the appropriate values (text, x & y location, etc.). As a last ditch effort, I put code into the OnDrawItem that looped through all 4 items, drawing each one. This resulted in 4 items getting drawn, all with the same, first value (and the expected issues involving improper invalidation... it was just a test, after all). What have I done wrong? Who did I wrong in a past life?
protected override void OnDrawItem(DrawItemEventAr gs e)
{
base.OnDrawItem(e);
// Verify index is valid before painting
if (e.Index >= 0)
{
for(int i=0;i<4;i++)
{
Bitmap bmp = GetIndexBitmap(i);//e.Inde x);
Graphics graphics = CreateGraphics();
graphics.DrawImage(bmp, e.Bounds.X, e.Bounds.Y);
//Debug.WriteLine("index:" +e.Index+" x="+rectItem.X+" y="+rectItem.Y);
graphics.Dispose();
bmp.Dispose();
}
}
}
protected Bitmap GetIndexBitmap(int nIndex)
{
Rectangle r = GetItemRectangle(nIndex);
Bitmap offScreenBmp = new Bitmap(r.Width, r.Height);
Graphics offScreenDC = Graphics.FromImage(offScre enBmp);
string sItemText = (string)Items[nIndex];
Debug.WriteLine("index:"+n Index+" Items[index]:"+Items[nInde x]+" str="+sItemText);
pCurrentBrush = m_NormalTextSolidBrush;
// Clear the background for fresh drawing
//offScreenDC.FillRectangl e(m_BackSo lidBrush, e.Bounds);
// Draw text
{
offScreenDC.DrawString(sIt emText, Font, pCurrentBrush, (float)(r.Left + m_nIndentLeft),(float)(r.T op + m_nIndentTop));
}
Rectangle rectItem = GetItemRectangle(nIndex);
offScreenDC.DrawImage(offS creenBmp, r.X, r.Y);
offScreenDC.Dispose();
return offScreenBmp;
}
}
}
protected override void OnDrawItem(DrawItemEventAr
{
base.OnDrawItem(e);
// Verify index is valid before painting
if (e.Index >= 0)
{
for(int i=0;i<4;i++)
{
Bitmap bmp = GetIndexBitmap(i);//e.Inde
Graphics graphics = CreateGraphics();
graphics.DrawImage(bmp, e.Bounds.X, e.Bounds.Y);
//Debug.WriteLine("index:"
graphics.Dispose();
bmp.Dispose();
}
}
}
protected Bitmap GetIndexBitmap(int nIndex)
{
Rectangle r = GetItemRectangle(nIndex);
Bitmap offScreenBmp = new Bitmap(r.Width, r.Height);
Graphics offScreenDC = Graphics.FromImage(offScre
string sItemText = (string)Items[nIndex];
Debug.WriteLine("index:"+n
pCurrentBrush = m_NormalTextSolidBrush;
// Clear the background for fresh drawing
//offScreenDC.FillRectangl
// Draw text
{
offScreenDC.DrawString(sIt
}
Rectangle rectItem = GetItemRectangle(nIndex);
offScreenDC.DrawImage(offS
offScreenDC.Dispose();
return offScreenBmp;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops the commented lines are not necessary... ;o)