Советы по Delphi. Версия 1.4.3 от 1.1.2001
Шрифт:
for i := 0 to ComponentCount - 1 do
if Components[i] is TCheckBox then
(Components[i] as TCheckBox).Checked then begin
... сюда поместите ваш код ...
end;
end;
Кроме того, следующий код Delphi абсолютно корректен:
if Components[i] = CheckBox5 then
Чтотоделаем;
Также, каждый компонент в Delphi имеет опубликованное (Published) свойство с именем 'Tag', значение которого вы можете задавать во время создания компонента, и затем, во время выполнения приложения, обращаться к нему для получения доступа к компоненту:
var i : Integer
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TCheckBox then
with (Components[i] as TCheckBox) do
Case Tag of
1 : if Checked then DoSomethingOnBox1;
2 : if Checked then DoSomethingOnBox2;
… другое …
end;
end;
Для получения дополнительной информации, обратитесь к справке Delphi с ключевым словом «ComponentCount».
BitBtn
Кнопка с несколькими строчками текста III
Вот полный код проекта, создающего на кнопке во время выполнения две строчки текста.
program TwolnBtn;
uses Forms,TwolnBtu in 'TWOLNBTU.PAS' {Form1};
{$R *.RES}
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Файл TWOLNBTU.TXT → TWOLNBTU.DFM
object Form1: TForm1
Left = 202
Top = 98
Width = 320
Height = 176
Caption = 'Form1'
Font.Color = clRed
Font.Height = -12
Font.Name = 'Arial'
Font.Style = [fsBold]
PixelsPerInch = 96
OnActivate = ChgSpeedButton
OnCreate = ChgBitBtn
TextHeight = 15
object SpeedButton1: TSpeedButton
Left = 144
Top = 24
Width = 65
Height = 45
Caption = 'Это
двустрочный заголовок'
OnClick = ChgSpeedButton
end
object
BitBtn1: TBitBtn
Left = 32
Top = 24
Width = 69
Height = 37
Caption = 'Прерывание работы программы'
TabOrder = 0
OnClick = BitBtn1Click
end
end
Файл TWOLNBTU.PAS
unit Twolnbtu;
interface
uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;
type TForm1 = class(TForm)
BitBtn1: TBitBtn;
SpeedButton1: TSpeedButton;
procedure ChgBitBtn(Sender: TObject);
procedure ChgSpeedButton(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ChgBitBtn(Sender: TObject);
VAR
R : TRect;
N : Integer;
Buff : ARRAY[0..255] OF Char;
BEGIN
WITH BitBtn1 DO BEGIN
Glyph.Canvas.Font := Self.Font;
Glyph.Width := Width-6;
Glyph.Height := Height-6;
R := Bounds(0,0,Glyph.Width,0);
StrPCopy(Buff, Caption);
Caption := '';
DrawText(Glyph.Canvas.Handle, Buff, StrLen(Buff), R, DT_CENTER OR DT_WORDBREAK OR DT_CALCRECT);
Поделиться с друзьями: