引自about:delphi programming,详情见:http://delphi.about.com/od/vclusing/a/owner_drawing.htm procedure TForm1.mnuBoldDrawItem (Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean) ; var ImgID: integer; begin if Selected then ACanvas.Brush.Color := clHighlight else ACanvas.Brush.Color := clMenu; {move the rect to make place for the side bar} ARect.Left := 20; ACanvas.FillRect(ARect) ; if mnuBold.Checked then ImgID := 1 {thumb up in the ImageList} else ImgID := 0; {thumb down in the ImageList} ImageList1.Draw(ACanvas,22,ARect.Top + 2,ImgID) ; ACanvas.Font.Style := [fsBold]; {user defined text drawing function:} DrawItemText(45,ACanvas,ARect,'Bold') ; end;
Graphics::TBitmap *bmp; // Line 1
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
bmp = new Graphics::TBitmap; // Line 2
bmp->LoadFromFile("Bar.bmp"); // Line 3 // (bmp = new Graphics::TBitmap)->LoadFromFile("Bar.bmp");
// 这样写又可以省一行,哈哈,不过怕新手看不明白 :)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BarItemMeasureItem(TObject *Sender,
TCanvas *ACanvas, int &Width, int &Height)
{
Width += 2; // Line 4
Height *= BarItem->Parent->Count - 1; // Line 5
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BarItemDrawItem(TObject *Sender, TCanvas *ACanvas,
TRect &ARect, bool Selected)
{
ACanvas->Draw(0, 0, bmp); // Line 6
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete bmp; // Line 7
}
//--------------------------
//---------------------------------------------------------------------------
procedure TForm1.Form1Create(Sender:TObject);
begin
bmp := TBitmap.Create; // Line 2
bmp.LoadFromFile('Bar.bmp'); // Line 3 // (bmp = new Graphics::TBitmap)->LoadFromFile("Bar.bmp");
// 这样写又可以省一行,哈哈,不过怕新手看不明白 :)
end;
//---------------------------------------------------------------------------
procedure TForm1.BarItemMeasureItem(Sender:TObject;
ACanvas:TCanvas;Width,Height:integer);
begin
Width:=Width + 2; // Line 4
Height := Height*(BarItem.Parent.Count - 1); // Line 5
end;
//---------------------------------------------------------------------------
procedure TForm1.BarItemDrawItem(Sender:TObject ; ACanvas:TCanvas;
ARect:TRect ; Selected:boolean );
begin
ACanvas.Draw(0, 0, bmp); // Line 6
end;
//---------------------------------------------------------------------------
procedure TForm1.FormDestroy(Sender:TObject);
begin
bmp.Free; // Line 7
end;
//--------------------------
图片定位有问题,象是加了个背景的效果,咋整
procedure TForm1.mnuBoldDrawItem (Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean) ;
var
ImgID: integer;
begin if Selected then
ACanvas.Brush.Color := clHighlight
else
ACanvas.Brush.Color := clMenu; {move the rect to make place for the side bar}
ARect.Left := 20; ACanvas.FillRect(ARect) ; if mnuBold.Checked then
ImgID := 1 {thumb up in the ImageList}
else
ImgID := 0; {thumb down in the ImageList} ImageList1.Draw(ACanvas,22,ARect.Top + 2,ImgID) ; ACanvas.Font.Style := [fsBold]; {user defined text drawing function:}
DrawItemText(45,ACanvas,ARect,'Bold') ;
end;