Recent

Author Topic: Can Lazarus make a system tray app  (Read 11280 times)

Possum

  • Jr. Member
  • **
  • Posts: 69
    • uDoPage
Can Lazarus make a system tray app
« on: May 19, 2010, 04:35:00 pm »
Hi

Can Lazarus make a system tray app..

If it can how do I start on such an application

Thank you..

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Can Lazarus make a system tray app
« Reply #1 on: May 19, 2010, 06:30:05 pm »
Yes: this gives you a starting point.
Did some trial and error and it appears that the above code results in some compiler problems; the TNotifyIconData record structure isn't correctly processed by the compiler.
If you redefine the structure locally it works  :D

As a proof of concept, the code below works for me (lazarus 0.9.29, WinXP; FPC 2.3.1).
Just add 'Application.ShowMainForm := false;' to the program's initialization block.
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Windows, ShellApi, Messages,
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;

const
  WM_ICONTRAY = WM_USER + 1;

type
  MY_NOTIFYICONDATAA = record
      cbSize: DWORD;
      Wnd: HWND;
      uID: UINT;
      uFlags: UINT;
      uCallbackMessage: UINT;
      hIcon: HICON;
      szTip: array [0..63] of Char;
 end;

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { private declarations }
    // TrayIconData: TNotifyIconData;
    TrayIconData: MY_NOTIFYICONDATAA;
  public
    { public declarations }
    procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TrayIconData do
  begin
    cbSize := SizeOf(TrayIconData);
    uID := 0;
    uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
    uCallbackMessage := WM_ICONTRAY;
    hIcon := Application.Icon.Handle;
    StrPCopy(szTip, Application.Title);
  end;

  TrayIconData.Wnd := Handle;
  Windows.Shell_NotifyIcon(NIM_ADD, @TrayIconData);

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Windows.Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;

procedure TForm1.TrayMessage(var Msg: TMessage);
begin
  case Msg.lParam of
      WM_LBUTTONDOWN:
      begin
        ShowMessage('Left button clicked - let''s SHOW the Form!');
        Show;
      end;
      WM_RBUTTONDOWN:
      begin
        ShowMessage('Right button clicked - let''s HIDE the Form!');
        Hide;
      end;
    end;

end;

initialization
  {$I unit1.lrs}

end.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Can Lazarus make a system tray app
« Reply #2 on: May 19, 2010, 07:53:05 pm »
There is a component TTrayIcon on the "Additional" Tab.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Can Lazarus make a system tray app
« Reply #3 on: May 20, 2010, 05:06:48 am »
Why choose the easy way :-)

Just joking: this is why we need a better search mechanism for the wiki.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018