Autor Beitrag
Wonko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 69



BeitragVerfasst: Do 10.04.08 09:48 
Hallo,

in meiner Anwendung soll ein PopupMenu, dessen Einträge RadioItems sind, mit einer RadioGroup verknüpft werden. Die RadioGroup hat also das selbe OnClick-Ereignis wie die Menüeinträge. Wenn das PopupMenu durch Klicken auf das Tray-Icon der Anwendung geöffnet wird, funktioniert das ganze aber nicht: manchmal wird der entsprechende Eintrag selektiert, manchmal ein anderer, manchmal ist das ganze ohne Wirkung.
Wenn ich das PopupMenu über einen Knopf auf der Form öffne, funktioniert alles einwandfrei.
Es muss also irgendwie mit dem Tray-Icon zu tun haben, dadurch wird leider das Debuggen recht unbequem.
Hier der Quellcode eines Minimalbeispiels: vielleicht kann mal jemand drüberschauen und mir sagen, wo der Fehler liegt.
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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus, ExtCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    PopupMenu1: TPopupMenu;
    item0: TMenuItem;
    item1: TMenuItem;
    item2: TMenuItem;
    item3: TMenuItem;
    Button1: TButton;
    Label1: TLabel;
    procedure RadioGroup1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);    
    procedure Wiederherstellen;
  private
    { Private declarations }
    procedure MinimizeToTNA(Sender: TObject);
  public
    { Public declarations }
  protected
    procedure WndProc(var Message: TMessage); override;
  end;

const
  WM_TRAYMSG = WM_USER + 10;

var
  Form1: TForm1;
  NID : TNotifyIconData = (
    cbsize           : sizeof(TNotifyIconData);
    uID              : 0;
    uFlags           : NIF_MESSAGE + NIF_ICON + NIF_TIP;
    uCallbackMessage : WM_TRAYMSG;
    szTip            : 'Hint des Tray-Icons';
    );

implementation

{$R *.dfm}

procedure TForm1.MinimizeToTNA(Sender: TObject);
begin
  if(Shell_NotifyIcon(NIM_ADD,@NID)) then
    ShowWindow(Application.Handle,SW_HIDE);
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
var
  freq: Integer;
begin
  if Sender <> RadioGroup1 then
    freq := StrToInt(TMenuItem(Sender).Name[5])
  else
    freq := RadioGroup1.ItemIndex;
  Label1.Caption:=IntToStr(freq);
  case freq of
    0:  item0.Checked := true;
    1:  item1.Checked := true;
    2:  item2.Checked := true;
    3:  item3.Checked:=true;
  end;
  RadioGroup1.ItemIndex:=freq;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  P: TPoint;
begin
  GetCursorPos(P);
  SetForegroundWindow(Self.Handle);
  PopupMenu1.Popup(P.X, P.Y);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  NID.Wnd := self.Handle;
  NID.hIcon := Application.Icon.Handle;
  Application.OnMinimize := MinimizeToTNA;
end;

procedure TForm1.Wiederherstellen;
begin
  Shell_NotifyIcon(NIM_DELETE,@NID);
  ShowWindow(Application.Handle,SW_SHOW);
  Application.Restore;
end;

procedure TForm1.WndProc(var Message: TMessage);  
var
  P: TPoint;
begin
  case Message.Msg of
    WM_TRAYMSG:
      case Message.lParam of
        WM_LBUTTONDOWN:
          Wiederherstellen;
        WM_RBUTTONDOWN:
          begin
            GetCursorPos(P);
            SetForegroundWindow(Self.Handle); 
            PopupMenu1.Popup(P.X, P.Y);
          end;
      end;
    else
      inherited WndProc(Message);
  end;
end;

end.

Um das ganze mit eigenen Augen betrachten zu können, als Anhang auch noch die minimale *.exe.

Vielen Dank,
Wonko
Einloggen, um Attachments anzusehen!
_________________
"Read the directions, even if you don't follow them.", Mary Schmich