烦了我几天的问题,如何把Memo里的内容垂直居中 ( 积分: 100 )

  • 主题发起人 主题发起人 aries_jo
  • 开始时间 开始时间
A

aries_jo

Unregistered / Unconfirmed
GUEST, unregistred user!
注意:是垂直居中
 
注意:是垂直居中
 
你说的垂直居中我不是非常明白你的意思。
是不是这个?你去试一下
TMemo.Alignment:=taCenter;
这个是居中的选项。
 
TMemo.Alignment:=taCenter这个是顶上居中的,
我想中居中,你应该可以想象的到的
 
界面上放一个Memo1,一个button,双击一下button,然后用下面的代码覆盖整个代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
SetWindowLong(Memo1.handle, GWL_STYLE, GetWindowLong(Memo1.handle, GWL_STYLE) or ES_MULTILINE or ES_CENTER);//居中
Memo1.Invalidate;
end;

end.


dfm文件
object Form1: TForm1
Left = 192
Top = 107
Width = 348
Height = 295
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 80
Top = 36
Width = 185
Height = 89
ImeName = '中文 (简体) - 拼音加加3.11'
Lines.Strings = (
'Memo1'
''
'sdf'
'gsa'
'sadg'
'fds'
'fsda'
'f')
TabOrder = 0
end
object Button1: TButton
Left = 132
Top = 200
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end
 
上面的老大。按你上面来做。把整个代码覆盖。点击按钮里面的内容还是没反映
 
如何使TEDIT控件中的TEXT显示在控件的中间或下面(一般在上面),
像TLABEL中的LAYOUT至为CENTER一样
var
Rct: TRect;
begin
Rct := edit1.ClientRect;
Inc(Rct.Top, 3);
sendmessage(edit1.handle, EM_SETRECT, 0, Integer(@Rct));
end;
EM_SETRECT 只对于 Multiline Edit 有效? :-)
An application sends an EM_SETRECT message to set the formatting rectangle of a
multiline edit control.
换成 Memo 就可以了。
procedure TForm1.Button2Click(Sender: TObject);
var
Rct: TRect;
begin
SendMessage(Memo1.Handle, EM_GETRECT, 0, Integer(@Rct));
Inc(Rct.Top, 5);
SendMessage(Memo1.Handle, EM_SETRECT, 0, Integer(@Rct));
end;
那也简单, 那些语句前加一句
SetWindowLong(edit.Handle,GWL_STYLE, GetWindowLong(edit.Handle, GWL_STYLE) or ES_MULTILINE);
不过需要多写个OnKeyPress事件过滤掉输入的回车符.
最好自己继承TEdit做个有这功能的控件. 事实上很简单的.
unit JackEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TKeySet = (ksReturn, ksEscape);
TKeySets = set of TKeySet;
TJackEdit = class(TEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FKeys: TKeySets;
procedure SetAlignment(value: TAlignment);
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams);
override;
procedure KeyPress(var Key: Char);
override;
public
{ Public declarations }
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
{ Published declarations }
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property Keys: TKeySets read FKeys write FKeys;
end;

procedure Register;
implementation
procedure TJackEdit.SetAlignment(value: TAlignment);
begin
if value <> FAlignment then
begin
FAlignment := value;
RecreateWnd;
end;
end;

procedure TJackEdit.KeyPress(var Key: Char);
begin
if ksReturn in Keys then
begin
if Key = Chr(Vk_Return) then
begin
Key := Chr(0);
(Owner as TControl).Perform(wm_NextDlgCtl,0,0);
end;
end;
if ksEscape in Keys then
begin
if Key = Chr(Vk_Escape) then
begin
Key := Chr(0);
(Owner as TControl).Perform(wm_NextDlgCtl,1,0);
end;
end;
inherited KeyPress(Key);
end;

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

constructor TJackEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
end;

destructor TJackEdit.Destroy;
begin
inherited Destroy;
end;

procedure Register;
begin
RegisterComponents('Jack', [TJackEdit]);
end;

end.
 
没有象Label1里的layout属性的tlCenter,就直接中居中,
象上面的大哥只是移动到下面的位置。如果打印的话。是否也一样可以显示中居中
 
procedure TForm1.Button1Click(Sender: TObject);
var
r : TRect;
begin
r := Rect(50,50,120,120);
SendMessage(Memo1.Handle,EM_SETRECT,0,Integer(@r));
end;

自己根据需要设置这个Rect,
 
接受答案了.
 
后退
顶部