TLinkLabel background on a TPageControl

こ雲淡風輕ζ 提交于 2019-12-05 16:49:11

问题


I am trying to use a TLinkLabel on a TPageControl, and I can't find a way to make it use it's parent's background.

// Image removed because the website doesn't exist any more 
// and I can't find it anywhere... Sorry.

As you can see, the parent tab sheet's lovely gradient is not preserved behind the link text.

I would like the functionality of having multiple links in a flowing block of text (the functionality that TLinkLabel provides) and have the background of the parent showing behind the text.

TLinkLabel does not have a ParentBackground property. I have tried creating a derived class that adds csParentBackground to the control style, to no avail:

TMyLinkLabel = class (TLinkLabel)
public
  constructor Create(AOwner: TComponent); override;
end;

...

constructor TMyLinkLabel.Create(AOwner: TComponent); 
begin
  inherited;
  ControlStyle := ControlStyle - [csOpaque] + [csParentBackground]
end;

Anyone have any ideas?


回答1:


Nat, you are nearly there with your changes to the ControlStyle of the TLinkLabel. What you have to do in addition is to make sure that the parent of the standard Windows static control (that's what the TLinkLabel is) handles the WM_CTLCOLORSTATIC message correctly.

The VCL has a nice redirection mechanism to let controls handle messages that are sent as notifications to their parent windows for themselves. Making use of this a completely self-contained transparent link label can be created:

type
  TTransparentLinkLabel = class(TLinkLabel)
  private
    procedure CNCtlColorStatic(var AMsg: TWMCtlColorStatic);
      message CN_CTLCOLORSTATIC;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TTransparentLinkLabel.Create(AOwner: TComponent);
begin
  inherited;
  ControlStyle := ControlStyle - [csOpaque] + [csParentBackground];
end;

procedure TTransparentLinkLabel.CNCtlColorStatic(var AMsg: TWMCtlColorStatic);
begin
  SetBkMode(AMsg.ChildDC, TRANSPARENT);
  AMsg.Result := GetStockObject(NULL_BRUSH);
end;



回答2:


Normally I hate it when people offer a third-party component as an answer, but I'll mention the TMS THTMLabel as an alternative for what you want to do. It has the Transparent property of the TLabel, and allows you to use HTML as the caption, and so you can do multiple links as per your example.




回答3:


The csParentBackground and csOpaque styles both require cooperation from other parts of the control's code. Merely setting them wouldn't have much effect; if it did, then the control would probably have a public Transparent property already.

You can look at TCustomLabel.Paint to see how it respects the csOpaque style. It checks for that style by reading its Transparent property before it paints its background:

if not Transparent then
begin
  Canvas.Brush.Color := Self.Color;
  Canvas.Brush.Style := bsSolid;
  FillRect(ClientRect);
end;

The csParentBackground style has no effect on TCustomLabel because that style only affects windowed controls; TCustomLabel descends from TGraphicControl, not TWinControl.

I don't have TLinkLabel, so I can't look at its source code to find out what it would need to change. If it's a TGraphicControl descendant, then it would need to include code like I showed above from TCustomLabel. If it descends from TWinControl, then I'd adapt code from TCustomStaticText instead. That's a little more complicated; it calls DrawParentBackground in response to the cn_CtlColorStatic notification message. It also doesn't paint itself in Delphi code. The control is a wrapper for the Win32 "static" control type.

TLinkLabel evidently paints its background unconditionally. To fix this, you'll need to override the Paint method. Removing functionality (background-painting, in this case) is hard to do with the traditional way of overriding virtual methods because you won't be able to call the inherited method to get all the text painted. Instead, You'll probably have to copy and paste the base class's implementation and then add the conditional parts in the middle somewhere.




回答4:


One way I can think of is to create helper class under implementation

type
  TLinkLabelHelper = class helper for TLinkLabel
  public
    procedure Add(const aBGColor: TColor; const S: string);
  end;

procedure TLinkLabelHelper.Add(const aBGColor: TColor; const S: string);
begin
  Color := aBGColor;
  Caption := S;
end;

Then, I create a public

procedure AfterConstruction; override;

procedure Form_A.AfterConstruction;
begin
  inherited;
  LinkLabel1.Add(Self.Color, 'Hello World');
end;

Hope this works.




回答5:


My advice: use simple TLabel. TLabel has a property named Transparent - this is what you need. Set your TLabels cursor to crHandPoint (AFAIR this is the link cursor), set font to blue underline, and write OnClick event handler, that will open web browser to navigate to the pointed url. You can even have one default event handler.

procedure OnClickOnMyLinkTLabels(Sender : TObject);
var
  Address : string;
begin
  if NOT (Sender is TLabel) then Exit;
  Address := (Sender as TLabel).Caption;
  ShellExecute(self.WindowHandle,'open',PChar(Address),nil,nil, SW_SHOWNORMAL);
end;

Edit:

If you do not want to have address in your caption, you can use Tag property to retrieve address and set caption to whatever you want:

procedure OnClickOnMyLinkTLabels(Sender : TObject);
var
  Address : string;
begin
  if NOT (Sender is TLabel) then Exit;
  Address :=  GetAddresByTag( (Sender as TLabel).Tag );
  ShellExecute(self.WindowHandle,'open',PChar(Address),nil,nil, SW_SHOWNORMAL);
end;

How you will implement GetAddresByTag is your choice. The most simple one is use an array of strings:

//in your form defintion
   private
     FAddresses : array of string;

function GetAddresByTag(id : integer): string;
begin
  if (i<Low(FAddresses)) OR (I> High(FAddresses)) then
    raise EXception.Create('wrong id sent!');
  Result:= FAddresses[id];
end;



回答6:


If your text is static, then you can still do this using labels. Lay out your entire text block INCLUDING the words you want as links. Set the label as transparent. Next, drop separate label components (also set to transparent) that will be the link. Change the color to clNavy, font style to fsunderline and the cursor to crHand. Then position the label OVER the existing text. Then write a onClick handler for each "link" label to perform your hot link.

While this is not optimal, it does work as long as you don't want to bold the text and are willing to keep the text the same font size. Of course this doesn't work so well if the block is dynamic, as you would have to calculate the position of the link labels in code, which is fairly complicated if you are using wordwrap. If not, you can use the canvas.textwidth and canvas.textheight methods to determine the necessary offset positions for your link labels.



来源:https://stackoverflow.com/questions/1345316/tlinklabel-background-on-a-tpagecontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!