2005. szeptember 15., csütörtök

How to create a custom TShape with a caption


Problem/Question/Abstract:

I'd like to read text from a Unicode text file, but don't know how to do this. It looks like ReadLn only works with single-byte character sets.

Answer:

Here is how you can add a caption:

unit SampleShape;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls;

type
  TSampleShape = class(TShape)
  private
    { Private declarations }
  protected
    { Protected declarations }
    procedure Paint; override;
    procedure CMFontChanged(var Msg: TMessage); message CM_FONTCHANGED;
    procedure CMTextChanged(var Msg: TMessage); message CM_TEXTCHANGED;
  public
    { Public declarations }
  published
    { Published declarations }
    property Caption;
    property Font;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TSampleShape]);
end;

procedure TSampleShape.CMFontChanged(var Msg: TMessage);
begin
  inherited;
  Invalidate;
end;

procedure TSampleShape.CMTextChanged(var Msg: TMessage);
begin
  inherited;
  Invalidate;
end;

procedure TSampleShape.Paint;
var
  R: TRect;
begin
  inherited;
  Canvas.Font.Assign(Font);
  R := ClientRect;
  DrawText(Canvas.Handle, PChar(Caption), -1, R, DT_VCENTER or
    DT_CENTER or DT_SINGLELINE);
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése