自动调整大小的TCheckBox(如TLabel)

我想创建一个复选框,可以自动调整其宽度,就像TLabel一样。

UNIT cvCheckBox;
{  It incercepts CMTextChanged where it recomputes the new Width}
INTERFACE
USES
  Winapi.Windows,Winapi.Messages,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.StdCtrls;

TYPE
 TcCheckBox = class(TCheckBox)
 private
   FAutoSize: Boolean;
   procedure AdjustBounds;
   procedure setautoSize(b: Boolean);  reintroduce;
   procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
   procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
 protected
    procedure Loaded; override;
 public
    constructor Create(AOwner: TComponent); override;
 published
    //property Caption read GetText write SetText;
    property AutoSize: Boolean read FAutoSize write setautoSize stored TRUE;
 end;

IMPLEMENTATION

CONST
  SysCheckWidth: Integer = 21;  // In theory this can be obtained from the "system"

constructor TcCheckBox.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  FAutoSize:= TRUE;
end;


procedure TcCheckBox.AdjustBounds;
VAR
   DC: HDC;
   Canvas: TCanvas;
begin
  if not (csReading in ComponentState) and FAutoSize then
  begin
    // this caused the problem [solution provided by Dima] 
    if HandleAllocated then   // Deals with the missing parent during Creation
    begin
     // We need a canvas but this control has none. So we need to "produce" one.
     Canvas := TCanvas.Create;
     DC     := GetDC(Handle);
     TRY
       Canvas.Handle := DC;
       Canvas.Font   := Font;
       Width := Canvas.TextWidth(Caption) + SysCheckWidth + 4;
       Canvas.Handle := 0;
     FINALLY
       ReleaseDC(Handle,DC);
       Canvas.Free;
     END;
    end;
  end;
end;


procedure TcCheckBox.setautoSize(b: Boolean);
begin
  if FAutoSize <> b then
  begin
    FAutoSize := b;
    if b then AdjustBounds;
  end;
end;

procedure TcCheckBox.CMTextChanged(var Message:TMessage);
begin
  Invalidate;
  AdjustBounds;
end;


procedure TcCheckBox.CMFontChanged(var Message:TMessage);
begin
  inherited;
  if AutoSize
  then AdjustBounds;
end;

procedure TcCheckBox.Loaded;
begin
  inherited Loaded;
  AdjustBounds;
end;
end.

但是我有一个问题。 PageControl的非活动选项卡中放置的复选框不会自动重新计算其大小。换句话说,如果我有两个包含复选框的选项卡,则在应用程序启动时,将仅正确调整当前打开的选项卡中的复选框的大小。当我单击另一个选项卡时,该复选框将具有原始大小(设计时设置的大小)。

我确实在程序启动时设置了整个表单的字体大小(在Form Create之后,使用PostMessage(Self.Handle,MSG_LateInitialize))。

procedure TForm5.FormCreate(Sender: TObject);
begin
 PostMessage(Self.Handle,MSG_LateInitialize,0);  
end;

procedure TForm5.LateInitialize(var message: TMessage);
begin
 Font:= 22;
end;

为什么未激活的选项卡中的复选框未宣布字体已更改?

lizhc 回答:自动调整大小的TCheckBox(如TLabel)

正如我在对该问题的评论中所述,问题在于TPageControl仅初始化当前选择的页面。这意味着其他页面将没有有效的句柄。因此,放置在其上的所有组件也没有手柄。这就是AdjustBounds方法根本不起作用的原因。

但是可以通过以其他方式使用常量DeviceContext获取HWND_DESKTOP来解决这种不良情况(有关详细信息,请参见更新部分)。
请参见下面的代码:

procedure TcCheckBox.AdjustBounds;
var
  DC: HDC;
  Canvas: TCanvas;
begin
  if not (csReading in ComponentState) and FAutoSize then
  begin
    // Retrieve DC for the entire screen
    DC := GetDC(HWND_DESKTOP);
    try
      // We need a canvas but this control has none. So we need to "produce" one.
      Canvas := TCanvas.Create;
      try
        Canvas.Handle := DC;
        Canvas.Font := Font;
        Width := Canvas.TextWidth(Caption) + SysCheckWidth + 4;
        Canvas.Handle := 0;
      finally
        Canvas.Free;
      end;
    finally
      ReleaseDC(HWND_DESKTOP,DC);
    end;
  end;
end;

更新
由于已经发布了一些有用的注释,因此我更改了代码以摆脱对GetDesktopWindow函数的调用。相反,代码使用HWND_DESKTOP常量,该常量传递给GetDC函数可以在整个屏幕上获取DeviceContext

本文链接:https://www.f2er.com/3006532.html

大家都在问