如何在 Delphi7 中将编辑框的文本居中?
How to center a text of an edit box in Delphi7?
我目前正在使用 Delphi7 开发 BlackJack 应用程序,我试图将编辑框的文本居中,以便稍后显示牌面值。我找到了这份文档 (http://delphidabbler.com/tips/85),但现在我无法正确实施它。我将 link 中的代码放入 "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.
您实际上根本没有使用 TMyEdit
class。这就是为什么 Unit1
不能使用 Unit2
的功能。 Unit1
仍在使用标准 TEdit
。
你有两个选择:
将Unit2
移动到它自己注册TMyEdit
的包中,然后将该包安装到IDE中。 TMyEdit
将在设计时可用,您可以将 TEdit
控件替换为 TMyEdit
控件。
如果您不想走那条路,另一种方法是将 TMyEdit
重新声明为 TEdit
并保持 Unit1
不变。它将使用 uses
子句中声明的最后一个 TEdit
类型。这被称为 "interposer class",例如:
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.
我目前正在使用 Delphi7 开发 BlackJack 应用程序,我试图将编辑框的文本居中,以便稍后显示牌面值。我找到了这份文档 (http://delphidabbler.com/tips/85),但现在我无法正确实施它。我将 link 中的代码放入 "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.
您实际上根本没有使用 TMyEdit
class。这就是为什么 Unit1
不能使用 Unit2
的功能。 Unit1
仍在使用标准 TEdit
。
你有两个选择:
将
Unit2
移动到它自己注册TMyEdit
的包中,然后将该包安装到IDE中。TMyEdit
将在设计时可用,您可以将TEdit
控件替换为TMyEdit
控件。如果您不想走那条路,另一种方法是将
TMyEdit
重新声明为TEdit
并保持Unit1
不变。它将使用uses
子句中声明的最后一个TEdit
类型。这被称为 "interposer class",例如: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.