Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Tray icons and Borderstyle set to bsnone.

Hi.

I have 2 buttons in a form.
- 1st button make the form show only important info and because i want to make it very small i need
   to set BorderStyle=bsNone.
- 2nd button make the form normal again.

BUT my program tray icon disappears.
How can i prevent that ??

Thanks for your help.
Avatar of LMuadDIb
LMuadDIb
Flag of United States of America image

well, your trayicon shouldnt disappear

whose trayicon component are you using?
Is it your own code? If it is, can you paste code here?
Avatar of CodedK

ASKER

Yes ... its not a component.
Here is the code :


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellApi, ImgList, StdCtrls, XPMan;

const
  WM_ICONTRAY=WM_USER+1;

type
  TForm1 = class(TForm)
    ImageList1: TImageList;
    Button1: TButton;
    Button2: TButton;
    XPManifest1: TXPManifest;
    Button3: TButton;
    procedure WMNCHitTest(var Msg: TWMNCHitTest);
     message WM_NCHITTEST;
    procedure FormTray(var Msg:TMessage);
     message WM_ICONTRAY;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    F1Tray:TNotifyIconData;
    F1Icon:TIcon;
    F2Tray:TNotifyIconData;
    F2Icon:TIcon;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
inherited;
if (Msg.Result = HTCLIENT)and(ControlAtPos(ScreenToClient(SmallPointToPoint
(TWMNCHitTest(Msg).Pos)), False)= nil) then Msg.Result := HTCAPTION;
end;

procedure TForm1.FormTray(var Msg:TMessage);
begin
 case Msg.lParam of  WM_LBUTTONDOWN:
 Form1.Show;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   With F1Tray do
   begin
    cbSize:=SizeOf(F1Tray);
    Wnd:=Handle;
    uID:=0;
    uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
    uCallbackMessage:=WM_ICONTRAY;
    F1Icon:=TIcon.Create;
    ImageList1.GetIcon(0,F1Icon);
    hIcon:=F1Icon.Handle;
    StrPCopy(szTip,'This is F1 ');
   end;
   Shell_NotifyIcon(NIM_ADD,@F1Tray);

   With F2Tray do
   begin
    cbSize:=SizeOf(F2Tray);
    Wnd:=Handle;
    uID:=1;
    uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
    uCallbackMessage:=WM_ICONTRAY;
    F2Icon:=TIcon.Create;
    ImageList1.GetIcon(0,F2Icon);
    hIcon:=F2Icon.Handle;
    StrPCopy(szTip,'This is F2');
   end;
   Shell_NotifyIcon(NIM_ADD,@F2Tray);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Height:=25;
Form1.BorderStyle:=bsNone;   //---- PROBLEM
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Height:=160;
Form1.BorderStyle:=bsToolWindow;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE,@F1Tray);
  Shell_NotifyIcon(NIM_DELETE,@F2Tray);
  F1Icon.Free;
  F2Icon.Free;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose:=False;
Form1.Hide;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Application.Terminate;
end;

end.
I'm not sure why do they disappear when you change borders by using default BorderStyle property, but here's a WinAPI alternative that does border changing without that 'bug':

procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Height:=25;
//Form1.BorderStyle:=bsNone;
SetWindowLong(handle, gwl_style, GetWindowLong(handle, gwl_style) and not (ws_caption or ws_sizebox)); // remove both 'blue border' and 'resizing border'
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Height:=160;
//Form1.BorderStyle:=bsToolWindow;
SetWindowLong(handle, gwl_exstyle, GetWindowLong(handle, gwl_exstyle) or ws_ex_toolwindow); // set form-type to toolwindow
SetWindowLong(handle, gwl_style, GetWindowLong(handle, gwl_style) or ws_caption or ws_sizebox); // add 'blue border' and 'resizing border'
SetWindowPos(handle, 0, 0, 0, 0, 0, swp_nozorder or swp_nomove or swp_nosize or swp_drawframe); // redraw new border
end;
ASKER CERTIFIED SOLUTION
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK

ASKER

Thank you very much :)

But i dont want a form that can resize...
Can you please tell me the command ?

Thanks :)
take a look at:
SetWindowLong(handle, gwl_style, GetWindowLong(handle, gwl_style) or ws_caption or ws_sizebox); // add 'blue border' and 'resizing border'
ws_sizebox style - that's the resizable border. change it to:
SetWindowLong(handle, gwl_style, GetWindowLong(handle, gwl_style) or ws_caption);
Avatar of CodedK

ASKER

:) :) :) :) :) :)
Thanks :)
Glad to help :)