2007年2月24日星期六

Delphi中的UI组件控制

Delphi的VCL设计精妙,给现代许多Framework提供了良好的基础与开发思路。在
Delphi中,继承自TControl的UI组件可以在运行期进行遍历并能够修改其属性。比
如常见的选择框样例,某个选择框的点选决定了一个GroupBox中多个组件的状态。
我写了一个简单的Delphi函数来完成这个功能。
unit FuncComponentDisplay;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
ComCtrls, StdCtrls, ExtCtrls;


procedure SwitchGroupBoxState(CheckBox: TCheckBox; ParentObj: TControl );

implementation

{ TComponentDisplay }

procedure ChangeEditDisplay(aEdit: TEdit;
State: Boolean);
begin
aEdit.Enabled := State;
if State then begin
aEdit.Color := clWindow;
end
else begin
aEdit.Color := clBtnFace;
end;
end;

procedure ChangeButtonDisplay( aButton:TButton; State:Boolean );
begin
aButton.Enabled := State;
end;

procedure ChangeCheckBoxDisplay( aCheckBox:TCheckBox; State:Boolean );
begin
aCheckBox.Enabled := State;
end;

procedure ChangeRadioButtonDisplay( aRadioButton:TRadioButton;
State:Boolean );
begin
aRadioButton.Enabled := State;
end;

procedure ChangeProgressBarDisplay( aProgressBar:TProgressBar;
State:Boolean );
begin
aProgressBar.Enabled := State;
end;

procedure ChangeTrackBarDisplay( aTrackBar:TTrackBar; State:Boolean );
begin
aTrackBar.Enabled := State;
end;

procedure ChangeListBoxDisplay( aListBox:TListBox; State:Boolean );
begin
aListBox.Enabled := State;
if State then begin
aListBox.Color := clWindow;
end
else begin
aListBox.Color := clBtnFace;
end;
end;
procedure ChangeComboBoxDisplay( aComboBox:TComboBox; State:Boolean );
begin
aComboBox.Enabled := State;
end;

procedure ChangeMemoDisplay( aMemo:TMemo; State:Boolean );
begin
aMemo.Enabled := State;
if State then begin
aMemo.Color := clWindow;
end
else begin
aMemo.Color := clBtnFace;
end;
end;

procedure SwitchGroupBoxState(CheckBox: TCheckBox; ParentObj: TControl );
var
I: Integer;
TempObj : TComponent;
begin
for I := 0 to CheckBox.Owner.ComponentCount - 1 do begin // Iterate
TempObj := CheckBox.Owner.Components[I] ;
if (TempObj is TControl) then begin
if (TempObj as TControl).Parent = ParentObj then begin
if ( TempObj is TEdit ) then begin // TEdit State Switch
ChangeEditDisplay( (TempObj as TEdit), CheckBox.Checked );
continue;
end;
if ( TempObj is TButton ) then begin
ChangeButtonDisplay( (TempObj as TButton), CheckBox.Checked );
end;
if ( TempObj is TCheckBox ) then begin
ChangeCheckBoxDisplay( (TempObj as TCheckBox), CheckBox.Checked );
continue;
end;
if ( TempObj is TRadioButton ) then begin
ChangeRadioButtonDisplay( (TempObj as TRadioButton),
CheckBox.Checked);
continue;
end;
if ( TempObj is TProgressBar ) then begin
ChangeProgressBarDisplay( (TempObj as TProgressBar),
CheckBox.Checked);
continue;
end;
if ( TempObj is TTrackBar ) then begin
ChangeTrackBarDisplay( (TempObj as TTrackBar), CheckBox.Checked);
continue;
end;
if ( TempObj is TListBox ) then begin
ChangeListBoxDisplay( (TempObj as TListBox), CheckBox.Checked);
continue;
end;
if ( TempObj is TComboBox ) then begin
ChangeComboBoxDisplay( (TempObj as TComboBox), CheckBox.Checked);
continue;
end;
if ( TempObj is TMemo ) then begin
ChangeMemoDisplay( (TempObj as TMemo), CheckBox.Checked);
continue;
end;
end;
end;
end; // for
ParentObj.Enabled := CheckBox.Checked ;
end;

end.

其调用方法是:


procedure TForm1.CheckBox1Click(Sender: TObject);
begin
SwitchGroupBoxState( ( Sender as TCheckBox ), GroupBox1 );
end;

0 评论: