⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settextalignu.pas

📁 DelphiWin32核心API参考光盘内容.是学习书籍中的源码,便于学习.
💻 PAS
字号:
unit SetTextAlignU;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    PaintBox1: TPaintBox;
    SpinEdit1: TSpinEdit;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    ColorGrid1: TColorGrid;
    Label8: TLabel;
    Label9: TLabel;
    procedure PaintBox1Paint(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure ColorGrid1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  HorzAlignmentValue: UINT;            // holds the horizontal alignment
  VertAlignmentValue: UINT;            // holds the vertical alignment
  IntercharacterSpacing: Integer;      // holds the intercharacter spacing

implementation

{$R *.DFM}

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  TextAlignment: UINT;   // holds the text alignment
begin
  {set the text alignment}
  SetTextAlign(PaintBox1.Canvas.Handle,
               HorzAlignmentValue or VertAlignmentValue);

  {set the intercharacter spacing}
  SetTextCharacterExtra(PaintBox1.Canvas.Handle, SpinEdit1.Value);

  {retrieve and display the current intercharacter spacing}
  Label7.Caption := IntToStr(GetTextCharacterExtra(PaintBox1.Canvas.Handle));

  {set the text color}
  SetTextColor(PaintBox1.Canvas.Handle, ColorGrid1.ForegroundColor);

  {retrieve and display the current text color}
  Label9.Caption := IntToHex(GetTextColor(PaintBox1.Canvas.Handle), 8);

  {draw some text (affected by alignment, spacing, and color) to
   the device context}
  TextOut(PaintBox1.Canvas.Handle, PaintBox1.Width div 2,
          PaintBox1.Height div 2, 'ABCabc', Length('ABCabc'));

  {retrieve the current text alignment}
  TextAlignment := GetTextAlign(PaintBox1.Canvas.Handle);

  {display the horizontal alignment}
  if (TextAlignment and (TA_LEFT or TA_CENTER or TA_RIGHT)) = TA_LEFT then
    Label2.Caption := 'Left';
  if (TextAlignment and (TA_LEFT or TA_CENTER or TA_RIGHT)) = TA_CENTER then
    Label2.Caption := 'Center';
  if (TextAlignment and (TA_LEFT or TA_CENTER or TA_RIGHT)) = TA_RIGHT then
    Label2.Caption := 'Right';

  {dislay the vertical alignment}
  if (TextAlignment and (TA_TOP or TA_BASELINE or TA_BOTTOM)) = TA_TOP then
    Label4.Caption := 'Top';
  if (TextAlignment and (TA_TOP or TA_BASELINE or TA_BOTTOM)) = TA_BASELINE then
    Label4.Caption := 'Baseline';
  if (TextAlignment and (TA_TOP or TA_BASELINE or TA_BOTTOM)) = TA_BOTTOM then
    Label4.Caption := 'Bottom';
end;


procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  {indicate the selected horizontal alignment}
  HorzAlignmentValue := 0;
  case TRadioButton(Sender).Tag of
    1: HorzAlignmentValue := TA_LEFT;
    2: HorzAlignmentValue := TA_CENTER;
    3: HorzAlignmentValue := TA_RIGHT;
  end;

  {refresh the screen}
  PaintBox1.Refresh;
end;

procedure TForm1.RadioButton4Click(Sender: TObject);
begin
  {indicate the selected vertical alignment}
  VertAlignmentValue := 0;
  case TRadioButton(Sender).Tag of
    1: VertAlignmentValue := TA_TOP;
    2: VertAlignmentValue := TA_BASELINE;
    3: VertAlignmentValue := TA_BOTTOM;
  end;

  {refresh the screen}
  PaintBox1.Refresh;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  {initialize the horizontal and vertical alignment values}
  HorzAlignmentValue := 0;
  VertAlignmentValue := 0;
  RadioButton1.Checked := TRUE;
  RadioButton4.Checked := TRUE;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
  {refresh the screen}
  PaintBox1.Refresh;
end;

procedure TForm1.ColorGrid1Change(Sender: TObject);
begin
  {refresh the screen}
  PaintBox1.Refresh;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -