Recent

Author Topic: [Solved] Programmatically placing the console at some position ?  (Read 1617 times)

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Win10, Lazarus trunk.

When I debug an application,  I toggle off Win32 gui application (-WG) off so it is possible to view DebugLn and family function output.

Annoyingly, it is necessary to move the console from under the debugged application window with the mouse.

Is there a way, at the start of running the application, to set programmatically the console window position.
« Last Edit: June 25, 2023, 07:05:01 pm by BrunoK »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Programmatically placing the console at some position ?
« Reply #1 on: June 25, 2023, 02:54:00 pm »
Is there a way, at the start of running the application, to set programmatically the console window position.
If memory serves me correctly (it has been a while for me to have used windows) you can use API calls findwindow and setwindowpos. M$ must have some online examples in c (which can easily be ported back to Pascal).
Today is tomorrow's yesterday.

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: Programmatically placing the console at some position ?
« Reply #2 on: June 25, 2023, 03:09:46 pm »
If memory serves me correctly (it has been a while for me to have used windows) you can use API calls findwindow and setwindowpos. M$ must have some online examples in c (which can easily be ported back to Pascal).
Thanks. Searching in that direction.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Programmatically placing the console at some position ?
« Reply #3 on: June 25, 2023, 03:47:09 pm »
Pfft  :) Took me long enough to come up with this.

For setwindowpos, please do a search for it on the forums here.

BTW: I used to use console API functions such as getconsolewindow but that seems deprecated these days, see f.e. https://learn.microsoft.com/en-us/windows/console/getconsolewindow hence the suggestion to use findwindow as that is more generic. For more information on dropping console API see https://learn.microsoft.com/en-us/windows/console/classic-vs-vt
« Last Edit: June 25, 2023, 03:56:51 pm by TRon »
Today is tomorrow's yesterday.

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: Programmatically placing the console at some position ?
« Reply #4 on: June 25, 2023, 07:04:34 pm »
Thank you TRon. The www.swissdelphicenter.ch stuff has been useful to search info.
I found that I can move the console with :
Code: Pascal  [Select][+][-]
  1.   lConsoleHWND:=Windows.GetConsoleWindow;
  2.   Windows.SetWindowPos(lConsoleHWND, HWND_TOP, -2700, 200, 0, 0, SWP_SHOWWINDOW+SWP_NOSIZE);
  3.   Windows.GetWindowRect(lConsoleHWND, lRect);
I have to figure now out the correct coordinates because my 2 screens are different in size and resolution.

440bx

  • Hero Member
  • *****
  • Posts: 6536
Re: [Solved] Programmatically placing the console at some position ?
« Reply #5 on: June 25, 2023, 10:50:52 pm »
When I debug an application,  I toggle off Win32 gui application (-WG) off so it is possible to view DebugLn and family function output.
That's perfectly fine but, a GUI can dynamically create a console whenever needed for debugging purposes. That spares the programmer from having to change the application type and recompile.  That said, that method requires a little extra work to inform the RTL that a console has been created (otherwise outputting to the console causes an access violation.)  I posted an example of doing it that way some time back, I can find it if you're interested in that method.  Another plus of this method is that the console can be made to appear only when a bug has been detected and the first output line can be a unique string that identifies the code that created it.  This makes it trivial to find the code using "find" for the identifier in a text editor.

Annoyingly, it is necessary to move the console from under the debugged application window with the mouse.

Is there a way, at the start of running the application, to set programmatically the console window position.
Yes, and using SetWindowPos is perfectly fine but, MoveWindow, which is usually simpler can also be used.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018