Delphi: paint column of list view - delphi

I draw a list view with OwnerDraw. I need to paint the first column. But I cannot understand how.
I tried:
procedure TFrame6.DownloadListCustomDraw(Sender: TCustomListView;
const ARect: TRect; var DefaultDraw: Boolean);
var
R: TRect;
begin
DefaultDraw := False;
Sender.Canvas.Brush.Color := $F7F7F7;
Sender.Canvas.Brush.Style := bsSolid;
R := ARect;
R.Right := ListView_GetColumnWidth(DownloadList.Handle, DownloadList.Columns[0].Index);
Sender.Canvas.FillRect(R);
DefaultDraw := True;
end;
But I draw over items. How to draw correctly, items and a background?
Thanks!

Summary from comments:
I suggest you to read this delphiDabbler article and hope that it contains enough information to resolve your problem. E.g. Example 1 shows how to change background and Example 4 shows point where item appearance can be changed.
Small tip: don't restore DefaultDraw to True at the end of the handler if you don't want text to be drawn.

I suggest you use VirtualStringTree if you want a lot of customization on the list. Its easy to use and almost anything is possible and most of all freeware. The component can be downloaded at Soft-Gems and few example can be found here

Related

How do I enlarge a TImage in Delphi when my cursor is over it?

I am using a VCL Application in Delphi. I have a TImage. I want to enlarge the TImage slightly when my cursor is over it. Does anybody know how to do this?
There are many ways.
Personally, I'd create a new custom control specifically for the purpose, so it can be reused.
But if you need a quick version, you can simply use the control's OnMouseEnter and OnMouseLeave events:
procedure TForm1.Image1MouseEnter(Sender: TObject);
var
R: TRect;
begin
R := Image1.BoundsRect;
R.Inflate(6, 6, 6, 6);
Image1.BoundsRect := R;
end;
procedure TForm1.Image1MouseLeave(Sender: TObject);
var
R: TRect;
begin
R := Image1.BoundsRect;
R.Inflate(-6, -6, -6, -6);
Image1.BoundsRect := R;
end;
This is actually more robust than you might think. For instance, if you place the cursor above the image, so that it is enlarged, and then press Ctrl+Alt+Del and move the cursor away, you will find that the image is restored to its original size when you get back to the desktop.

Add icons to TTabControl in Firemonkey

First of all I have to say that I've read this question of SO. But actually it wasn't helpful for me.
I want to add icons to TTabControl but it doesn't seems as easy as I can do it in VCL (TPageControl). As you know there is no something like Image Index in TTabControl.
So what's the easiest way to do this?
Thanks for your help.
I would suggest not going down the route of modifying the style given the inherent 'copy and paste "inheritance"' nature of the exercise, which becomes an issue if you're targeting more than one OS (even just Windows 7 and Windows 8.x). Instead, try this:
1) For each item you want an icon on, change its TextAlign property to taTrailing and pad its Text with four leading space characters.
2) Add one TImage to the form per tab, and load small bitmaps into them as desired.
3) Associate each tab item with its image by (for example) assigning its TagObject property to the image control in an OnCreate handler for the form:
procedure TForm1.FormCreate(Sender: TObject);
begin
TabItem1.TagObject := Image1;
//...
end;
4) Assign each tab item's OnPaint event the following shared event handler:
procedure TForm1.TabItemPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
var
B: TBitmap;
SrcR, DstR: TRectF;
TabItem: TTabItem;
begin
TabItem := (Sender as TTabItem);
B := (TabItem.TagObject as TImage).Bitmap;
SrcR := RectF(0, 0, B.Width, B.Height);
DstR := SrcR;
DstR.Fit(RectF(ARect.Left, ARect.Top, ARect.Left + ARect.Height, ARect.Bottom));
if not TabItem.IsSelected then DstR.Offset(0, 1);
Canvas.DrawBitmap(B, SrcR, DstR, 1);
end;

Take a screenshot of a particular area

I am using Lazarus and I have a TImage inside a form. The black table is a TImage and the numbers are labels. I need to take a screenshot of the red area I drew.
How can I perform this?
I have Lazarus 1.0.14 and I didn't find any example about this. Any suggestion?
This is a painful design, but well, one simple way might be to put all the controls on a common container and copy its canvas to a bitmap. The following example assumes, that you have put your image and all the labels on a common TPanel control (Panel1):
procedure TForm1.Button1Click(Sender: TObject);
var
R: TRect;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
R := Rect(0, 0, Panel1.Width, Panel1.Height);
Bitmap.SetSize(Panel1.Width, Panel1.Height);
Bitmap.Canvas.CopyRect(R, Panel1.Canvas, R);
Bitmap.SaveToFile('C:\Screenshot.bmp');
finally
Bitmap.Free;
end;
end;
You can use GetFormImage to get the form image, and keep the part that corresponds to your image area in it:
var
Bmp: TBitmap;
begin
Bmp := GetFormImage;
try
Bmp.Canvas.CopyRect(Image1.ClientRect, Bmp.Canvas, Image1.BoundsRect);
Bmp.SetSize(Image1.Width, Image1.Height);
Bmp.SaveToFile('....');
finally
Bmp.Free;
end;

Setting up background images for forms in Delphi

How can I make my program load an image and make it the background for a form?
I need the exact code for it. I've looked all over the internet and the only things I've found are various tweaks and fixes to make backgrounds work as intended in special circumstances. I've also tried some Delphi books I have and I can't find it anywhere.
Put a TImageon your form. Make sure it's behind all other controls on the form. You can right-click it and choose the "send to back" menu option.
Load a graphic.
var
img: TBitmap;
begin
img := TBitmap.Create;
try
img.LoadFromFile('S:\background.bmp');
Assign it to the image control.
Image1.Picture := img;
Clean up.
finally
img.Free;
end;
end;
You can also combine the last three steps to load the graphic and put it in the image control all at once. Thanks to Jon for the suggestion.
Image1.Picture.LoadFromFile('B:\background.bmp');
See also: How to add background images to Delphi forms
What I would do is use the forms OnPaint event, get the canvas (Form1.Canvas), and then use the Draw method (which takes an image) to draw the image you want. Something like the following:
procedure TForm1.FormPaint(Sender: TObject);
var
mypic: TBitMap;
begin
mypic := TBitMap.Create;
try
mypic.LoadFromFile('cant.bmp');
Form1.Canvas.Draw(0, 0, mypic);
finally
FreeAndNil(mypic);
end;
end;
Note that this could be extremely slow.
This is the way all my applications show a form image. I load the image at form creation or when the application calls a specific showing event
var
vDest, vRect: TRect;
begin
vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
vDest := Rect(0,0,Self.Width, Self.Height);
Canvas.StretchDraw(vDest, FBackgroundImage);
if FileExists(this) then
FBackgroundImage.LoadFromFile(this);
#Brendan
thanks
//from Brendan code;
var
vDest, vRect: TRect;
FBackgroundImage: TGraphic;
begin
FBackgroundImage := image1.Picture.Graphic; //LOAD from invisible image
vRect := Rect(0, 0, FBackgroundImage.Width, FBackgroundImage.Height);
vDest := Rect(0,0,Self.Width, Self.Height);
Canvas.StretchDraw(vDest, FBackgroundImage);
end;

Problem with adding graphics to TLabel

I'm trying to create with Delphi a component inherited from TLabel, with some custom graphics added to it on TLabel.Paint. I want the graphics to be on left side of text, so I overrode GetClientRect:
function TMyComponent.GetClientRect: TRect;
begin
result := inherited GetClientRect;
result.Left := 20;
end;
This solution has major problem I'd like to solve: It's not possible to click on the "graphics area" of the control, only label area. If the caption is empty string, it's not possible to select the component in designer by clicking it at all. Any ideas?
First excuse-me for my bad English.
I think it is not a good idea change the ClientRect of the component. This property is used for many internal methods and procedures so you can accidentally change the functionality/operation of that component.
I think that you can change the point to write the text (20 pixels in the DoDrawText procedure -for example-) and the component can respond on events in the graphic area.
procedure TGrlabel.DoDrawText(var Rect: TRect; Flags: Integer);
begin
Rect.Left := 20;
inherited;
end;
procedure TGrlabel.Paint;
begin
inherited;
Canvas.Brush.Color := clRed;
Canvas.Pen.Color := clRed;
Canvas.pen.Width := 3;
Canvas.MoveTo(5,5);
Canvas.LineTo(15,8);
end;
What methods/functionality are you getting from TLabel that you need this component to do?
Would you perhaps be better making a descendent of (say, TImage) and draw your text as part of it's paint method?
If it's really got to be a TLabel descendant (with all that this entails) then I think you'll be stuck with this design-time issue, as doesn't TLabel have this problem anyway when the caption is empty?
I'll be interested in the other answers you get! :-)

Resources