制作Delphi TPanel字幕包

时间:2009-08-13 20:54:48

标签: delphi delphi-6 tpanel

有没有办法将文本包装在Delphi的TPanel.Caption中(在我的案例中是Delphi 6)?

2 个答案:

答案 0 :(得分:12)

不是默认情况下,我很害怕。正如您在TPanel的源代码中看到的那样,文本由DrawText()-windows api绘制:

procedure TCustomPanel.Paint;
{snip}
begin
  {snip}
  Flags := DT_EXPANDTABS or DT_SINGLELINE or
    VerticalAlignments[FVerticalAlignment] or Alignments[FAlignment];
  Flags := DrawTextBiDiModeFlags(Flags);
  DrawText(Handle, PChar(Caption), -1, Rect, Flags);
end;

您可以派生并覆盖Paint-method,也可以改为使用标签。

答案 1 :(得分:0)

这是实际工作的代码:

const
  Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
  VerticalAlignments: array[TVerticalAlignment] of Longint = (DT_TOP, DT_BOTTOM, DT_VCENTER);
var
  Rect: TRect;

   ... 
    
   if ShowCaption AND (Caption <> '') then
    begin
     Rect := GetClientRect;
     Canvas.Brush.Style := bsClear;
     Canvas.Font := Self.Font;
     Flags := DT_NOPREFIX OR
              DT_WORDBREAK OR
              VerticalAlignments[VerticalAlignment] OR
              Alignments[Alignment];
     Flags := DrawTextBiDiModeFlags(Flags);
     DrawText(Canvas.Handle, PChar(Caption), Length(Caption), Rect, Flags);
    end;