我写了一个控件,给edit加了个左对齐,右对齐属性,听我慢慢道来。先谢谢各位。(100分)

  • 主题发起人 主题发起人 雪飞
  • 开始时间 开始时间

雪飞

Unregistered / Unconfirmed
GUEST, unregistred user!
unit myedit;

interface

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

type
TCustomREdit = class(TCustomEdit)
private
{ Private declarations }
FAlignment:TAlignment;
procedure SetAlignment(Value: TAlignment);
protected
{ Protected declarations }
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
published
{ Published declarations }
end;

TmyEdit = class(TCustomREdit)
published
{ Published declarations }
property Alignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure TCustomREdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;

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

procedure Register;
begin
RegisterComponents('YTdpk', [TmyEdit]);
end;

end.


我写了一个控件,代码如上.给edit加了个左对齐,右对齐属性,
我想当光标移到edit上时它的内容,左对齐,移出时右对齐。

在onenter, onexit分别写,
procedure TForm1.myEdit1Enter(Sender: TObject);
begin

myEdit1.Alignment:=taLeftJustify;
end;

procedure TForm1.myEdit1Exit(Sender: TObject);
begin
myedit1.Alignment:=taRightJustify
end;
问题1: 虽然可以实现功能。但不见光标(我指的是在打字后的那个标)。
问题2:怎样给上面的程序加一个开关,如果属性设为True,
这个edit就可以实现光标移进去的时候左对齐,
移出时右对齐.

谢谢
 
各位大侠,帮帮忙!
 
光标是可以用API设置的, 我没用过, 没记得在哪里, 你找找看有这个函数,
添加属性的办法与Alignment属性差不多呀,
 
第一个问题,没安装你的控件,不太清楚,第二个应该用判断FOCUS方法解决
我想应该写在MOUSEMOVE中吧(没试过)
 
不好意思。 因为据win32.hlp说的以及我做过的经验, 只有ES_MULTILINE时才能居中/居右。
你不加这个都能实现, 我实在答不出。
 
是不是没有希望了。
我上面的代码真的可以实现左右对齐, 

但我把
在onenter, onexit分别写,
procedure TForm1.myEdit1Enter(Sender: TObject);
begin

myEdit1.Alignment:=taLeftJustify;
end;

procedure TForm1.myEdit1Exit(Sender: TObject);
begin
myedit1.Alignment:=taRightJustify
end;
问题1: 虽然可以实现功能。但不见光标(我指的是在打字后的那个标)。
问题2:怎样给上面的程序加一个开关,如果属性设为True,
这个edit就可以实现光标移进去的时候左对齐,
移出时右对齐.
 
1. 试一下:
procedure TForm1.Edit1Enter(Sender: TObject);
begin
Edit1.SetFocus;
Edit1.SelectAll;
Edit1.SelStart := Length(Edit1.Text);
end;

2. 加一个Published的变量(也就是控件的属性)
 
2.
TCustomREdit = class(TCustomEdit)
private
FAutoAlignment: boolean;
...
published
{ Published declarations }
...
AutoAlignment: Cardinal read FAutoAlignment write SetAutoAlignment default true;
end;

procedure TCustomREdit.SetAutoAlignment(Value: boolean);
begin
FAutoAlignment := Value;
if FAutoAlignment then ...
end;


 
这样写没通过。

procedure TForm1.myEdit1Enter(Sender: TObject);
begin
myEdit1.Alignment:=taLeftJustify;
myEdit1.SetFocus;
myEdit1.SelectAll;
myEdit1.SelStart := Length(myEdit1.Text);

end;
 
1. 我说的是windows.SetFocus(myEdit1.Handle);
2. 你不妨加上ES_MULTILINE试试(只要在KeyPress中过滤掉#13效果和单行编辑一样)。
 
请告知运行环境。
因为我知道在98, 2000下从来不会出现你说的这种问题。
如果一切无效, 那么尝试不要用RecreateWnd, 而用SetWindowLong直接修改ES_XXX
 
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment] or ES_MULTILINE;
 
第一个问题:好象和你说的不一样,我这边正常
第二个问题 发布 一个TRUE FALSE属性,和后面干一样的事
procedure TForm1.myEdit1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
myEdit1.Alignment:=taLeftJustify;
end;
 
楼上的,我要移开又回到
tarightjustify,那就不成了。对吧

to Another_eYes
我把这个
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment] or ES_MULTILINE;
加了了,
在程序里

procedure TForm1.myEdit1Enter(Sender: TObject);
begin
myedit1.Alignment:=taLeftJustify;
windows.setfocus(myedit1.Handle);
end;

procedure TForm1.myEdit1Exit(Sender: TObject);
begin
myedit1.Alignment:=tarightJustify;
end;

不成功。错在哪里。
 
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));

if FAlignment <> Value then
begin
FAlignment := Value;
if HandleAllocated then
SetWindowLong(Handle, GWL_STYLY, (GetWindowLong(Handle, GWL_STYLE) and not (ES_LEFT or ES_CENTER or ES_RIGHT)) or Alignments[UseRightToLeftAlignment, FAlignment]);
end;
 
procedure TCustomREdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;

procedure TCustomRedit.SetAlignment(Value:TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
if HandleAllocated then
SetWindowLong(Handle, GWL_STYLY, (GetWindowLong(Handle, GWL_STYLE) and not (ES_LEFT or ES_CENTER or ES_RIGHT)) or Alignments[UseRightToLeftAlignment, FAlignment]);
end; //上面这句出错
[Error] myedit.pas(103): Undeclared identifier: 'GWL_STYLY'
 
SetWindowLong(Handle, GWL_STYLe, (GetWindowLong(Handle, GWL_STYLE) and not (ES_LEFT or ES_CENTER or ES_RIGHT)) or Alignments[UseRightToLeftAlignment, FAlignment]);
[Error] myedit.pas(103): Array type required
 
倒。
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
没看到?
 
晕,呵呵Another_eYes的方法可以
 
procedure TCustomREdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;

procedure TCustomRedit.SetAlignment(Value:TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
if HandleAllocated then
SetWindowLong(Handle, GWL_STYLe, (GetWindowLong(Handle, GWL_STYLE) and not (ES_LEFT or ES_CENTER or ES_RIGHT)) or Alignments[UseRightToLeftAlignment, FAlignment]);
end;//上面这一行
end;
[Error] myedit.pas(103): Undeclared identifier: 'Alignments'

还是出现上面的错误
 
后退
顶部