//---------------------------------------------------------------------------
__fastcall
TForm1::TForm1(TComponent* Owner)
:
TForm(Owner)
{
//Get the Header control's handle (KEY METHOD)
HWND HeaderHandle = GetDlgItem(ListView1->Handle, 0);
HD_ITEM hdi;
for (int index = 0; index < ListView1->Columns->Count;
index++)
{
//Specify that we're going to change the fmt
member
hdi.mask = HDI_FORMAT;
//Flag owner draw state
hdi.fmt = HDF_OWNERDRAW;
//Force the changes
Header_SetItem(HeaderHandle, index, &hdi);
}
//subclass the window procedure
OldListViewWP = ListView1->WindowProc;
ListView1->WindowProc = NewListViewWP;
}
//---------------------------------------------------------------------------
//new
window procedure
void
__fastcall
TForm1::NewListViewWP(TMessage &Msg)
{
if (Msg.Msg == WM_DRAWITEM)
{
LPDRAWITEMSTRUCT lpdis = (DRAWITEMSTRUCT *)Msg.LParam;
HDC HeaderHDC = lpdis->hDC;
RECT Rect = lpdis->rcItem;
int Index = lpdis->itemID;
int State = lpdis->itemState;
TFont *HeaderFont = new TFont();
TBrush *HeaderBrush = new TBrush();
HeaderFont->Style = HeaderFont->Style << fsBold;
HeaderBrush->Color = clBlue;
SelectObject(HeaderHDC, HeaderFont->Handle);
::FillRect(HeaderHDC, &Rect, HeaderBrush->Handle);
::SetTextColor(HeaderHDC, ColorToRGB(clYellow));
::SetBkMode(HeaderHDC, TRANSPARENT);
int x_offset = 2;
int y_offset = 1;
int text_offset = 5;
//Handle the depressed case by
//offsetting the text and bitmap
if (State & ODS_SELECTED)
{
x_offset = x_offset + 1;
y_offset = y_offset + 1;
text_offset = text_offset + 1;
}
//Lets draw Image1 only on the first columns
header
if (Index == 0)
{
BitBlt(HeaderHDC, Rect.left + x_offset,
Rect.top + y_offset,
Image1->Picture->Bitmap->Width,
Image1->Picture->Bitmap->Height,
Image1->Canvas->Handle, 0, 0, SRCCOPY);
Rect.left = Rect.left + Image1->Picture->Bitmap->Width
+ text_offset;
}
else Rect.left = Rect.left + text_offset;
Rect.top = Rect.top + y_offset;
AnsiString text = ListView1->Columns->Items[Index]->Caption;
::DrawText(HeaderHDC, text.c_str(), text.Length(), &Rect, DT_LEFT);
delete HeaderFont;
delete HeaderBrush;
Msg.Result = true;
}
else OldListViewWP(Msg);
}
void
__fastcall
TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ListView1->WindowProc = OldListViewWP;
}
|