I made a ListBox.
There are image and Text in ListBox Like picture.
And i want to draw transparent Image on listbox When mousemove.
private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
int iWidth = image.Width;
int iHeight = image.Height;
ImageAttributes attr = new ImageAttributes();
ColorMatrix matrix = new ColorMatrix();
matrix.Matrix33 = 0.8f;
attr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
e.Graphics.DrawImage(image, new Rectangle(0, 0 iWidth, iHeight), 0, 0, iWidth, iHeight, GraphicsUnit.Pixel, attr);
}
This is my code.
But I have a problem.
Although I input the same value of transparency, Transparency of each image is different..
How can I solve it ??
++ Add Code
I use Scrollbar.
int LocationY = ListBox.PreferredHeight / ListBox.Items.Count * (Index/*ListBox index*/ - ListBox.TopIndex);
if ((LocationY + image.Height) > Listbox.Height)
LocationY = ListBox.Height - image.Height;
e.Graphics.DrawImage(image, new Rectangle(0, LocationY, iWidth, iHeight), 0, 0, iWidth, iHeight, GraphicsUnit.Pixel, attr);
When ownerdrawing a ListBox
it is important to understand how the DrawItem
event is called.
It will be called on several occasions, always referring only to the Item
it was called for.
But for better performance not all occasions will make the ListBox
draw all its items!
The area you are meant to draw on is the e.Bounds
rectangle.
You can still draw over the whole ListBox
but as the event usually is called several times in a row you may run into some issues. One such issue is with transparent drawings, as these will stack and become less and less transparent!
Let's look a few cases:
Refresh
all visible Items will be drawn Item
the selected item and also all previously selected items will be drawn So, after the initial display each item will have been drawn once but after selecting item3
and then item1
the first three items will all have been drawn different times: item1
: 2x, item2
:1x and item3
: 3x.
The workaound will usually be to create a clean slate before drawing an item, maybe like this:
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
This however will not go well with anything you draw across the item bounds!
Any image you draw on an Item
should respect the bounds. You can either squeeze an image into the item's bounds:
e.Graphics.DrawImage(image, e.Bounds, 0, 0, iWidth, iHeight, GraphicsUnit.Pixel, attr);
Or you can draw just a slice of a larger image, maybe like this:
e.Graphics.DrawImage(image, e.Bounds,
0, e.Index * e.Bounds.Height, iWidth, e.Bounds.Height,
GraphicsUnit.Pixel, attr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.