Board index » cppbuilder » ListView SubItems Background Draw

ListView SubItems Background Draw

I've been trying to figure out how to Draw a Rectangle on ListView specific
SubItem area. What i'm trying to do is, My program shows the user online &
there idle time, i'm trying to draw a progress bar in back of Subitems. If
you understand what i'm saying, please give me a small example, something do
do with Canvas. I'd appreciate it...

Sorry for bad English
-Sarorn

 

Re:ListView SubItems Background Draw


Hi Sarorn,

Quote
> I've been trying to figure out how to Draw a Rectangle on ListView specific
> SubItem area. What i'm trying to do is, My program shows the user online &
> there idle time, i'm trying to draw a progress bar in back of Subitems. If
> you understand what i'm saying, please give me a small example, something do
> do with Canvas.

Here's a simple example using the OnCustomDrawSubItem event.  If your version of
BCB does not exhibit such an event,
you'll need to use the Custom Draw service via the API -- the following
reference provides some code examples...

http://bcbcaq.freeservers.com/CustomDraw_LV.html

void __fastcall TForm1::ListView1CustomDrawSubItem(TCustomListView *Sender,
    TListItem *Item, int SubItem, TCustomDrawState State,
    bool &DefaultDraw)
{
    TListView *ListView = static_cast<TListView*>(Sender);
    if (ListView)
    {
        // extract the bounding rectangle of the subitem
        RECT R = {LVIR_BOUNDS, SubItem, 0, 0};
        SNDMSG(ListView->Handle, LVM_GETSUBITEMRECT, Item->Index,
               reinterpret_cast<LPARAM>(&R));
        InflateRect(&R, -1, -1);

        // render the progress bar
        TCanvas *LVCanvas = ListView->Canvas;
        LVCanvas->Brush->Color = clWindow;
        LVCanvas->Rectangle(R.left, R.top, R.right, R.bottom);

        // fill the progress bar
        InflateRect(&R, -LVCanvas->Pen->Width, -LVCanvas->Pen->Width);
        LVCanvas->Brush->Color = clHighlight;
        LVCanvas->FillRect(R); // fill 100% (adjust R.right otherwise)

        // render the text
        AnsiString text(Item->SubItems->Strings[SubItem - 1]);
        HDC Hdc = LVCanvas->Handle;
        SetBkMode(Hdc, TRANSPARENT);
        SetTextColor(Hdc, ColorToRGB(clHighlightText));
        DrawText(Hdc, text.c_str(), text.Length(),
                 &R, DT_LEFT | DT_VCENTER | DT_SINGLELINE);

        // tell Windows we drew the item manually
        DefaultDraw = false;
    }

Quote
}

Good luck!

--
Damon Chandler
http://bcbcaq.freeservers.com

Re:ListView SubItems Background Draw


Thanks, it works. But one problem, when i move an object over that Subitem
that is drawed, the part that is overlapsed get to its original.

Quote
Damon Chandler <dm...@cornell.edu> wrote in message

news:38B9968D.5C4ED2FC@cornell.edu...
Quote
> Hi Sarorn,
> > I've been trying to figure out how to Draw a Rectangle on ListView
specific
> > SubItem area. What i'm trying to do is, My program shows the user online
&
> > there idle time, i'm trying to draw a progress bar in back of Subitems.
If
> > you understand what i'm saying, please give me a small example,
something do
> > do with Canvas.

> Here's a simple example using the OnCustomDrawSubItem event.  If your
version of
> BCB does not exhibit such an event,
> you'll need to use the Custom Draw service via the API -- the following
> reference provides some code examples...

> http://bcbcaq.freeservers.com/CustomDraw_LV.html

> void __fastcall TForm1::ListView1CustomDrawSubItem(TCustomListView
*Sender,
>     TListItem *Item, int SubItem, TCustomDrawState State,
>     bool &DefaultDraw)
> {
>     TListView *ListView = static_cast<TListView*>(Sender);
>     if (ListView)
>     {
>         // extract the bounding rectangle of the subitem
>         RECT R = {LVIR_BOUNDS, SubItem, 0, 0};
>         SNDMSG(ListView->Handle, LVM_GETSUBITEMRECT, Item->Index,
>                reinterpret_cast<LPARAM>(&R));
>         InflateRect(&R, -1, -1);

>         // render the progress bar
>         TCanvas *LVCanvas = ListView->Canvas;
>         LVCanvas->Brush->Color = clWindow;
>         LVCanvas->Rectangle(R.left, R.top, R.right, R.bottom);

>         // fill the progress bar
>         InflateRect(&R, -LVCanvas->Pen->Width, -LVCanvas->Pen->Width);
>         LVCanvas->Brush->Color = clHighlight;
>         LVCanvas->FillRect(R); // fill 100% (adjust R.right otherwise)

>         // render the text
>         AnsiString text(Item->SubItems->Strings[SubItem - 1]);
>         HDC Hdc = LVCanvas->Handle;
>         SetBkMode(Hdc, TRANSPARENT);
>         SetTextColor(Hdc, ColorToRGB(clHighlightText));
>         DrawText(Hdc, text.c_str(), text.Length(),
>                  &R, DT_LEFT | DT_VCENTER | DT_SINGLELINE);

>         // tell Windows we drew the item manually
>         DefaultDraw = false;
>     }
> }

> Good luck!

> --
> Damon Chandler
> http://bcbcaq.freeservers.com

Other Threads