Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Stupid bug experimenting with tray icons. Please HELP!!!

legolas_wlegolas_w Member Posts: 7
I was experimenting with tray icons and I wrote the following code, but it has a bug. By doubleclicking the tray icon, the form is supposed to be shown and when minimized it disappears. However, the second time I doubleclick the icon the form is shown but it doesn't get focused (unless I change it's animation property in the popupmenu). It's a stupid bug and there must be a stupid solution but I cannot find it (I tried everything!!!). Can anybody help me, PLEASE!?!?!
This is the entire code:


unit Unit1;

interface

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

type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Exit1: TMenuItem;
Timer1: TTimer;
Button1: TButton;
Animar1: TMenuItem;
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Minimize(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
TrayIconImage1: TIcon;
TrayIconImage2: TIcon;
TrayIconImage3: TIcon;
TrayIcon: TNotifyIconData;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var j: Byte;

procedure TForm1.FormCreate(Sender: TObject);
begin
TrayIconImage1 := TIcon.Create;
TrayIconImage2 := TIcon.Create;
TrayIconImage3 := TIcon.Create;
TrayIconImage1.LoadFromFile('face01.ico');
TrayIconImage2.LoadFromFile('face02.ico');
TrayIconImage3.LoadFromFile('face03.ico');
Application.ShowMainForm := False;
Application.OnMinimize := Minimize;
TrayIcon.cbSize := SizeOf(TrayIcon);
TrayIcon.Wnd := Self.Handle;
TrayIcon.uId := 0;
TrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
TrayIcon.uCallBackMessage := WM_MOUSEMOVE;
j := 1;
TrayIcon.hIcon := TrayIconImage1.Handle;
TrayIcon.szTip := 'Icono esttico';
Shell_NotifyIcon(NIM_ADD, @TrayIcon);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon(NIM_DELETE, @TrayIcon);
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var CursorPos : TPoint;
begin
case (X) of
WM_LBUTTONDBLCLK: begin
Show;
Application.Restore;
end;
WM_RBUTTONUP: begin
SetForegroundWindow(Handle);
GetCursorPos(CursorPos);
PopupMenu1.Popup(CursorPos.X, CursorPos.Y);
PostMessage(Handle, WM_NULL, 0, 0);
end;
end;

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
j := j + 1;
case j of
1: begin
TrayIcon.hIcon := TrayIconImage1.Handle;
end;
2, 4: begin
TrayIcon.hIcon := TrayIconImage2.Handle;
if j = 4 then j := 0;
end;
3: begin
TrayIcon.hIcon := TrayIconImage3.Handle;
end;
end;
Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
Form1.Close;
end;

procedure TForm1.Minimize(Sender: TObject);
begin
Hide;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Caption = 'Animar icono' then
begin
Button1.Caption := 'Detener icono';
Animar1.Caption := 'Detener';
Timer1.Enabled := True;
TrayIcon.szTip := 'Icono animado';
Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
end
else
begin
Button1.Caption := 'Animar icono';
Animar1.Caption := 'Animar';
Timer1.Enabled := False;
TrayIcon.szTip := 'Icono esttico';
Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
end;

end;

end.

Comments

  • netgertnetgert Member Posts: 331
    [b][red]This message was edited by netgert at 2003-7-27 9:45:10[/red][/b][hr]
    : I was experimenting with tray icons and I wrote the following code, but it has a bug. By doubleclicking the tray icon, the form is supposed to be shown and when minimized it disappears. However, the second time I doubleclick the icon the form is shown but it doesn't get focused (unless I change it's animation property in the popupmenu). It's a stupid bug and there must be a stupid solution but I cannot find it (I tried everything!!!). Can anybody help me, PLEASE!?!?!
    : This is the entire code:
    :
    :
    : unit Unit1;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    : ExtCtrls, Menus, ShellAPI, StdCtrls;
    :
    : type
    : TForm1 = class(TForm)
    : PopupMenu1: TPopupMenu;
    : Exit1: TMenuItem;
    : Timer1: TTimer;
    : Button1: TButton;
    : Animar1: TMenuItem;
    : N1: TMenuItem;
    : procedure FormCreate(Sender: TObject);
    : procedure FormClose(Sender: TObject; var Action: TCloseAction);
    : procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
    : Y: Integer);
    : procedure Timer1Timer(Sender: TObject);
    : procedure Exit1Click(Sender: TObject);
    : procedure Minimize(Sender: TObject);
    : procedure Button1Click(Sender: TObject);
    : private
    : { Private declarations }
    : TrayIconImage1: TIcon;
    : TrayIconImage2: TIcon;
    : TrayIconImage3: TIcon;
    : TrayIcon: TNotifyIconData;
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : {$R *.DFM}
    :
    : var j: Byte;
    :
    : procedure TForm1.FormCreate(Sender: TObject);
    : begin
    : TrayIconImage1 := TIcon.Create;
    : TrayIconImage2 := TIcon.Create;
    : TrayIconImage3 := TIcon.Create;
    : TrayIconImage1.LoadFromFile('face01.ico');
    : TrayIconImage2.LoadFromFile('face02.ico');
    : TrayIconImage3.LoadFromFile('face03.ico');
    : Application.ShowMainForm := False;
    : Application.OnMinimize := Minimize;
    : TrayIcon.cbSize := SizeOf(TrayIcon);
    : TrayIcon.Wnd := Self.Handle;
    : TrayIcon.uId := 0;
    : TrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
    : TrayIcon.uCallBackMessage := WM_MOUSEMOVE;
    : j := 1;
    : TrayIcon.hIcon := TrayIconImage1.Handle;
    : TrayIcon.szTip := 'Icono esttico';
    : Shell_NotifyIcon(NIM_ADD, @TrayIcon);
    : end;
    :
    : procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    : begin
    : Shell_NotifyIcon(NIM_DELETE, @TrayIcon);
    : end;
    :
    : procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
    : Y: Integer);
    : var CursorPos : TPoint;
    : begin
    : [red]case (X) of
    : WM_LBUTTONDBLCLK: begin
    : Show;
    : Application.Restore;
    : end;
    : WM_RBUTTONUP: begin[/red]
    : SetForegroundWindow(Handle);
    : [blue]GetCursorPos(CursorPos);
    : PopupMenu1.Popup(CursorPos.X, CursorPos.Y);[/blue]
    : PostMessage(Handle, WM_NULL, 0, 0);
    : end;
    : end;
    :
    : end;
    :
    : procedure TForm1.Timer1Timer(Sender: TObject);
    : begin
    : j := j + 1;
    : case j of
    : 1: begin
    : TrayIcon.hIcon := TrayIconImage1.Handle;
    : end;
    : 2, 4: begin
    : TrayIcon.hIcon := TrayIconImage2.Handle;
    : if j = 4 then j := 0;
    : end;
    : 3: begin
    : TrayIcon.hIcon := TrayIconImage3.Handle;
    : end;
    : end;
    : Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
    : end;
    :
    : procedure TForm1.Exit1Click(Sender: TObject);
    : begin
    : Form1.Close;
    : end;
    :
    : procedure TForm1.Minimize(Sender: TObject);
    : begin
    : Hide;
    : end;
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : if Button1.Caption = 'Animar icono' then
    : begin
    : Button1.Caption := 'Detener icono';
    : Animar1.Caption := 'Detener';
    : Timer1.Enabled := True;
    : TrayIcon.szTip := 'Icono animado';
    : Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
    : end
    : else
    : begin
    : Button1.Caption := 'Animar icono';
    : Animar1.Caption := 'Animar';
    : Timer1.Enabled := False;
    : TrayIcon.szTip := 'Icono esttico';
    : Shell_NotifyIcon(NIM_MODIFY, @TrayIcon);
    : end;
    :
    : end;
    :
    : end.
    :
    :

    Why the heck do you test X-coordinate of mouse in TForm1.FormMouseMove for mouse event? (highlighted in red)

    And since you already get the mouse X & Y coordinates why do you make an extra call to GetCursorPos? (highlighted in blue)

    Also try calling Application.BringToFront after Application.Restore.
Sign In or Register to comment.