Thema: Delphi SystemTray

Einzelnen Beitrag anzeigen

Benutzerbild von flomei
flomei

Registriert seit: 17. Jan 2003
Ort: Schieder-Schwalenberg
2.094 Beiträge
 
Delphi 2005 Personal
 
#2
  Alt 18. Apr 2003, 10:17
Von www.delphi-fundgrube.de
Zitat:
Ein TrayIcon in der Taskbar Notification Area (neben der Systemuhr) anzeigen

Wie erreicht man, daß anstelle des normalen Programm-Buttons in der Windows-Taskbar ein TrayIcon in der Taskbar Notification Area neben der Windows.Systemuhr angezeigt wird?

Dafür gibt es die API-Funktion Shell_NotifyIcon() und den Daten-Record TNotifyIconData. Und so bindet man das ganze in ein Programm ein (Beispiel von Heino Tiedemann):

Delphi-Quellcode:
uses
  ShellAPI;

const
  WM_TASKABAREVENT = WM_USER+1; //Taskbar message
[...]

type
  TMainForm = class(TForm)
[...]
private
    { Private-Deklarationen }
    procedure TaskbarEvent(var Msg: TMessage);
      Message WM_TASKABAREVENT;
[...]

{Message-Prozedur für das TrayIcon}
procedure TMainForm.TaskbarEvent(var Msg: TMessage);
var Point : TPoint;
begin

  { Die WM_TaskbarEvent-Message "Msg" gibt in Msg.LParam
    das genaue Ereignis an. Msg.LParam kann folgende Werte für
    Mausereignisse annehmen:

    WM_MouseMove
    WM_LButtonDown
    WM_LButtonUp
    WM_LButtonDblClk
    WM_RButtonDown
    WM_RButtonUp
    WM_RButtonDblClk }


  { Eventhandler-Beispiele von Robert Fischer: }
  case Msg.LParam of
    WM_LBUTTONDBLCLK:
      begin
        //Mach etwas nach einem Doppelklick...
      end;
    WM_LBUTTONUP:
      begin
        //Mach etwas nach einem Linksklick...
      end;
    WM_RBUTTONUP:
      begin
        // Rechtsklick
        // Diese Zeile ist wichtig, damit das PopupMenu korrekt
        // wieder geschlossen wird:
        SetForegroundWindow(Handle);
        // PopupMenu anzeigen:
        GetCursorPos(Point);
        PopupMenu1.Popup(Point.x, Point.y);
        //oder ohne Variable Point:
        //PopupMenu1.Popup(Mouse.CursorPos.x, Mouse.CursorPos.y);
      end;
  end;
end;

{TrayIcon mit dem Hauptformular erzeugen}
procedure TMainForm.FormCreate(Sender: TObject);
var
  NotifyIconData: TNotifyIconData;
begin
  Fillchar(NotifyIconData,Sizeof(NotifyIconData),0);
  NotifyIconData.cbSize := Sizeof(NotifyIconData);
  NotifyIconData.Wnd := Handle;
  NotifyIconData.uFlags := NIF_MESSAGE
    or NIF_ICON
    or NIF_TIP;
  NotifyIconData.uCallbackMessage := WM_TASKABAREVENT;
  NotifyIconData.hIcon := Application.Icon.Handle;
  NotifyIconData.szTip := 'Hinweistext für das TrayIcon';
  Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
end;

{TrayIcon mit dem Hauptformular zerstören}
procedure TMainForm.FormDestroy(Sender: TObject);
var
  NotifyIconData: TNotifyIconData;
begin
  FillChar(NotifyIconData,Sizeof(NotifyIconData),0);
  NotifyIconData.cbSize := Sizeof(NotifyIconData);
  NotifyIconData.Wnd := Self.Handle;
  NotifyIconData.uFlags := NIF_MESSAGE
    or NIF_ICON
    or NIF_TIP;
  NotifyIconData.uCallbackMessage := WM_TASKABAREVENT;
  NotifyIconData.hIcon := Application.Icon.Handle;
  NotifyIconData.szTip := 'Punkt';
  Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
end;
Hab es selber noch nicht ausprobiert.

MfG Florian
Florian Meier
... ist raus.
Vielen Dank für die Zeit mit euch!
http://www.flomei.de -- http://www.md5hash.de
  Mit Zitat antworten Zitat