Recent

Author Topic: TMainmenu: Hide but shortcuts allowed  (Read 1586 times)

Milsa

  • Sr. Member
  • ****
  • Posts: 323
TMainmenu: Hide but shortcuts allowed
« on: July 09, 2023, 06:48:19 pm »
I have TMainMenu in my project.

Sometimes I need to run my application in fullscreen withtou main menu on the top of screen.

This is code for menu show:
Code: Pascal  [Select][+][-]
  1.       if FullScreen.Running then
  2.       begin
  3.         if Menu = Nil then
  4.           Menu := MainMenu
  5.         else
  6.           Menu := Nil;
  7.       end;
  8.  
When I remove menu then I am without shortcuts from menu.

It is posible use shortcuts from menu with hidden main menu without duplicity in code? I think shortcuts in main menu and shortcuts routines in FormKeyDown with hidden main menu?

Maybe it is possible change main menu's height to 0 and then it is done.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

Pe3s

  • Hero Member
  • *****
  • Posts: 574
Re: TMainmenu: Hide but shortcuts allowed
« Reply #1 on: July 09, 2023, 06:58:32 pm »
Hello, hide menu
Code: Pascal  [Select][+][-]
  1. Self.Menu := nil;
  2.  
show menu
Code: Pascal  [Select][+][-]
  1.  self.Menu := MainMenu1;
  2.  

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #2 on: July 09, 2023, 07:09:54 pm »
This is my code upper.

I need code for hide menu but all menu shortcuts must be allowed. Removing menu from menu property disallows menu shortcuts.

It is possible to change menu height to 0 without remove menu?
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #3 on: July 09, 2023, 07:14:01 pm »
This is my exact code:
Code: Pascal  [Select][+][-]
  1. procedure TFormMain.SwitchFullscreen(b: Boolean);
  2. begin
  3.   with FullScreen do
  4.   begin
  5.     if Running = b then
  6.       Exit;
  7.     if not Running then
  8.     begin
  9.       WLeft := Left;
  10.       WTop := Top;
  11.       WWidth := Width;
  12.       WHeight := Height;
  13.       Menu := Nil;
  14.     end;
  15.     Running := b;
  16.     ToolBar.Visible := not Running;
  17.     StatusBar.Visible := not Running;
  18.     if Running then
  19.     begin
  20.       BorderStyle := bsNone;
  21.       WindowState := wsFullScreen;
  22.     end
  23.     else
  24.     begin
  25.       BorderStyle := bsSizeable;
  26.       WindowState := wsNormal;
  27.     end;
  28.     if not Running then
  29.     begin
  30.       Menu := MainMenu;
  31.       Left := WLeft;
  32.       Top := WTop;
  33.       Width := WWidth;
  34.       Height := WHeight;
  35.     end;
  36.   end;
  37.   {
  38.   FormResize(Self);
  39.   }
  40.   OpenGLControl.Free;
  41.   OpenGLControl := TOpenGLControl.Create(PanelScreen);
  42.   with OpenGLControl do
  43.   begin
  44.     OnPaint := @OpenGLControlPaint;
  45.     Parent := PanelScreen;
  46.   end;
  47.   fTex := 0;
  48.   FormResize(OpenGLControl);
  49. end;                    
Shortcut does not work in fullscreen. They can not.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: TMainmenu: Hide but shortcuts allowed
« Reply #4 on: July 09, 2023, 11:56:54 pm »
I don't know your target and I don't think there is any cross platform way to do this unless the IntLCL supports the
LM_SysKeyDown message ?

 I see that you are using a Panel so I can assume that is a TPanel which can receive keyboard input. You can create a local
morph class for the TPanel to support the WM_SysKeyDown message and process the AL+??? messages.

 You would need to create a CASE list of those that also live in your menu.

 I tested by sending that message to the Form and it does allow passage and is processable. Not all messages are passed through the class.

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: TMainmenu: Hide but shortcuts allowed
« Reply #5 on: July 10, 2023, 01:30:32 am »
Here is an example of a local class fixup to capture the message.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, StdCtrls;
  9.  
  10. type
  11.  TButton = Class(StdCtrls.TButton)
  12.    Procedure WMSysKeyDown(Var Msg:Tmessage) Message WM_SysKeyDown;
  13.  end;
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     MainMenu1: TMainMenu;
  20.     MenuItem1: TMenuItem;
  21.     procedure MenuItem1Click(Sender: TObject);
  22.   private
  23.   public
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32. Procedure TButton.WmSysKeyDown(Var Msg:TMessage);
  33. Begin
  34.    Inherited;
  35.    Msg.Result:= 1;
  36.    Caption := Msg.wParam.Tostring;
  37. End;
  38. { TForm1 }
  39.  
  40. procedure TForm1.MenuItem1Click(Sender: TObject);
  41. begin
  42.   Application.Terminate;
  43. end;
  44.  
  45. end.
  46.  
  47.  

if you play with that, you will see the ALT key code until you press another and from that point on, only the other keys shows.

 You can mimic the accelerator keys by doing a ALT+?

Actually, you can use this exclusively if you wish.

The Panel I believe receives the Input if the flags are set or use a Tbutton for the whole surface ;)
The only true wisdom is knowing you know nothing

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #6 on: July 10, 2023, 05:11:37 pm »
I think that duplicity is easier:
1. Enabled menu: Shorcuts in main menu.
2. Disabled menu: Shortcuts in FormKeyDown with case and testing disabled menu.

But thank you for your time.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: TMainmenu: Hide but shortcuts allowed
« Reply #7 on: July 10, 2023, 05:43:26 pm »
The easiest way to achieve this request is by using actions (good article by Brian Long: http://blong.com/Articles/Actions/Actions.htm, but ignore the part about ActionManager which does not exist in Lazarus).

The attached demo contains three actions, one to open a file dialog, one to exit the application, and one to toggle visibility of the main menu. The short cuts CTRL+O, CTRL+X and CTRL+M are assigned to these actions, respectively. Each action is assigned to a menu item, and thus the menu items get the caption, shortcut and click handler from the associated action.

Run the demo, the main menu is shown by default (by changing the code you could turn it off initially equally well). Press CTRL+M (toggle menu), and the menu disappears. Press CTRL+O and the file dialog opens although the mainmenu is hidden. Press CTRL+M again, and the menu reappears although it was hidden.

As you can see, the shortcuts are working even when the menu is turned off, and all this without writing any line of code (except for the code needed to show/hide the menu).

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #8 on: July 19, 2023, 06:10:12 pm »
This is the way. Thank you very much.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #9 on: September 03, 2023, 10:44:09 am »
I have one problem with this:

I have TMainMenu and TSpeedButton. TSpeedButton.Caption is empty.

I set TAction to TMM and TSB. During program running TSpeedButton.Caption is set to TAction caption. I can not delete it.

Please help me.


I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

Milsa

  • Sr. Member
  • ****
  • Posts: 323
Re: TMainmenu: Hide but shortcuts allowed
« Reply #10 on: September 03, 2023, 03:16:09 pm »
Code: Pascal  [Select][+][-]
  1.   for i := 0 to ToolBar.ControlCount - 1 do
  2.     ToolBar.Controls[i].Caption := '';
This is my temporary solution. Does exist any better solution?
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: TMainmenu: Hide but shortcuts allowed
« Reply #11 on: September 03, 2023, 05:12:45 pm »
But why do you add a TSpeedButton to a TToolbar, and not a TToolButton? Toolbuttons, by default, do not display the Caption. If you do want a Caption you must turn TToolbar.Showcaptions on, and every toolbar individually can be with or without caption by turning its TToolbButton.Showcaption on or off. And you can place the caption to the right of the icon by setting TToolBar.List to true.

If you still need the TSpeedButton in the toolbar your code probably is the simplest solution. I don't know why the TSpeedButton.Caption is regenerated from the Action at runtime, and I would call this a bug, but Delphi does the same... An alternative to your solution could be to delete the Caption in the actions and to add them for the other controls using these actions.

 

TinyPortal © 2005-2018