5

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...

  • 1
    The control shown on the Windows dialog box really is just a plain edit control. To get the same effect in your program, I think it's just a matter of getting the configuration right on the edit control and the page control. Also, to me, the phrase "simple PageControl" means you used the Delphi-provided TPageControl class, but that's not what you've shown in your picture. – Rob Kennedy Jul 27 '10 at 17:32
  • Yes I also suspect the windows dialog uses a plain edit control, but it's background happens to be single color, so that works. The DevEx TabControl I used behaves the same way as Delphi TPageControl, so it doesn't make a difference. As for "just a matter of getting the configuration right on the edit control and the page control", I don't think that's possible, hence this question. –  Jul 27 '10 at 18:29
  • Windows XP dialogs use a gradient, and the "selectable labels" (edit controls) on those dialogs correctly show the gradient, even while focused. So, it *is* possible. – Rob Kennedy Jul 27 '10 at 23:05

2 Answers2

8

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)

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • Have you tried it with a graded background? In my D2009 the edit uses the ParentColor, but the Color property of the Parent is just one of its gradient colors. (using TAdvPanel by TMS Software as the back drop for the edit). So the edit still shows with a solid color. – Marjan Venema Jul 27 '10 at 18:18
  • Set `ParentBackground = True` to tell a control to uses its parent's *themed* background. `ParentColor` is irrelevant when the parent doesn't even use its color. – Rob Kennedy Jul 27 '10 at 23:07
  • 1
    @Rob Kennedy: A TEdit doesn't not have a ParentBackground property. For other controls it's implemented as csParentBackground in ControlStyle, but that is not sufficient for a TEdit to become transparent –  Jul 28 '10 at 14:03
  • ParentBackground is protected in TEdit. – Rob Kennedy Jul 28 '10 at 15:55
  • 1
    Yes it's way up in TWinControl, but setting ParentBackground to true (IOW adding csParentBackground to ControlStyle) doesn't make a TEdit transparent. I Updated the question with a CN_CTLCOLORSTATIC handler which does make it transparent, but it has unwanted sideffects (or is not sufficient). –  Jul 28 '10 at 18:44
3

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.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • The class from the link you provided is indeed fully transparent (in XP at least), but only while it's not focused, once you focused the background is painted in a single color, but I'll investigate the source, maybe I can figure out something, thanks –  Jul 27 '10 at 17:04
  • @dmauric.mp: Good, but be careful: Some of your end-users might be running Windows Vista or Windows 7, and they might not be very happy... – Andreas Rejbrand Jul 27 '10 at 17:19
  • I modified the code so that it's now transparent when focused as well. I have also tried it in Xp, Vista and W7 (Aero) and it seems to work in all of them, care to explain what problems did you encounter ? –  Jul 27 '10 at 17:34
  • @dmauric.mp: Care to share your modifications? – Marjan Venema Jul 27 '10 at 18:22
  • @Marjan Venema: Nonthing that would affect W7 operation, it just had Ftransparent := false; in DoEnter, which I removed. But on closer inspection, I realized it doesn't really work, I just didn't notice the first time... sorry for the confusion –  Jul 27 '10 at 19:05