例子:
//Change the color of the selected text when the user choose a
//color from the Color dialog box
procedure TForm1.sbColorClick(Sender: TObject);
begin
Colordialog1.Execute;
RichEdit1.SelAttributes.Color:=Colordialog1.Color;
End;
例子:
procedure TForm1.sbColorClick(Sender: TObject);
var X:Integer;
begin
Colordialog1.CustomColers:= StrCustomColors;
Colordialog1.Execute;
RichEdit1.SelAttributes.Color:=Colordialog1.Color;
StrCustomColors:=Colordialog1.CustomColors;
//Save StrCustomColors to the registry or an INI file
//when the program exits…
end;
例子:
当按钮1被按击时,以下的代码加载了在屏幕上的所有的窗体名到ListBoxl上。
procedure TForm1.Button1Click(Sender : TObject);
var
I : integer;
begin
For I := 0 to Screen.FormCount – 1 do
ListBox1.Items.Add(Screen.Forms[I ].Name);
end;
Width属性
对应对象:TScreen
声明:property Width:Integer;
功能:Width属性说明了屏幕的宽度。这个属性是只读型的,包含有屏幕的水平尺寸,以象素来计数。示例:
with Screen do
for I := 0 to FormCount – 1 do
if Forms[I].Width > Width then Forms[I].Width := Width;
//Create an event handler for OnActiveControlChange
procedure TForm1.FormCreate(Sender : TObject);
begin
Screen.OnActiveControlChange := FocusChanged;
end;
//Respond to OnActiveControlChange events
procedure TForm1.FocusChanged(Sender : TObject);
begin
if Screen.ActiveControl = Button2 then
Button1.Enabled := False;
Else Button1.Enabled := True;
end;
end.
OnActiveFormChange事件
对应对象:TScreen
声明:TNotifyEvent = procedure(Sender:TObject) of object;
property OnActiveFormChange:TNotifyEvent;
功能:在激活的窗体被改变之前OnActiveFormChange事件发生。用户可以为OnActiveFormChange事件创建一个认为代码来实现在窗体改变之前希望完成的操作。
例子:
//Create a form with many controls,change the Interval property
//of the Timer to 500-every time a control is selected,the Form
//Caption changes to indicate which control has the foucus
procedure TForml.TimerlTimer(Sender:TObject);
begin
Form1.Caption := Screen.ActiveControl.Name;
end;
例子:
//Create a program with two forms-Show Form2 by
//clicking a button on the first form. This method will
//put the name of the active form on Form2’s caption
procedure TForm2.Timer1Timer(Sender : TObject);
begin
Form2.Caption := Screen.ActiveForm.Name;
end;
例子:
//Display all of the available fonts
procedure TForm1.FormCreate(Sender : TObject);
begin
ListBox1.Sorted := True;
ListBox1.Items := Screen.Fonts;
end;
例子:
type
Tform1=class(Tform)
Procedure MyMouseUp(Sender:Tobject;Button:TmouseButton;Shift:TshiftState;X,Y:Integer);
Private
{private declarations}
public
{public declarations}
end;
var
Form1:Tform1;
Implementation
{$R *.DFM}
procedure Tform1.FormCreate(Sender:Tobject);
begin
Form1.OnMouseUp:=Form1.MyMouseUp;
End;
Procedure Tform1.MymouseUp(Sender:Tobject;Button:TmouseButton;Shift:TshiftState;X,Y:Integer);
Begin
StatusBar1.Panels[0].Text:=’MouseUp on Form:’+IntToStr(X)+’,’+IntToStr(Y);
End;
OnStartDrag事件
对应对象:Tcontrol
声明:TstartDragEvent=procedure (Sender:Tobject;var DragObject:TdragObject) of object;
property OnStartDrag:TstartDragEvent;
功能:OnStartDrag事件在控件释放操作发生时点发生。当用户在一个控件上放置鼠标并且点击了按钮以便对控件进行拖动时,OnStartDrag事件将被触发。DragObject参数说明了被拖动的目标。用户可以为一个OnStartDrag事件的处理程序设置一个运行过程,也可以设计时,在大小探测器中为一个控件选择OnStartDrag事件来完成操作。
例子:
type
Tform1=class(Tform)
StatusBar1:Tstatusbar;
Procedure MyStartDrag(Sender:Tobject;var DragObject:TdragObject);
Private
{private decalrations}
public
{public declarations}
end;
var
Form1:Tform1;
Implementation
{$R *.DFM}
procedure Tform1.FormCreate(Sender:Tobject);
begin
Image1.OnStartDrag:=Form1.MyStartDrag;
End;
Procedure Tform1.MyStartDrag(Sender:Tobject;var DragObject:TdragObject);
Begin
StatusBar1.Panels[1].Text:=’Drag Started.’;
End;
OnDragOver事件
对应对象:Tcontrol
声明:TdragState=(dsDragEnter,dsDragLeave,dsDragMove);
TdragOverEvent=procedure(Sender,Source:Tobject;X,Y:Integer;State:TdragState;var:Boolean) of object;
功能:OnDragOver事件在一个控件上拖动对象时发生。
当用户在控件上拖动一个对象时,OnDragOver事件将发生。Sender说明了控件的标识符。Soruce参数说明被拖动的对象。坐标参数说明了被拖动对象在控件坐标上的位置。Accept参数说明了控件是否允许被拖动的对象放置在它的上面。如果Accept参数被设置为真,那么将使控件接受对象的放置。否则不接受。用户可以为一个OnDragOver事件的处理程序设置为一个运行过程,也可以在设计时,在大小探测器中为一个控件选择OnDragOver事件来完成操作。
例子:参见OnDragDrop事件的例子。
OnEnddrag事件
对应对象:Tcontrol
声明:TendDragEvent=procedure(Sender,Target:Tobject;X,Y:Integer) of object;
property OnEndDrag:TendDragEvent;
功能:OnEndDrag事件在一个拖动的操作停止时发生。
当控件接受一个被拖动的对象,或者用户取消了拖动操作时,用户可取消拖动操作。Sender参数是被拖动的对象。Target是鼠标对应的控件,当一个拖动的操作停止时,它将变为被拖动的对象。坐标是使用Target控件的坐标用来说明被拖动的对象在当前的位置。用户可以为一个OnEndDrag事件的处理程序设置一个运行过程,也可以在设计时,在大小探测器中为一个控件选择OnEndrag事件来完成操作。
例子:
type
Tform1=class(Tform)
Panel1:Tpanel;
Image1:Timage;
Procedure MyEndDrag(Sender,Target:Tobject;X,Y:Integer);
Private
{Private declarations}
public
{Public declarations}
end;
var
Form1:tForm1;
Implementation
{$R *.DFM}
procedure Tform1.FormCreate(Sender:Tobject);
begin
Image1.OnEndDrag:=Form1.MyEndDrag;
End;
Procedure Tform1.MyEndDrag(Sender,Target:Tobject;X,Y:Integer);
Begin
If Target is Tpanel then
StatusBar1.Panels[1].text:=’Drag ended on panel.’
Else
Statusbar1.panels[1].text:=’Drag did not end on panel.’;
End;
OnMouseDown事件
对应对象:Tcontrol
声明:TMouseButton=(mbLeft,mbRight,mbMiddle);
TshiftState=set of ssShift,ssAlt,ssCtrl,ssLeft,ssRight,ssMiddle,ssDouble);
TMouseEvent=procedure(Sender:Tobject;Button:TmouseButton;Shift:Tshiftstat e;X,Y:Integer) of object;
Property OnMouseDown:TmouseEvent;
功能:OnMouseDown事件当在控件上点击鼠标时激发。
Sender参数用来说明鼠标点击时,鼠标下面的那一个控件。Button参数说明了使用哪一个鼠标的按钮,此变量是属于TmouseButton类型,有效时为mbLeft,mbRight和mbMiddle。Shift参数是ssShift,ssAlt,ssCtrl,ssLeft,ssRight,ssMiddle,ssDouble这些参数的任意的组合,如果没有使用修正键,那么Shift将被设置成为[].坐标的参数使用了Sender控件坐标以指出点击时鼠标的位置。用户可以为一个OnMouseDown事件的处理程序设置一个运行过程,也可以设计时,在大小探测器中为一个控件选择OnMouseDown事件来完成操作。
例子:
type
Tform1=class(Tform)
Procedure MyMouseDown(Sender:Tobject;Button:TmouseButton:Shift;TshiftState;X,Y:Integer);
Private
{Private declarations}
public
{public declarations}
end;
var
Form1:Tform1;
Implementation
{$R *.DFM}
procedure Tform1.FormCreate(Sender:Tobject);
begin
Form1.MyMouseDown:=Form1.MyMouseDown;
End;
Procedure Tform1.MyMouseDown(Sender:Tobject;Button:TmouseButton;Shift:TSHiftState;X,Y:Integer);
Begin
StatusBar1.Panels[0].Text:=’MouseDown on Form:’+IntToStr(X)+’,’+IntToStr(Y);
End;
OnMouseMove事件
对应对象:Tcontrol
声明:TshiftState=set of(ssShift,ssAlt,ssCtrl,ssLeft,ssRight,ssMiddle,ssDouble);
TmouseMoveEvent=procedre(Sender:Tobject;Shift:TshiftState;X,Y:Integer) of object;
Property OnMouseMove:TmouseMoveEvent;
功能:OnMouseMove事件当鼠标指针在控件上移动时发生。
当鼠标指针通过每一个控件时,将连续地发生OnMouseMove事件。这个事件的发生将不需要鼠标的点击。Sender参数说明了发生事件的控件。Shift参数是ssShift,ssAlt,ssCtrl,ssLeft,ssRight,ssMiddle,ssDouble这些参数的任意的组合,如果没有使用修正键那么Shift将被设置成为[].在事件发生时坐标参数将使用控件的坐标系来说明鼠标的位置。用户可以为一个OnMouseMove事件的处理程序设置一个运行过程,也可以设计时,在大小探测器中为一个控件选择OnMouseMove事件来完成操作。
例子:
type
Tform1=class(Tform)
StatusBar1:TstatusBar;
Procedure MyMouseMove(Sender:Tobject;Shift:TshiftState;X,Y:Integer);
Private
{private declarations}
public
{public declarations}
end;
var
Form1:Tform1;
Implementation
{$R *.DFM}
procedure Tform1.FormCreate(Sender:Tobject);
begin
Form1.OnMouseMove:=Form1.MyMouseMove;
End;
Procedure Tfrom1.MyMouseMove(Sender:Tobject;Shift:TshiftState;X,Y:Integer);
Begin
StatusBar1.Panels[2].Text:=’MouseMove on Form:’+IntToStr(X)+’,’+IntToStr(Y);
End;
SendToBack方法
对应对象:Tcontrol
声明:procedure SendToBack;
功能:SendToBack方法将控件放置到容器中的所有其它控件的下面。窗口和非窗口控件有不同的Z轴的次序集合。一个窗体上的每一个窗口都在一个相同的Z轴的次序集合中,窗口控件总是出现在非窗口控件的上面,所以使用SendToBack方法将不能使一个非窗口控件上面的窗口控件放置到它的下面。
例子:
/Send the button behind the Panel
procedure Tform1.Button1Click(Sender:Tobject);
begin
Button1.SendToBack;
End;
SetBounds方法
对应对象:Tcontrol
声明:procedure SetBounds(Aleft,Atop,Awidth,Aheight:Integer);virtual;
功能:SetBounds方法用来设置控件的Left,Top,Width和Height属性。SetBounds方法使用用户可以在一次中设置控件边界的所有属性。这样可以使控件在一次重新绘制时就完成最后的效果,然而,一般的单独调用将使控件进行多次的重新绘制。
例子:
//Have the panel resize with the form
procedure Tform1.FormResize(Sender:Tobject);
begin
Panel1.SetBounds(10,10,Form1.ClientWidth-20,Form1.ClientHeight-20);
End;
SetTextBuf方法
对应对象:Tcontrol
声明:procedure SetTextBuf(Buffer:Pchar);
功能:SetTextBuf方法使一个缓冲区中的文字拷贝到一个控件中。
Buffer是一个指向以空字符结束的字符串。
例子:
procedure Tform1.Button2Click(Sender:TObject);
var MyBuffer:Pchar;
MyBufSize:Integer;
Begin
MyBufSize:=Button1.GetTextLen;
Button1.GetTextBuf(MyBuffer,MyBufSize);
StatusBar1.SetTextBuf(MyBuffer);
End;
SetZOrder方法
对应对象:Tcontrol
声明:procedure SetZOrder(TopMost:Boolean);dynamic;
声明:Trect=record
case Integer of
0:(Left,Top,Right,Bottom:Integer);
1:(TopLeft,BottomRight:Tpoint);
end;
procedure UpdateBoundsRect(const R:Trect);
功能:UpdateBoundsRect方法改变控件的Top,Left,Width和Height属性,但是没有更新控件的屏幕图像。
OnClick事件
对应对象:Tcontrol
声明:TnotifyEvent=procedure(Sender:Tobject) of object;
property:TnotifyEvent;
功能:OnClick事件在鼠标指针点击控件时发生。
当用户使用鼠标在控件上时点击鼠标按钮激发一个控件,或用户通过其它的操作触发OnClick事件。这个事件是应用程序使用最多的一个事件。用户可以为OnClick事件的处理程序设置为运行的过程,也可以在设计时,在大小探测器中为一个控件选择Onlick事件来完成操作。
OnDblClick事件
对应对象:Tcontrol
声明:TnotifyEvent=procedure(Sender:Tobject) of object;
property OnDblClick:TnotifyEvent;
功能:OnDblClick事件当用户双击控件时发生。在用户在一个控件上双击鼠标时将引发OnDblClick事件。用户可以为一个OnDblClick事件的处理程序设置为一个运行过程,也可以在设计时,在大小探测器中为一个控件选择OnDblClick事件来完成操作。
OnDragDrop事件
对应对象:Tcontrol
声明:TdragDropEvent=procedure(Sender,Source:Tobject;X,Y:Integer) of object;
property OnDragDrop:TdragDropEvent;
功能:OnDragDrop事件当一个被拖动的对象被放置到控件上时发生。Sender参数说明了将被放置对象的控件的标识符。Source参数说明了被拖动放置的对象。坐标参数说明了对象被放置到控件的位置。用户可以为一个OnDragDrop事件的处理程序设置为一个运行过程,也可以在设计时,在大小探测器中为一个控件选择OnDragDrop事件来完成操作。
例子:
type
Tform1=class(Tform)
Panel1:Tpanel;
Image1:Timage;
Procedure MyDragDrop(Sender,Source:Tobject;X,Y:Integer);
Procedure MyDragOver(Sender,Source:Tobject;X,Y:Integer;State:TdragState;var Accept:Boolean);
Private
{Private declarations}
Public
{Public declarations}
end;
var
Form1:Tform1;
Implementation
{$R *.DFM}
procedure Tform1.MyDragDrop(Sender,Source:Tobject;X,Y:Integer);
begin
if Soruce is Timage then
begin
TImage(Source).Parent:=Panel1;
TImage(Source).Left:=X;
Timage(Source).Top:=Y;
End;
End;
Procedure Tform1.MyDragOver(Sender,Source:Tobject;X,Y:Ingeger;State:TdragState; var Accept:Boolean);
Begin
if Soruce is Timage then begin
Timage(Source).DragCursor:=crCross;
Accept:=True;
End;
End;
Procedure Tform1.FormCreate(Sender:Tobject);
Begin
Panel1.OnDragDrop:=Form1.MyDragDrop;
Panel1.OnDragOver:=Form1.MyDragOver;
End;