Delphi状态栏控件应用一例

软件世界

在一些图形处理和建筑CAD软件中,常常可以看到窗体界面的状态栏中的top、left、button、righ会随着鼠标的移动而改变其中的数值,始终保持记录鼠标的位置。其实,这个实现起来很简单,下面就让我们来实现这一功能。
打开Delphi应用程序,新建一窗体,在控件面板第三页的win32中找到statusbar控件,拖放到窗体的底部,调整好大小,在其属性面板中找到panels,点击右面的小工具图标,打开editing statusbar panels小界面,用add new命令添加panels[0]、panels[1]、panels[2]、panels[3]四个项,并且在其text属性中命名为top、left、button、righ。添加以下代码,其功能是在窗体中画矩形,并且,其状态栏记录鼠标的位置,非常简单,在此不再赘述。
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
private
{Private declarations}
public
{Public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
startx,starty:integer;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
startx:=x;
starty:=y;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
form1.Canvas.Rectangle(startx,starty,x,y);
statusbar1.panels[0].text:='';
statusbar1.panels[1].text:='';
statusbar1.panels[2].text:='';
statusbar1.panels[3].text:='';
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
//form1.Canvas.Rectangle(startx,starty,x,y);
if y>starty then
begin
statusbar1.panels[0].text:='Top:'+intTostr(starty);
statusbar1.panels[2].Text:='botton:'+inttostr(y);
end
else
begin
statusbar1.panels[0].text:='top:'+inttostr(y);
statusbar1.panels[2].text:='botton:'+inttostr(starty);
end;
if x>startx then
begin
statusbar1.panels[1].text:='left:'+inttostr(startx);
statusbar1.panels[3].text:='right:'+inttostr(x);
end
else
begin
statusbar1.panels[1].text:='left:'+inttostr(x);
statusbar1.panels[3].text:='right:'+inttostr(startx);
end;
end;
end.
注:本程序在Win98、Delphi6中运行通过。