Delphi中其它Unite中的数据需要在uses中进行引用,但若是循环引用的话需要其中一个在implementation下用uses引用。有关循环引用在下面代码中也提供了具体的解决方法。
下面的代码将生成一个Form,使用了一个TImageList控件,定义了7个图形按钮:
unit Demo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ImgList, Buttons, GetDemoFrm;
type
TForm1 = class(TForm)
pnl1: TPanel;
ilButttons: TImageList;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure SpeedButtonClick(Sender: TObject);
public
{ Public declarations }
getfrmdata: TButtonInfo;
SpBtnArr: array[0..6] of TSpeedButton;
procedure InitSpeedButton;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.InitSpeedButton;
const
strHints: array[0..6] of string =
('选择', '文档', '读文档', '写文档', '删除文档', '邮件', '拷贝文档');
var
i: Integer;
nTop, nLeft: Integer;
procedure SetBtnIco(index: Integer);
var btnIndex: Integer;
begin
case index of
0: btnIndex := 0; //选择
1: btnIndex := 37; 2: btnIndex := 17; 3: btnIndex := 15; 4: btnIndex := 16; 5: btnIndex := 18;
6: btnIndex := 19;
else
btnIndex := -1;
end;
ilButttons.GetBitmap(btnIndex, SpBtnArr[index].Glyph);
end;
begin
nTop := 8;
nLeft := 12;
for i := 0 to 6 do
begin
SpBtnArr[i] := TSpeedButton.Create(nil);
SpBtnArr[i].Height := 23;
SpBtnArr[i].Width := 36;
SpBtnArr[i].Left := nLeft;
SpBtnArr[i].Top := nTop;
if (i mod 1) = 0 then
begin
Inc(nTop, 20);
nLeft := 12;
end
else
Inc(nLeft, 40);
SpBtnArr[i].Flat := True;
SpBtnArr[i].GroupIndex := 1;
SetBtnIco(i);
SpBtnArr[i].Parent := pnl1;
SpBtnArr[i].ShowHint := True;
SpBtnArr[i].Hint := strHints[i];
SpBtnArr[i].OnClick := SpeedButtonClick;
SpBtnArr[i].Tag := i;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitSpeedButton;
end;
procedure TForm1.SpeedButtonClick(Sender: TObject);
var
i: Integer;
ChoicebtnId: Integer;
Flag: Boolean;
begin
Flag :=
(self.SpBtnArr[2].Down) or
(self.SpBtnArr[3].Down) or (self.SpBtnArr[4].Down) or (self.SpBtnArr[5].Down) or
(self.SpBtnArr[6].Down);
getfrmdata.getBtnInfo;
end;
end.
下面的Unit是用来获取Form中SpBtnArr这个TSpeedButtons的数组,根据其Down属性来决定对哪个按钮进行响应。在这里只是做了个简单的测试:
unit GetDemoFrm;
interface
uses
SysUtils, Classes, Windows, Graphics, Messages, StdCtrls, Forms,
Dialogs, Buttons;
type
TButtonInfo = class
private
bflag: Boolean;
public
procedure getBtnInfo;
property flag: Boolean write bflag;
end;
implementation
uses Types, mssbs, Demo; {为了避免循环引用错误,所在Demo在此处引入。}
procedure TButtonInfo.getBtnInfo;
var
i: integer;
ChoicebtnId: integer;
begin
ChoicebtnId := -1;
{Form1 是类TForm1的一个对象,由于引入了Demo,所以在这里可以直接调用其对象}
for i := 0 to length(Form1.SpBtnArr) - 1 do
begin
if Form1.SpBtnArr[i].Down then
begin
ChoicebtnId := i;
break;
end;
end;
case ChoicebtnId of
0: begin ShowMessage('选择:' + IntToStr(ChoicebtnId)); end;
1: begin ShowMessage('文档:' + IntToStr(ChoicebtnId)); end;
2: begin ShowMessage('读文档:' + IntToStr(ChoicebtnId)); end;
3: begin ShowMessage('写文档:' + IntToStr(ChoicebtnId)); end;
4: begin ShowMessage('删除文档:' + IntToStr(ChoicebtnId)); end;
5: begin ShowMessage('邮件:' + IntToStr(ChoicebtnId)); end;
6: begin ShowMessage('拷贝文档:' + IntToStr(ChoicebtnId)); end;
else
ShowMessage('error');
end;
end;
end.
上述完成后,就可运行程序,然后就可以看到我们想要的测试结果了。
例子很简单,可以帮助初学都加深对Delphi的了解,由于我也是在学习阶段,记录一下全当是学习笔记。希望能对其它初学者有所帮助。
附上源码下载地址http://download.csdn.net/detail/welcome000yy/4488816