如何在Delphi7中居中编辑框的文本?

时间:2016-12-07 15:33:02

标签: delphi delphi-7

我目前正在使用Delphi7中的BlackJack应用程序,我正在尝试将编辑框的文本居中,以便稍后显示卡片值。我找到了这个文档(http://delphidabbler.com/tips/85),现在我无法正确实现它。我将链接中的代码放入" Unit2"我现在正试图在" Unit1"的编辑框中调用这两个函数。对齐他们的文字。每当我尝试调用其中一个函数时,它会告诉我传递的参数不相同。 如果你们能够帮助我,我们将不胜感激。

以下是Unit1的取消:

 unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Unit2;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button10: TButton;
    Button4: TButton;
    Edit2: TEdit;
    Edit3: TEdit;

[...]

这里是Unit2的代码:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMyEdit = Class(TEdit)
  public
    FAlignment: TAlignment;
    procedure SetAlignment(Value: TAlignment);
    procedure CreateParams(var Params: TCreateParams); override;
    property Alignment: TAlignment read FAlignment write SetAlignment;
  end;

implementation

procedure TMyEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  case Alignment of
    taLeftJustify:
      Params.Style := Params.Style or ES_LEFT and not ES_MULTILINE;
    taRightJustify:
      Params.Style := Params.Style or ES_RIGHT and not ES_MULTILINE;
    taCenter:
      Params.Style := Params.Style or ES_CENTER and not ES_MULTILINE;
  end;
end;

procedure TMyEdit.SetAlignment(Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;
end.

1 个答案:

答案 0 :(得分:1)

您根本没有使用TMyEdit课程。这就是Unit1无法使用Unit2功能的原因。 Unit1仍在使用标准TEdit

您有两种选择:

  1. Unit2移动到自己的注册TMyEdit的程序包,然后将该程序包安装到IDE中。然后,TMyEdit会在设计时提供,您可以使用TEdit控件替换TMyEdit控件。

  2. 如果您不想使用该路线,则可以选择将TMyEdit重新声明为TEdit并按原样离开Unit1。它将使用TEdit子句中声明的最后uses类型。这被称为“插入者类”,例如:

    unit Unit2;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TEdit = Class(StdCtrls.TEdit)
      public
        FAlignment: TAlignment;
        procedure SetAlignment(Value: TAlignment);
        procedure CreateParams(var Params: TCreateParams); override;
        property Alignment: TAlignment read FAlignment write SetAlignment;
      end;
    
    implementation
    
    procedure TEdit.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      case Alignment of
        taLeftJustify:
          Params.Style := Params.Style or ES_LEFT and not ES_MULTILINE;
        taRightJustify:
          Params.Style := Params.Style or ES_RIGHT and not ES_MULTILINE;
        taCenter:
          Params.Style := Params.Style or ES_CENTER and not ES_MULTILINE;
      end;
    end;
    
    procedure TEdit.SetAlignment(Value: TAlignment);
    begin
      if FAlignment <> Value then
      begin
        FAlignment := Value;
        RecreateWnd;
      end;
    end;
    
    end.