Recent

Author Topic: Minimize on 'x' and close by system application menu?  (Read 31533 times)

rajivsoft

  • New Member
  • *
  • Posts: 48
Minimize on 'x' and close by system application menu?
« on: February 15, 2011, 11:08:57 am »
Hi all,
I'm need to minimize my application instead of close it when a user clicks on red 'x', but if i simply set CanClose to false the i can't close my application anymore! It seems that 'Exit from My App' voice of system application menu calls same OnCloseEvent of the MainForm (i'v tried to cast Sender.Class to messagebox and in either cases it mention my TFrm_Main).

So my question is, how to minimize on 'x' and close by system application menu?

Sora-Kun

  • Full Member
  • ***
  • Posts: 162
  • I can smell your presence ...
    • Sora-Kun
Re: Minimize on 'x' and close by system application menu?
« Reply #1 on: February 15, 2011, 11:17:06 am »
Hello,
 I don't know how to do that really, but some one made it, when you exit the  app, it's minimized to the system bar. have a look, the application name is Opengrabby, the source code is available too http://fabienwang.com.
good luck :)
if nothing suites you, make it your self!
The Revolution, Genesis. The next generation IDE.
If you want to help, PM me.

Made in Lazarus.
Soon, in The WWW.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Minimize on 'x' and close by system application menu?
« Reply #2 on: February 15, 2011, 12:53:04 pm »
but if i simply set CanClose to false the i can't close my application anymore!

Well, then make it conditional based on a boolean variable.
And if the user selects the actual exit function, set that variable to true.
Something like:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   if not ReallyCanClose then
  4.     CloseAction := caNone;
  5. end;
  6.  
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Minimize on 'x' and close by system application menu?
« Reply #3 on: February 15, 2011, 04:40:12 pm »
I'm need to minimize my application instead of close it when a user clicks on red 'x',

Remind me again why you want to change standard program behavior. Sounds like a bad idea and very un-Mac-like.

Thanks.

-Phil

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: Minimize on 'x' and close by system application menu?
« Reply #4 on: February 16, 2011, 03:21:06 pm »
but if i simply set CanClose to false the i can't close my application anymore!

Well, then make it conditional based on a boolean variable.
And if the user selects the actual exit function, set that variable to true.
Something like:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   if not ReallyCanClose then
  4.     CloseAction := caNone;
  5. end;
  6.  
The problem is as i say that it seems like 'x' and system menu exit are calling same FromOnClose function and i don't know how to distinguish them...

I'm need to minimize my application instead of close it when a user clicks on red 'x',

Remind me again why you want to change standard program behavior. Sounds like a bad idea and very un-Mac-like.

Thanks.

-Phil
in a word, marketers... for them it must be same functionally as on windows...

but for now they are calm and not insist much on this, it's more like for future to minimizing it to tray as it happen on all Intant Messengers. Also I don't see this one as change of standard program behavior, in quite all mac app if you close it with 'x' it's only close main window, not 'program' itself that remain active in dock and with main menu.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Minimize on 'x' and close by system application menu?
« Reply #5 on: February 16, 2011, 03:42:45 pm »
Can't you have a separate application menu or button that use the "ReallyCanClose" variable? That would be the preferred way to close it. When i recall for example Miranda messenger behavior it only operates in system tray. Only way to quit it is opening its application controlled popup.

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: Minimize on 'x' and close by system application menu?
« Reply #6 on: February 17, 2011, 02:26:55 pm »
Can't you have a separate application menu or button that use the "ReallyCanClose" variable? That would be the preferred way to close it. When i recall for example Miranda messenger behavior it only operates in system tray. Only way to quit it is opening its application controlled popup.
Windows version of program has exit voice in main menu which is used as 'real exit', but in mac you have system exit menu, so there is non need of another one exit...

I would like only to know how to make like for example in chrome where ehen you click on red 'x' it closes form and tubs, but program itself remain opened and you can open new form with new tab from main menu.

A tricky way to do it is to create on startup an empty form with only main menu, wich creates another form with all stuff, so when you close child form it only hides (canclose := false and windowstate = wsminimized) and i can always exit application from system menu exit. But it's only a workaround, not what it really must be...
« Last Edit: February 17, 2011, 02:30:20 pm by rajivsoft »

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: Minimize on 'x' and close by system application menu?
« Reply #7 on: March 09, 2011, 05:43:23 pm »
I'v seen that Skype is hiding main form on 'x' and exit program from system menu... (what also I need) but it's written in Object C and native Carbon... anybody know how to have same functionality via LCL and Lazarus?

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Minimize on 'x' and close by system application menu?
« Reply #8 on: March 09, 2011, 09:22:45 pm »
YourForm.OnCloseQuery()
begin
  CanClose := False;
  Hide;
end;
should do the job if I correctly understood you

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: Minimize on 'x' and close by system application menu?
« Reply #9 on: March 11, 2011, 11:10:08 am »
YourForm.OnCloseQuery()
begin
  CanClose := False;
  Hide;
end;
should do the job if I correctly understood you
No, you haven't read all my post, i'v already tried this way, but doing so also makes system 'Exit from yourapplication' to hide form because it fires same YourForm.OnCloseQuery... which is not exit as menu says and you can't modify caption of application system menu (i have tried).

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: Minimize on 'x' and close by system application menu?
« Reply #10 on: March 11, 2011, 12:32:06 pm »
procedure TFrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CloseAction := caHide;
end;

In your menu item to Quit:

Application.Terminate;
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: Minimize on 'x' and close by system application menu?
« Reply #11 on: March 11, 2011, 04:23:56 pm »
procedure TFrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CloseAction := caHide;
end;

In your menu item to Quit:

Application.Terminate;

On MAC OS X all application have their own system exit menu, it's default, all application have it with or without custom main menu, and you can't change it, so if i use your code as i said in post before when user click system application exit menu the apllication will hide, and it's not exit...

btw i have found a solution via stack and custom form class inspection:
Code: Pascal  [Select][+][-]
  1. uses LMessages, ...
  2.  
  3. type
  4.   { TFrm_Main }
  5.  
  6.   TFrm_Main = class(TForm)
  7.   ...
  8.   procedure WMCloseQuery(var message: TLMessage); message LM_CLOSEQUERY;
  9.  
  10. ...
  11.  
  12. procedure TFrm_Main.WMCloseQuery(var message: TLMessage);
  13. begin
  14.   Self.WindowState := wsMinimized;
  15. end;
WMCloseQuery is called when you press 'x' and if not rewrited by you in your form call's Close.

Now the only problem is that as you see i must use Self.WindowState := wsMinimized because if i use Self.Hide it hides form ( XD ) and you can no more show it by only clicking on application dock icon, so the question is:
What event is fired when you click on dock icon?

guest48704

  • Guest
Re: Minimize on 'x' and close by system application menu?
« Reply #12 on: June 03, 2018, 10:01:30 pm »
var
Form1: TForm1;
       RemoteCanClose : boolean = True;


//WMSysCommand
procedure TForm1.WMSysCommand(VAR Msg: TWMSysCommand);
begin
     if Msg.CmdType = SC_CLOSE then          // - button  (upper right of form next to X button)
        RemoteCanClose := False;
end;

//Close button
procedure TForm1.Button1Click(Sender: TObject);
begin
     RemoteCanClose := True;
     Close;
end;

//FormCloseQuery
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
     CanClose := RemoteCanClose;
     Form1.Show;         // X will not work again unless this done before hide
     Form1.Hide;
end;

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Minimize on 'x' and close by system application menu?
« Reply #13 on: June 04, 2018, 03:05:53 pm »
lazarus9, did you spot that this was a seven year old thread ?

Anyway, its a topic that interests me too. I do that in tomboy-ng, it works on carbon but not on cocoa in my model at least.
With cocoa, when you hide the main form, the apple provided menu hides, the forms main menu hides and even the tray icon (if you have one) hides.

So be careful in basing a project on being able to do this, carbon will not be around for long.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018