Author: PETER ASCHBACHER (PA-SOFT)
Sometimes, it's necessary to hide the console window of a CONSOLE APPLICATION entirely to avoid distracting the user with an unnecessary command prompt. Fortunately, Delphi provides a straightforward way to achieve this. Below, we'll walk through the steps to create a console application that runs silently without displaying the console window.
program OpenTaskbarSettings;
// The $APPTYPE GUI directive ensures that the application is treated as a GUI application,
// which prevents the console window from being shown.
{$APPTYPE GUI}
uses
System.SysUtils,
Winapi.Windows,
Winapi.ShellAPI;
// Procedure to hide the console window if it exists
procedure HideConsole;
var
hConsole: HWND;
begin
// Get the handle of the console window
hConsole := Winapi.Windows.GetConsoleWindow;
// If the console window handle is valid, hide the console window
if hConsole <> 0 then
ShowWindow(hConsole, SW_HIDE);
end;
// Procedure to open the Taskbar Settings window
procedure DoOpenTaskbarSettings;
begin
// Use ShellExecute to open the Taskbar Settings window
Winapi.ShellAPI.ShellExecute(0, 'open', 'ms-settings:taskbar', nil, nil, SW_SHOWNORMAL);
end;
begin
// Hide the console window immediately upon startup
HideConsole;
try
// Open the Taskbar Settings window
DoOpenTaskbarSettings;
except
// Handle any exceptions that occur
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Explanation of the Code:
// Remove: {$APPTYPE CONSOLE}, instead add:
{$APPTYPE GUI}: The
$APPTYPE GUI
directive* ensures that the application is treated as a GUI application, which prevents the console window from being shown.HideConsole Procedure:
GetConsoleWindow
: Retrieves the handle of the console window.ShowWindow
: Hides the console window if the handle is valid.
DoOpenTaskbarSettings Procedure:
ShellExecute
: Uses the ShellExecute function to open the Taskbar Settings window.
Main Block:
HideConsole
: Hides the console window immediately upon startup.DoOpenTaskbarSettings
: Opens the Taskbar Settings window.try...except
: Handles any exceptions that may occur during the execution.
This setup will create a console application that hides its console window immediately upon startup, so the console window will not be visible at all while the application runs.
Steps to Implement:
- Open Delphi.
- Create a new Console Application.
- Replace the default code with the above code.
- Run the application.
_______________
* A "directive" in programming is a special instruction that tells the compiler how to process the code.
The following declaration is missing:
ReplyDeleteFunction GetConsoleWindow: HWND; stdcall; external kernel32;
Thank you for your comment. GetConsoleWindow is declared in Winapi.Windows. I have added the unit scope in the source. Your comment illustrates the importance of redundant naming for identifiers.
ReplyDeleteThank you again for your feedback! The GetConsoleWindow function is already declared in the Winapi.Windows unit, so you do not need to declare it again. If you include the Winapi.Windows unit in your uses clause (as is the case in the provided source code), the function should be available for use.
ReplyDeleteHi! Nice trick! What's the HideConsole call for? With APPTYPE GUI, there isn't any console to hide. At least I don't notice any, even with HideConsole commented out.
ReplyDeleteD10.2.3 Winapi.Windows.GetConsoleWindow no such function
ReplyDeleteD10.2.3 I canged it to hConsole := Winapi.Windows.GetConsoleCP;
ReplyDelete