Recent

Author Topic: Win11 - How to start console app in non-tabbed window from Windows Explorer  (Read 685 times)

dculp

  • Full Member
  • ***
  • Posts: 151
When I start my console app in Win10 from its File Explorer, the resulting console window doesn't have tabs. (See 1st image.) The result is the same if I start my app in Win11 from a 32bit file manager (Q-dir or FreeCommander). This is the desired situation so that the correct size of the console window can be set from within my app.

If I start my console app in Win11 from its File Explorer, the resulting console window has tabs. The console window isn't sized to the required size and so some of the framework at the bottom of the UI is cut off. (See 2nd image.) Dragging the window boundary to increase the window height doesn't restore the framework and the UI may become unusable.

A partial solution is to start my app from a batch file that calls wt.exe (windows terminal). However, wt.exe isn't included with Win10 so it must be downloaded from the Microsoft Store. I want to avoid this for the Win10 user.

Alternately, a non-tabbed cmd window could be started with admin privileges and my app could be started from there without using File Explorer. However, the end user might not have admin privileges and this extra step is not preferred. Also, the end user's natural inclination is to start the app from File Explorer.

Hence, is there a way to start my console app from Win11 File Explorer in a non-tabbed window so that the UI will be correct?

(Also, please let me know if perhaps there are any better forums to ask this question.)

Thanks,
Don C.

Lazarus 3.8 (rev lazarus_3_8) FPC 3.2.2 i386-win32-win32/win64
« Last Edit: April 16, 2025, 03:56:26 pm by dculp »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5965
  • Compiler Developer
Hence, is there a way to start my console app from Win11 File Explorer in a non-tabbed window so that the UI will be correct?

Windows 11 changed the default terminal emulator from cmd.exe to Terminal. The application itself has no control what kind of terminal emulator will be used. I don't know if this default can be changed, but you definitely only can do this for yourself and not enforce it upon potential users of your software (in case this is relevant for your software).

paule32

  • Sr. Member
  • ****
  • Posts: 448
this will be done with a Batch File:

- open editor
- type in:

Code: Pascal  [Select][+][-]
  1.   cmd.exe /C
  2.   dir
  3.   pause

- save as foo.bat in your directory.

- open Explorer:  Win-Key + E
- navigate to the Path of foo.bat
- double click foo.bat

- voila:  A command windows will be open without tabs.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

dseligo

  • Hero Member
  • *****
  • Posts: 1507
- voila:  A command windows will be open without tabs.

On my Windows 11 it still opens command window with tab.

paule32

  • Sr. Member
  • ****
  • Posts: 448
you could use win32api AllocConsole to make your own Command Line Interface
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1506
    • Lebeau Software
You really should not be messing with the console window to begin with.  You don't own that window. It is not even running in your process.  If you need a customized UI, you should make your own.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

dculp

  • Full Member
  • ***
  • Posts: 151
You really should not be messing with the console window to begin with.  You don't own that window. It is not even running in your process.  If you need a customized UI, you should make your own.

The app is for clients. I want to supply it to them and have it run without them having to fiddle with other processes. This was fine in Win10 where the size of the cmd.exe console window could be set from within the app. (As I mentioned, for Win11 the UI doesn't display correctly with the default console window. Manually resizing the console window after the app has loaded doesn't help.)

I'm currently setting up a batch file that will query the Windows version. (See discussion https://forum.lazarus.freepascal.org/index.php/topic,70722.0.html) If Win11 then the batch file will start wt.exe (Windows Terminal) which allows setting the window size and other parameters. If Win10 or earlier then it will it will just load the non-tabbed cmd.exe.
This works OK but I'm still open to other suggestions.

(Windows Terminal "is available in all versions of Windows 11 and versions of Windows 10 22H2 after the installation of the May 23, 2023 update". See https://learn.microsoft.com/en-us/windows/terminal/install )

Batch file possibly not quite finalized -

Code: Pascal  [Select][+][-]
  1. @ECHO OFF
  2. CLS
  3.  
  4. SETLOCAL
  5. REM See https://www.jamesparker.dev/what-is-the-setlocal-command-in-batch-files/
  6. REM SET commands must be outside "IF EXIST" block.
  7.  
  8. SET App_title="CARD (Computer Aided Resonator Design)"
  9. SET App=CARD_.exe
  10.  
  11. SET CMD_Windows11=%USERPROFILE%\AppData\Local\Microsoft\WindowsApps\wt.exe
  12. SET CMD_Windows11_path=%USERPROFILE%\AppData\Local\Microsoft\WindowsApps\
  13. SET CMD_window=%WINDIR%\System32\cmd.exe
  14.  
  15. SET CRT_columns=83
  16.    REM 12/Apr/2025 - minimum for App is 83; can be larger
  17.  
  18. SET CRT_rows=45
  19.    REM 12/Apr/2025 - minimum for App is 45; can be larger
  20.  
  21. SET Position_X=10
  22. SET Position_Y=1
  23.  
  24. IF EXIST %App% (
  25.    Find_Windows_version;
  26.       REM Returned exitcode (ERRORLEVEL) corresponds to the Windows version
  27.    IF ERRORLEVEL 11 (
  28.       REM Win11 or higher
  29.       IF EXIST %CMD_Windows11% (
  30.          call %CMD_Windows11% --size %CRT_columns%,%CRT_rows% --pos Position_X,Position_Y --title CARD --suppressApplicationTitle -d . %CMD_window% /k %App%
  31.             REM Multi-tabbed console with title bar; dragging window does not distort UI
  32.             REM https://learn.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows
  33.             REM    ==> Using command line arguments for Windows Terminal
  34.             REM "-d ." flag opens wt.exe in current directory. See https://endjin.com/blog/2020/05/5-tips-for-an-awesome-windows-terminal-experience#open-the-terminal-at-the-current-directory
  35.             REM --size option must come before flag options
  36.  
  37.       REM - Other console window options -
  38. REM         call %CMD_Windows11% --maximized --title CARD --suppressApplicationTitle -d . %CMD_window% /k %App%
  39.             REM Full screen with title bar; multi-tabbed console; dragging window may distort UI
  40.  
  41. REM         call %CMD_Windows11% --fullscreen --title CARD --suppressApplicationTitle -d . %CMD_window% /k %App%
  42.             REM No title bar or tabs; window will start on default monitor and can't be dragged
  43.  
  44. REM         call %CMD_Windows11% --focus --title CARD --suppressApplicationTitle -d . %CMD_window% /k %App%
  45.  
  46.         GOTO END
  47.      ) ELSE (
  48.         cls
  49.         ECHO *** ERROR ***
  50.         ECHO Can't find %CMD_Windows11%.
  51.          ECHO This file is required in order to create the proper command window in Windows 11.
  52.          ECHO This file can be downloaded from the Microsoft Store -
  53.          ECHO    "https://apps.microsoft.com/detail/9n0dx20hk701?hl=en-US&gl=US"
  54.          ECHO For more information see -
  55.          ECHO    "https://learn.microsoft.com/en-us/windows/terminal/install"
  56.          ECHO.
  57.          PAUSE
  58.          )
  59.  
  60.    ) ELSE (
  61.       call %CMD_window% /k %App%
  62.          REM Win10 or lower that don't have multi-tabbed cmd so can set window dims in App
  63.      )
  64.  
  65. ) ELSE (
  66.   cls
  67.   ECHO *** ERROR ***
  68.   ECHO Can't find %App% in the current folder.
  69.    PAUSE
  70.    )
  71.  
  72. :END
  73.  
  74. ENDLOCAL
  75.  
  76. :: PAUSE
  77. exit
  78.  
« Last Edit: April 18, 2025, 09:00:57 pm by dculp »

dculp

  • Full Member
  • ***
  • Posts: 151
this will be done with a Batch File:

- open editor
- type in:

Code: Pascal  [Select][+][-]
  1.   cmd.exe /C
  2.   dir
  3.   pause

- save as foo.bat in your directory.

- open Explorer:  Win-Key + E
- navigate to the Path of foo.bat
- double click foo.bat

- voila:  A command windows will be open without tabs.

Still has tabs when started from file managers Windows Explorer (64bit) and FreeCommander (32bit). However, no tabs when started from file manager Q-Dir (32bit). Curious.

dculp

  • Full Member
  • ***
  • Posts: 151
you could use win32api AllocConsole to make your own Command Line Interface

I grabbed the following but it still opens cmd with tabs. Maybe this needs additional coding?

Code: Pascal  [Select][+][-]
  1. program Get_console_window_10a;
  2. // https://forum.lazarus.freepascal.org/index.php/topic,12234.15.html
  3.  
  4. uses
  5.    Windows;
  6.  
  7. function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll' name 'GetConsoleWindow';
  8.  
  9. var
  10.    hMenuHandle: HMENU;
  11. begin
  12.    AllocConsole();
  13.  
  14.    hMenuHandle := GetSystemMenu(GetConsoleWindow(), FALSE);
  15. //   DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);
  16. //   SetConsoleTitle('MyApp');
  17. //   DrawMenuBar(GetConsoleWindow());
  18.  
  19.    // do something more here
  20.    writeln('OK');
  21.    readln;
  22.  
  23.    FreeConsole();
  24. end.
  25.  

 

TinyPortal © 2005-2018