Public class listviewprogress: system. Windows. Forms. listview
{
Private string progresstext;
Private system. Drawing. Color progressbackcolor;
Public System. Drawing. Color progressbackcolor
{
Get {return progressbackcolor ;}
Set {progressbackcolor = value ;}
}
Private system. Drawing. Color progresstextcolor;
Public System. Drawing. Color progresstextcolor
{
Get {return progresstextcolor ;}
Set {progresstextcolor = value ;}
}
Private int progresscolumnindex =-1;
Public int progresscolumnindex
{
Get {return progresscolumnindex ;}
Set {progresscolumnindex = value ;}
}
Public listviewprogress ()
{
This. Name = "progresslistview ";
This. progressbackcolor = system. Drawing. color. yellowgreen;
This. progresstextcolor = base. forecolor;
This. ownerdraw = true;
}
Protected override void ondrawcolumnheader (system. Windows. Forms. drawlistviewcolumnheadereventargs E)
{
E. drawdefault = true;
Base. ondrawcolumnheader (E );
}
Protected override void ondrawsubitem (system. Windows. Forms. drawlistviewsubitemeventargs E)
{
If (E. columnindex = This. progresscolumnindex)
{
// Draw a progress bar
System. Drawing. Graphics progressgraphics = E. graphics;
System. Drawing. Rectangle progressrect = new system. Drawing. rectangle (
E. bounds. Left, E. bounds. Top, E. bounds. Width, E. bounds. Height );
If (progressrect. Height> 6 & progressrect. width> 6)
{
// Adjust to a new area to produce a certain interval
Progressrect = new system. Drawing.
Rectangle (E. bounds. Left + 2, E. bounds. Top + 2, E. bounds. Width-5, E. bounds. Height-5 );
Single percent = 0;
Try
{
Percent = convert. tosingle (E. Subitem. Text );
}
Catch
{
Percent = 0;
}
If (percent> 1.0f)
Percent = percent/100366f;
// External box
Progressgraphics. fillrectangle (system. Drawing. Brushes. White, progressrect );
Progressgraphics. drawrectangle (new system. Drawing. Pen (E. Subitem. forecolor), progressrect );
// Content
System. Drawing. Rectangle progresscontentrect = new rectangle (
Progressrect. Left + 1, progressrect. Top + 1, (INT) (progressrect. Width * percent)-1, progressrect. Height-1 );
Progressgraphics. fillrectangle (new system. Drawing. solidbrush (progressbackcolor), progresscontentrect );
// Output text
If (E. Subitem. Font. height <progressrect. Height)
{
Progressrect = new system. Drawing. rectangle (progressrect. Left, progressrect. Top
-(Progressrect. Height-E. Subitem. Font. Height)/2), progressrect. Width, progressrect. Height );
}
Else
{
Progressrect = new system. Drawing. rectangle (E. bounds. Left, E. bounds. Top, E. bounds. Width, E. bounds. Height );
}
Using (stringformat Sf = new stringformat ())
{
SF. Alignment = system. Drawing. stringalignment. Center;
SF. linealignment = system. Drawing. stringalignment. Center;
SF. trimming = system. Drawing. stringtrimming. ellipsischaracter;
E. Graphics. drawstring (percent. tostring ("p1"), E. Subitem. Font,
New system. Drawing. solidbrush (this. progresstextcolor), progressrect, SF );
}
}
Base. ondrawsubitem (E );
}
Else
{
E. drawdefault = true;
Base. ondrawsubitem (E );
}
}
}
Usage: After the data is loaded, set the progresscolumnindex attribute.