I have a combobox that has custom drawn items. It also makes use of zebra striping and because of this, the hover color is sometimes off. How can I control how it looks on hover?
Here's the code I use:
Color zeros = Color.FromArgb(200, 200, 200);
Color ones = Color.FromArgb(225, 225, 255);
private void innerBox_DrawItem(object sender, DrawItemEventArgs e) {
Brush brush = new SolidBrush(e.ForeColor);
Pen pen = new Pen(e.ForeColor);
if (e.Index % 2 == 0) {
Brush backgroundBrush = new SolidBrush(zeros);
e.Graphics.FillRectangle(backgroundBrush, e.Bounds);
} else {
Brush backgroundBrush = new SolidBrush(ones);
e.Graphics.FillRectangle(backgroundBrush, e.Bounds);
}
Tenant tenant = ((ComboBox)sender).Items[e.Index] as Tenant;
Rectangle rect = e.Bounds;
//Draw Tenant Name
if (tenant.TenantName != null) {
e.Graphics.DrawString(tenant.TenantName, e.Font, brush, rect, StringFormat.GenericDefault);
}
rect.X += MaxTenantLength;
e.Graphics.DrawLine(pen, new Point(rect.X - 3, rect.Y), new Point(rect.X - 3, rect.Y + e.开发者_如何学编程Bounds.Height));
//Draw Property
if (tenant.Property != null) {
e.Graphics.DrawString(tenant.Property.PropertyName, e.Font, brush, rect, StringFormat.GenericDefault);
}
rect.X += MaxPropertyLength;
e.Graphics.DrawLine(pen, new Point(rect.X - 3, rect.Y), new Point(rect.X - 3, rect.Y + e.Bounds.Height));
//Draw Rental Unit
if (tenant.RenatlUnit != null) {
e.Graphics.DrawString(tenant.RenatlUnit.UnitNumber, e.Font, brush, rect, StringFormat.GenericDefault);
}
rect.X += MaxRentalUnitLength;
e.DrawFocusRectangle();
}
Capture the MouseMove event and create some kind of member variable to act as a flag if the mouse is over a striped "zebra" item. Call the Refresh() method, if necessary.
精彩评论