views:

199

answers:

2

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

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

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  A: 

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
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
Daniel Maurić
@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
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 ?
Daniel Maurić
@dmauric.mp: Care to share your modifications?
Marjan Venema
@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
Daniel Maurić
+7  A: 

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
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
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
@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
Daniel Maurić
ParentBackground is protected in TEdit.
Rob Kennedy
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).
Daniel Maurić