Recent

Author Topic: How to hide window buttons (Minimize, Maximize and Close) in a Windows app  (Read 2598 times)

Libra07

  • New Member
  • *
  • Posts: 17
Hi! I need to change the title of the window and hide the buttons (Minimize, Maximize and Close) on the top right corner in a small windows console application.
The users constantly close the running application or resize the window and want to prevent this.
This is not a Lazarus GUI App, only a Windows console App. So I can't set this in Lazarus window properties at Object Inspector.
And we don't need it to be platform independent, it's enough if it runs on Windows.

I made a small program to present this. This small application runs only in Windows, so this use the "Windows" unit and operating with Windows API functions.
I can save the original title, and I can update it, and restore the original value. But I don't know, how can I hide/show the buttons.
Can anyone help me with this?

Code: Pascal  [Select][+][-]
  1. PROGRAM Window_Title_And_Buttons;
  2. Uses Windows;
  3. Var Original_Window_Title : ShortString;
  4.  
  5. Function Get_Console_Title:ShortString;
  6. Var GGet_Title,
  7.     SGet_Title  : ShortString;
  8.     LX          : Longint;
  9. Begin
  10.   GETConsoleTitle(@GGet_Title[1],255);
  11.  
  12.   SGet_Title:='';
  13.   LX:=1;
  14.   While GGet_Title[LX]<>#0 Do
  15.   Begin
  16.     SGet_Title:=SGet_Title+GGet_Title[LX];
  17.     Inc(LX);
  18.   End;
  19.  
  20.   Get_Console_Title:=SGet_Title;
  21. End;
  22.  
  23. Procedure Set_Console_Title(CTitle:ShortString);
  24. Begin
  25.   SETConsoleTitle(@CTitle[1]);
  26. End;
  27.  
  28. Begin
  29.   Writeln('Press ENTER to update Window Title...');
  30.   ReadLn;
  31.  
  32.   Original_Window_Title:=Get_Console_Title;
  33.   Set_Console_Title('Demo program...'#0);
  34.  
  35.   Writeln('Press ENTER to restore original Window Title...');
  36.   ReadLn;
  37.  
  38.   Set_Console_Title(Original_Window_Title+#0);
  39.  
  40.   Writeln('Press ENTER to Exit...');
  41.   ReadLn;
  42. End.
  43.  

Here are the Windows Console API functions, but I can't find the right one:
https://learn.microsoft.com/en-us/windows/console/console-functions
« Last Edit: December 07, 2022, 12:04:31 pm by Libra07 »

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #1 on: December 07, 2022, 12:12:33 pm »
By the way I am trying to ChatGPT so I asked it and answer is:

Me:How to hide window button of the console/cmd screen in lazarus/fpc

GPT: It is not possible to hide the window buttons on a console or command prompt screen in Lazarus/Free Pascal. The console or command prompt is a separate application from your Lazarus/Free Pascal program, and it does not provide any methods or properties that allow you to manipulate the appearance of its window.

In general, you cannot control the appearance of the console or command prompt window from a Lazarus/Free Pascal program. The only way to hide the window buttons would be to use the operating system's window management functions to manipulate the console or command prompt window directly, but this is not possible using Lazarus/Free Pascal alone.

If you want to hide the window buttons on a form in your Lazarus/Free Pascal program, you can use the BorderStyle property of the TForm object, as I described in my previous response. This property allows you to control the type of border and buttons that are displayed on the form, including the option to hide the buttons completely. However, this will only affect the form within your Lazarus/Free Pascal program, and it will not affect the console or command prompt window.
« Last Edit: December 07, 2022, 12:14:18 pm by tr_escape »

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #2 on: December 07, 2022, 12:19:35 pm »
But I think :

https://learn.microsoft.com/en-us/windows/console/getconsolewindow

You can take handle of the current console window

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #3 on: December 07, 2022, 12:47:13 pm »
After some chat by ChatGPT I remember some functions of the Windows :)

So of course winApi gives a lot of option that I just tried another way...


Code: Pascal  [Select][+][-]
  1. program consolewin;
  2.  
  3. uses
  4.   Windows;
  5.  
  6. var
  7.  
  8.   handle: THANDLE;
  9.   Wnd: HWND;  // Handle to the window
  10.   Style: longint;  // Window style attributes
  11.   Title: array[0..255] of char;  // Buffer to store the console title
  12.  
  13. begin
  14.   handle := Windows.GetConsoleWindow();
  15.   if handle <> 0 then
  16.   begin
  17.     GetConsoleTitle(Title, Length(Title));
  18.     WriteLn(Title);
  19.     // Get the handle to the window you want to hide the buttons for
  20.     Wnd := FindWindow('consolewin', Title);
  21.  
  22.     // Get the current window style attributes
  23.     Style := GetWindowLong(handle, GWL_STYLE);
  24.  
  25.     // Modify the window style to hide the buttons
  26.     Style := Style and not (WS_SYSMENU or WS_MINIMIZEBOX or WS_MAXIMIZEBOX);
  27.  
  28.     // Set the modified window style attributes
  29.     // SetWindowLong(Wnd, GWL_STYLE, Style);
  30.  
  31.     // Set the modified window style attributes
  32.     SetWindowLong(handle, GWL_STYLE, Style);
  33.  
  34.   end;
  35.   ReadLn;
  36. end.
  37.  
« Last Edit: December 07, 2022, 12:51:28 pm by tr_escape »

balazsszekely

  • Guest
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #4 on: December 07, 2022, 12:56:34 pm »
@tr_escape

Very nice! I almost posted something similar, then I saw your reply. As a side note you can still close the application with Alt + F4.

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #5 on: December 07, 2022, 12:57:39 pm »
@Getmem :

I have to ask to ChatGPT ;)
« Last Edit: December 07, 2022, 12:59:19 pm by tr_escape »

balazsszekely

  • Guest
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #6 on: December 07, 2022, 01:00:18 pm »
@tr_escape

Quote
I have to as ChatGPT ;)
What is ChatGPT? According to google is an Open AI. Where do you ask questions?

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #7 on: December 07, 2022, 01:02:02 pm »
Yes absolutly:

The answer is:

Me:
How can I deny to close console app in the console mode?

GPT:
To prevent a console application from being closed in the console mode in Windows, you can use the SetConsoleCtrlHandler function from the Windows unit. This function allows you to register a callback function that is called when the user attempts to close the console window using the Ctrl+C or Ctrl+Break key combinations.

Here is an example of how to use the SetConsoleCtrlHandler function to prevent a console application from being closed:

Copy code
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. var
  4.   PrevHandler: PHandlerRoutine;  // Pointer to the previous handler routine
  5.  
  6. // Callback function that is called when the user attempts to close the console
  7. function ConsoleCtrlHandler(CtrlType: DWORD): BOOL; stdcall;
  8. begin
  9.   // Do not allow the console to be closed
  10.   Result := True;
  11. end;
  12.  
  13. begin
  14.   // Register the callback function
  15.   SetConsoleCtrlHandler(@ConsoleCtrlHandler, True, PrevHandler);
  16.  
  17.   // Run the main program logic here
  18.  
  19.   // Unregister the callback function when the program is finished
  20.   SetConsoleCtrlHandler(PrevHandler, False);
  21. end;
This code will register the ConsoleCtrlHandler function as a callback that is called when the user attempts to close the console window using the Ctrl+C or Ctrl+Break key combinations. The ConsoleCtrlHandler function simply returns True, which prevents the console from being closed. When the program is finished, the callback function is unregistered using the SetConsoleCtrlHandler function.

Note that you must include the Windows unit in your code in order to use the SetConsoleCtrlHandler function. You can do this by adding the following line at the top of your code:

Copy code
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
This will allow you to use the SetConsoleCtrlHandler function in your code.

Keep in mind that using the SetConsoleCtrlHandler function to prevent a console application from being closed is a low-level operation and should be used with caution. It can have unintended side effects, and it may not be portable to other operating systems. It is generally recommended to use the built-in methods and properties of the Lazarus/Free Pascal language to manipulate the console window, if possible.

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #8 on: December 07, 2022, 01:02:45 pm »
@tr_escape

Quote
I have to as ChatGPT ;)
What is ChatGPT? According to google is an Open AI. Where do you ask questions?

https://chat.openai.com/chat

You have to register by email

balazsszekely

  • Guest
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #9 on: December 07, 2022, 01:10:28 pm »
Quote
https://chat.openai.com/chat

You have to register by email
Thank you!

Quote
GPT:
To prevent a console application from being closed in the console mode in Windows, you can use the SetConsoleCtrlHandler function from the Windows unit. This function allows you to register a callback function that is called when the user attempts to close the console window using the Ctrl+C or Ctrl+Break key combinations.
Interesting.

PS: Before I register, please ask him if he knows the solution to the Riemann hypothesis.  :D

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2069
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #10 on: December 07, 2022, 01:13:45 pm »
Slightly updated for 32/64bit compatible working.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  4.  
  5. uses
  6.   Windows;
  7.  
  8. var
  9.   Original_Window_Title: shortstring;
  10.   Wnd: HWND;
  11.   OldStyle, NewStyle: LONG_PTR;
  12.   Caption: array[0..255] of AnsiChar;
  13.  
  14.   function Get_Console_Title: shortstring;
  15.   var
  16.     GGet_Title, SGet_Title: shortstring;
  17.     LX: longint;
  18.   begin
  19.     GETConsoleTitle(@GGet_Title[1], 255);
  20.  
  21.     SGet_Title := '';
  22.     LX := 1;
  23.     while GGet_Title[LX] <> #0 do
  24.     begin
  25.       SGet_Title := SGet_Title + GGet_Title[LX];
  26.       Inc(LX);
  27.     end;
  28.  
  29.     Get_Console_Title := SGet_Title;
  30.   end;
  31.  
  32.   procedure Set_Console_Title(CTitle: shortstring);
  33.   begin
  34.     SETConsoleTitle(@CTitle[1]);
  35.   end;
  36.  
  37. function Handler(dwCtrlType: DWORD): WINBOOL; StdCall;
  38. begin
  39.   Result := False;
  40.   case dwCtrlType of
  41.     CTRL_C_EVENT: begin WriteLn('CTRL_C_EVENT'); ReadLn; Result := True; end;
  42.     CTRL_BREAK_EVENT: begin WriteLn('CTRL_BREAK_EVENT'); ReadLn; Result := True; end;
  43.     CTRL_CLOSE_EVENT: begin WriteLn('CTRL_CLOSE_EVENT'); ReadLn; Result := True; end;
  44.     CTRL_LOGOFF_EVENT: begin WriteLn('CTRL_LOGOFF_EVENT'); ReadLn; Result := True; end;
  45.     CTRL_SHUTDOWN_EVENT: begin WriteLn('CTRL_SHUTDOWN_EVENT'); ReadLn; Result := True; end;
  46.   else
  47.     begin WriteLn(dwCtrlType, ' = unknown Event'); ReadLn; end;
  48.   end;
  49. end;
  50.  
  51.  
  52. begin
  53.   if (GetConsoleWindow = 0) then
  54.     AllocConsole;
  55.  
  56.   if SetConsoleCtrlHandler(@Handler, True) then
  57.     WriteLn('Handler set.')
  58.     else
  59.     WriteLn('Handler NOT set.');
  60.  
  61.   GetConsoleTitle(Caption, Length(Caption));
  62.   Wnd := FindWindow(nil, Caption);
  63.   if Wnd <> 0 then
  64.     begin
  65.       WriteLn('Update the Style');
  66.       OldStyle := GetWindowLongPtr(Wnd, GWL_STYLE);
  67.       NewStyle := OldStyle and not (WS_SYSMENU or WS_MINIMIZEBOX or WS_MAXIMIZEBOX);
  68.       SetWindowLongPtr(Wnd, GWL_STYLE, NewStyle);
  69.       Writeln('Press ENTER to restore original Window Style...');
  70.       ReadLn;
  71.       SetWindowLongPtr(Wnd, GWL_STYLE, OldStyle);
  72.     end
  73.     else
  74.       WriteLn('Error @ Update the Style');
  75.  
  76.   Original_Window_Title := Get_Console_Title;
  77.   Writeln('Press ENTER to update Window Title...');
  78.   ReadLn;
  79.   Set_Console_Title('Demo program...'#0);
  80.   Writeln('Press ENTER to restore original Window Title...');
  81.   ReadLn;
  82.  
  83.   Set_Console_Title(Original_Window_Title + #0);
  84.  
  85.   Writeln('Press ENTER to Exit...');
  86.   ReadLn;
  87.   FreeConsole;
  88.   SetConsoleCtrlHandler(@Handler, False);
  89. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #11 on: December 07, 2022, 01:37:10 pm »
Quote
https://chat.openai.com/chat

You have to register by email
Thank you!

Quote
GPT:
To prevent a console application from being closed in the console mode in Windows, you can use the SetConsoleCtrlHandler function from the Windows unit. This function allows you to register a callback function that is called when the user attempts to close the console window using the Ctrl+C or Ctrl+Break key combinations.
Interesting.

PS: Before I register, please ask him if he knows the solution to the Riemann hypothesis.  :D

You have to ask your self :) maybe answer isn't clear

Libra07

  • New Member
  • *
  • Posts: 17
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #12 on: December 07, 2022, 01:44:45 pm »
Thank you tr_escape!

This is the perfect solution for me. The buttons on the Title Bar are also gone, and the right mouse button to close the app doesn't work either. Perfect!
I built it into the application and it works perfectly. At the end of the application, I made a reset to the original state. Thank you!

I will also look at the other answers, because I see that there are other solutions. I'm curious about those too. Maybe I can get some extra ideas. It looks like somebody could disable Ctrl+C and Ctrl+Break, which would also be useful.

Libra07

  • New Member
  • *
  • Posts: 17
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #13 on: December 09, 2022, 05:58:52 pm »
KodeZwerg, thanks for this extended source code! This pointer solution at the Console Style is much nicer than my previous.

I have one more question. I tried to hide or disable only the Maximize and the Close buttons. The Minimize would be good for users if they want to do something else on the PC. I couldn't do it, even though it worked logically.

I can disable only the Maximize button, this works quite understandably.
NewStyle := OldStyle and not (WS_MAXIMIZEBOX);

If I turn off SYSMENU, all buttons and menus are disabled. After all, this is also understandable.
NewStyle := OldStyle and not (WS_SYSMENU);

Can I just remove the CLOSE separately? Or to make it inactive? I guess it can't be remove only this, because it's a toolbar together? But can't I make it inactive, like the Maximize? The Handler() function notices the close operation, but it is executed. Should I prevent it somehow?

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to hide window buttons (Minimize, Maximize and Close) in a Windows app
« Reply #14 on: December 10, 2022, 01:38:19 pm »
I tried to hide or disable only the Maximize and the Close buttons. The Minimize would be good for users if they want to do something else on the PC.
...
Can I just remove the CLOSE separately?
Here is an example. This will only help from accidentally closing the console. If the user wants, he can close through the taskbar menu or through the program manager.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses Windows;
  5.  
  6. procedure DisableButtons(out CloseCaption: UnicodeString);
  7. var
  8.   Wnd: HWND;
  9.   Menu: HMENU;
  10.   Buf: array[Byte] of WideChar;
  11.   Len: LONG;
  12. begin
  13.   Wnd := GetConsoleWindow;
  14.   SetWindowLong(Wnd, GWL_STYLE, GetWindowLong(Wnd, GWL_STYLE) and not WS_MAXIMIZEBOX);
  15.   Menu := GetSystemMenu(Wnd, False);
  16.   Len := GetMenuStringW(Menu, SC_CLOSE, @Buf, Length(Buf), MF_BYCOMMAND);
  17.   SetString(CloseCaption, Buf, Len);
  18.   DeleteMenu(Menu, SC_CLOSE, MF_BYCOMMAND);
  19. end;
  20.  
  21. procedure EnableButtons(const CloseCaption: UnicodeString);
  22. var
  23.   Wnd: HWND;
  24.   R: TMenuItemInfoW;
  25. begin
  26.   Wnd := GetConsoleWindow;
  27.   SetWindowLong(Wnd, GWL_STYLE, GetWindowLong(Wnd, GWL_STYLE) or WS_MAXIMIZEBOX);
  28.   ZeroMemory(@R, SizeOf(R));
  29.   R.cbSize := SizeOf(R);
  30.   R.fMask := MIIM_STRING or MIIM_ID or MIIM_BITMAP;
  31.   R.dwTypeData := Pointer(CloseCaption);
  32.   R.wID := SC_CLOSE;
  33.   R.hbmpItem := HBMMENU_MBAR_CLOSE;
  34.   InsertMenuItemW(GetSystemMenu(Wnd, False), 6, True, @R);
  35. end;
  36.  
  37. function GetConsoleTitle: UnicodeString;
  38. var
  39.   Buf: array[0..511] of WideChar;
  40.   Len: DWORD;
  41. begin
  42.   Len := Windows.GetConsoleTitleW(@Buf, Length(Buf));
  43.   SetString(Result, Buf, Len);
  44. end;
  45.  
  46. var
  47.   OldConsoleTitle, CloseCaption: UnicodeString;
  48. begin
  49.   OldConsoleTitle := GetConsoleTitle;
  50.   Write('The usual view of the console. Press Enter to continue...');
  51.   Readln;
  52.   SetConsoleTitle('Demo program...');
  53.   DisableButtons(CloseCaption);
  54.   Write('Now the console has changed. Press Enter to continue...');
  55.   Readln;
  56.   SetConsoleTitleW(Pointer(OldConsoleTitle));
  57.   EnableButtons(CloseCaption);
  58.   Write('Restored. Press Enter to exit...');
  59.   Readln;
  60. end.

 

TinyPortal © 2005-2018