Delphi7如何实现让Tedit显示文字垂直居中(上下居中)

通过下面的组件,可以在输入文字的时候自动垂直居中
直接把下面代码保存到Unit1.pas即可
------------------------------------------

unit Unit1;

interface

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

type
TEdit = class(StdCtrls.TEdit)
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyPress(var Key: Char); override;
procedure WMSize(var msg: TWMSize);message WM_SIZE;
procedure SetParent(AParent: TWinControl);override;
procedure SetCenter;
end;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
Edt: TEdit;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
{ TEdit }

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
//if Key = #13 then
// ShowMessage('输入了回车键');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Edt := TEdit.Create(self);
Edt.Parent := self;
Edt.AutoSize := False;
Edt.Height := 50;
end;

procedure TEdit.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or ES_MULTILINE;
end;

procedure TEdit.KeyPress(var Key: Char);
begin
inherited;
if Key = #13 then
key := #0;
end;

procedure TEdit.WMSize(var msg: TWMSize);
begin
inherited;
SetCenter;
end;

procedure TEdit.SetParent(AParent: TWinControl);
begin
inherited;
if Parent <> nil then
begin
SetCenter;
end;
end;

procedure TEdit.SetCenter;
var
DC: HDC;
SaveFont: HFont;
Sin: Integer;
SysMetrics, Metrics: TTextMetric;
Rct: TRect;
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
if Ctl3D then Sin := 8 else Sin := 6;
Rct := ClientRect;
Sin := Height - Metrics.tmHeight - Sin;
Rct.Top := Sin div 2;
SendMessage(Handle, EM_SETRECT, 0, Integer(@Rct));
end;




end.



当这个保存成unit1.pas 后,然后通过delphi组件安装功能来安装组件,具体安装方法可以到网上查方法



转至:http://bbs.2ccc.com/topic.asp?topicid=419284




本文地址:http://www.xszlo.com/article/2012-12-22/7648.html,转发请保留这个地址,谢谢
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值