Question

i am using the code below. I want to hide the application, but show it in the system try(works), but then when i try to show the main form on left mouse click down, nothing happens. can you guys please help? i have included just about all the code.

Main Form Code:

unit Main_Unit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,shellApi,AppEvnts;



type
  TMain = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
   TrayIconData: TNotifyIconData;
    procedure TrayMessage(var Msg: TMessage);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Main: TMain;

const
  WM_ICONTRAY = WM_USER + 1;

implementation

{$R *.dfm}

uses Functions;

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

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

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

end.

Initializing Code:

program Test;

uses
  Vcl.Forms,
  Main_Unit in 'Main_Unit.pas' {Main},
  Functions in 'Functions.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TMain, Main);
  Application.Run;
end.

Thank You

Était-ce utile?

La solution

You defined a message handler, but you did not connect to to the message ID. In the declaration of the form type, change the TrayMessage declaration to be:

procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;

Beyond that I have the following comments:

  1. Always check Win32 function return values for errors.
  2. You write Main.Show and Main.Hide in a TMain method. You should simply remove Main. and call these methods on the implicit Self object.
  3. Use bitwise or rather than arithmethic + to combine flags.
  4. You use the form handle for the notification icon. If the form is re-created, your icon will be orphaned. Instead you need to use a stable window handle obtained by a call to AllocateHWnd.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top