悬停时的字体颜色,用于组合框中的列表项

时间:2010-07-21 19:59:34

标签: c# winforms user-controls

我有一个具有自定义绘制项目的组合框。它还利用斑马条纹,因此,悬停颜色有时会脱落。如何控制悬停时的外观?

这是我使用的代码:

   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();
    }

1 个答案:

答案 0 :(得分:0)

捕获MouseMove事件并创建某种成员变量,以便在鼠标位于条纹“斑马”项目上时充当标记。如有必要,请调用Refresh()方法。