Советы по Delphi

         

Как сообщить какую-то глобальную переменную все (в т.ч. скрытым) окнам программы?


Своим опытом делится Олег Кулабухов:

Решением для такой задачи является рассылка пользовательского сообщения всем окнам массива Screen.Forms

    {Code for Unit1}

const
UM_MyGlobalMessage = WM_USER + 1;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;


procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
{ Private declarations }
private
procedure
UMMyGlobalMessage(var AMessage: TMessage); message
UM_MyGlobalMessage;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

uses Unit2;

procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show;
end;

procedure TForm1.UMMyGlobalMessage(var AMessage: TMessage);
begin
Label1.Left := AMessage.WParam;
Label1.Top := AMessage.LParam;
Form1.Caption := 'Got It!';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
f: integer;
begin
for
f := 0 to Screen.FormCount - 1 do
Screen.Forms[f].Perform(UM_MyGlobalMessage, 42, 42);
end;

{Code for Unit2}

const
UM_MyGlobalMessage = WM_USER + 1;

type
TForm2 = class(TForm)
Label1: TLabel;
private
{ Private declarations }
procedure UMMyGlobalMessage(var AMessage: TMessage); message
UM_MyGlobalMessage;
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.UMMyGlobalMessage(var AMessage: TMessage);
begin
Label1.Left := AMessage.WParam;
Label1.Top := AMessage.LParam;
Form2.Caption := 'Got It!';
end;

[001847]



Содержание раздела