Autor Beitrag
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Fr 05.08.05 11:46 
Wie kann man an alle laufenden Programme eine Message verschicken?

BenBE sagte mir über ICQ den Befehl SendBroadcastMessage, allerdings kennt Delphi und MSDN nicht. Wie kann man es dann machen, wenn man nicht unbedingt von allen Programmen ein Habdle verarbeiten will?
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Fr 05.08.05 12:06 
Ich habe jetzt hier die Prozedur SendBroadcastMessage gefunden, allerdings für Kylix.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
type
  TPrivateMessage = record
    ID: cardinal;
    Handled: boolean;
    pData: pointer;
  end; // TPrivateMessage

  function SendBroadcastMessage(Target: TObject; MsgID: cardinal; pData: pointer = nil): boolean;

implementation

function SendBroadcastMessage(Target: TObject; MsgID: cardinal; pData: pointer = nil): boolean;
var
  Msg: TPrivateMessage;
begin
  Result := FALSE;
  if Target = nil then EXIT;
  Msg.ID := MsgID;
  Msg.Handled := FALSE;
  Msg.pData := pData;
  Target.Dispatch(Msg);
  if not Msg.Handled then // control didn't process it itself
    if Target is TWidgetControl then
      TWidgetControl(Target).Broadcast(Msg);
  Result := Msg.Handled;
end; // SendBroadcastMessage


Diese habe ich jetzt an Windows angepasst:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
program Project1;

uses
  Forms, Windows, Controls,
  Unit1 in 'Unit1.pas' {Form1},
  Fortschritt_Unit in 'Fortschritt_Unit.pas' {Form2},
  Einstellungen_Unit in 'Einstellungen_Unit.pas' {Form3};

{$R *.res}

var mHandle: THandle;

function SendBroadcastMessage(Target: TObject; MsgID: cardinal; pData: pointer = nil): boolean;
type
  TPrivateMessage = record
    ID: cardinal;
    Handled: boolean;
    pData: pointer;
  end;
var
  Msg: TPrivateMessage;
begin
  Result := FALSE;
  if Target = nil then EXIT;
  Msg.ID := MsgID;
  Msg.Handled := FALSE;
  Msg.pData := pData;
  Target.Dispatch(Msg);
  if not Msg.Handled then // control didn't process it itself
    if Target is TWinControl then
      TWinControl(Target).Broadcast(Msg);
  Result := Msg.Handled;
end;

begin
  mHandle:=CreateMutex(nil, True, PChar('MP3-Tag-Editor'));
  if GetLastError=ERROR_ALREADY_EXISTS then
   begin
    SendBroadcastMessage(Application, 2763);
    Halt
   end
  else
   begin
    Application.Initialize;
    Application.Title:='MP3-Tag-Editor';
    Application.CreateForm(TForm1, Form1);
    Application.CreateForm(TForm2, Form2);
    Application.CreateForm(TForm3, Form3);
    Application.Run
   end;
  if mHandle<>0 then
   begin
    CloseHandle(mHandle);
   end;
end.


Allerdings schickt er die nicht ab, da Application nicht vom Typ TWinControl zu sein scheint. Was muss ich an dessen stelle an die Funktion übergeben?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 05.08.05 15:50 
Nimm als Handle HWND_BROADCAST, dann wird die Nachricht mit SendMessage an alle toplevel Fenster geschickt.
Heiko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Sa 06.08.05 08:57 
Du meinst also so hier:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
program Project1;

uses
  Forms, Windows, Controls,
  Unit1 in 'Unit1.pas' {Form1},
  Fortschritt_Unit in 'Fortschritt_Unit.pas' {Form2},
  Einstellungen_Unit in 'Einstellungen_Unit.pas' {Form3};

{$R *.res}

var mHandle: THandle;

begin
  mHandle:=CreateMutex(nil, True, PChar('MP3-Tag-Editor'));
  if GetLastError=ERROR_ALREADY_EXISTS then
   begin
    SendMessage(HWND_BROADCAST, 2763, 0, 0);
    Halt
   end
  else
   begin
    Application.Initialize;
    Application.Title:='MP3-Tag-Editor';
    Application.CreateForm(TForm1, Form1);
    Application.CreateForm(TForm2, Form2);
    Application.CreateForm(TForm3, Form3);
    Application.Run
   end;
  if mHandle<>0 then
   begin
    CloseHandle(mHandle);
   end;
end.


Allerdings reagiert er auch hier nicht auf die Message(falls eine wirklich an die Anwendung geschickt wird):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
type
  TForm1 = class(TForm)
    procedure WM_XYZ(var Msg: TMsg); message 2763;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

procedure TForm1.WM_XYZ(var Msg: TMsg);
begin
 ShowMessage('');
end;