Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
    base.OnDrawItem(e);
    DrawItemInternal(e);
}

internal void DrawItemInternal(DrawListViewItemEventArgs e)
{
    Rectangle backRect = e.Bounds;
    Graphics g = e.Graphics;
    ListViewItem item = e.Item;
    ListViewItemStates itemState = e.State;

    ImageList imageList = item.ImageList;
    bool bDrawImage = (imageList != null) && (item.ImageIndex != -1 || !string.IsNullOrEmpty(item.ImageKey));
    bool bSelected = (itemState & ListViewItemStates.Selected) != 0;

    if (bDrawImage)
    {
        DrawImage(g, item, bSelected);
    }

    if (bDrawImage == true)
    {
        backRect.X = backRect.X + 20;
        backRect.Width = backRect.Width - 20;

    }

    Rectangle rect = new Rectangle(backRect.X, backRect.Y, backRect.Width - 1, backRect.Height - 1);
    //Console.WriteLine(itemState.ToString());
    if (itemState == (ListViewItemStates.Focused|ListViewItemStates.ShowKeyboardCues| ListViewItemStates.Selected))
    {
        RectangleF rectf = new RectangleF(backRect.X, backRect.Y, backRect.Width - 1, backRect.Height - 0.5f);
        using (SolidBrush blueBrush = new SolidBrush(Color.FromArgb(49, 106, 196)))
        {
            g.FillRectangle(blueBrush, rectf);
        }

        using (Pen pen1 = new Pen(Color.Black))
        {
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g.DrawRectangle(pen1, rect);
        }
    }

    if (itemState == (ListViewItemStates.Focused | ListViewItemStates.ShowKeyboardCues))
    {
        using (Pen pen1 = new Pen(Color.Black))
        {
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g.DrawRectangle(pen1, rect);
        }
    }

    Console.WriteLine(itemState.ToString());

}

I custom listview and draw listview item ,there's some questions.
when i choose one item,it will be show blue background,when i leave the mouse,it will be show dot rectangle around this item.

so the itemState below is:
if (itemState == (ListViewItemStates.Focused|ListViewItemStates.ShowKeyboardCues| ListViewItemStates.Selected))

but it's in windows xp environment,if i run my software in windows 7 environment. the itemState below is:
if (itemState == (ListViewItemStates.Focused|ListViewItemStates.Selected))

why the itemState is diffirent?
Posted
Updated 15-Apr-15 23:07pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900