Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a selectable label control?

I'd like a selectable label control, like the one in the screenshot. It could be done with a borderless TEdit, but I was wondering if there is another way that would work with gradient background?

example http://usera.ImageCave.com/brk303/SelectableLabel.png.jpg

To clarify, I'm using a simple PageControl, which since Win XP has gradient drawing, a borderless TEdit placed on a page doesn't blend in perfectly:

Edit on a PageControl http://usera.ImageCave.com/brk303/Gradient.png.jpg

Update:

I managed to get half way there by adding

procedure CNCtlColorStatic(var AMsg: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;

procedure TTransparentEdit.CNCtlColorStatic(var AMsg: TWMCtlColorStatic);
begin
  with ThemeServices do
    if ThemesEnabled then
    begin
      SetBkMode(AMsg.ChildDC, Windows.TRANSPARENT);
      DrawParentBackground(Handle, AMsg.ChildDC, nil, False);
      AMsg.Result := GetStockObject(NULL_BRUSH);
    end
    else
      inherited;
end;

It's now transparent, but something else needs to be done, as painting when text is selected doesn't work properly. The behavior is hard to explain, I will investigate further and update here...

like image 373
Daniel Maurić Avatar asked Jul 27 '10 16:07

Daniel Maurić


2 Answers

Labels are not editable. TLabel can't even receive the focus, because it does not inherit from TWinControl.

I'd use a TEdit to mimic your screenshot:

object Edit1: TEdit
  BorderStyle = bsNone
  ParentColor = True
  ReadOnly = True
  Text = 'Editable label'
end

(you can copy-and-paste the above code to your form)

like image 158
Wouter van Nifterick Avatar answered Oct 04 '22 00:10

Wouter van Nifterick


The normal way is to use a borderless (BorderStyle := bsNone) and read-only (ReadOnly := true) TEdit, possibly combined with Color := clBtnFace, as you say. However, gradient backgrounds are not common, and there is no out-of-the-box support for such. However, it is not too difficult to do it yourself. I will try to find a simple solution within a few minutes.

Update

Drawing in Windows edit boxes is not trivial. Are you sure you need a gradient background? You could of course write your own control -- writing a TEdit-like control is not really that hard. I have done so a few times. (Proof)

Update 2

I havn't tried it myself, and it might not work with visual themes, but you could try to create a transparent `TEdit` control: http://www.delphi3000.com/articles/article_935.asp?SK=

Now I tried it, and it does not work at all under Windows 7 with Aero.

like image 39
Andreas Rejbrand Avatar answered Oct 03 '22 23:10

Andreas Rejbrand