Executable restarts after END statement

Just starting out? Need help? Post your questions and find answers here.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Executable restarts after END statement

Post by DeanH »

Did a bit of experimenting with Oso's nice code. Thank you for providing. SetWindowPos_ seems to be the function that makes it open properly with the focus on the console. It works as expected with the executable format set to Windows. I can't explain why a console window would open behind the file explorer. For regular windows I use SetForegroundWindow_() and SetActiveWindow() to make sure they are always on top when opened.

Code: Select all

If OpenConsole()
	librno.i=OpenLibrary(#PB_Any,"kernel32.dll")
	hwnd.i=CallFunction(librno.i,"GetConsoleWindow")
	SetWindowPos_(hwnd.i,#Null,-5, 100,#Null,#Null,#SWP_NOSIZE|#SWP_NOZORDER)
	CloseLibrary(librno.i)
	Print("<> ")
	ikey$ = Input()
	End  
EndIf
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Executable restarts after END statement

Post by infratec »

Maybe

Code: Select all

SetForegroundWindow_(hwnd.i)
is enough.
I can not test it, because I can not reproduce the behaviour.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Executable restarts after END statement

Post by netmaestro »

It has to be compiled as a console exe. compiler options->executable format->console then it will work fine.
BERESHEIT
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

netmaestro wrote: Tue Feb 07, 2023 2:11 am It has to be compiled as a console exe. compiler options->executable format->console then it will work fine.
Hi netmaestro. Yes agreed, that appears to ensure that the console assumes a foreground window, though the setting may not always be ideal for what we need. When the Compiler Options - Executable format is changed to Console, the resulting executable opens a normal window when running from the Windows explorer, which is fine, but if you run it from a command (cmd) shell, it doesn't open a new window. Instead, the input/output from the PB console application displays in the existing cmd shell. I accept this is a small point, but it is not ideal if a user executes an application from the command shell, that we want to display in its own window.

It seems that some users' systems don't exhibit the console opening in the background anyway. For me, on PB6.0, it has always happened and looking at BrianW's post and this thread, it is happening for others (perhaps for newer users have have come straight to PB6.0?). I have produced a downloadable video at the below link showing it happening. I start with it set to Windows, then compile and execute the routine. I show it with a small explorer window and then a full-screen explorer window. In both cases, the console application appears behind the explorer window and in the full-screen explorer, the console window is completely obscured. At the end of the video I change to the Console setting and it's fine.

Regarding the Console setting, the below is how it appears when executing from the cmd shell.

Image

This link contains the MP4 — it is a downloadable file, not a streaming site : https://ufile.io/6rzinc9p
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

infratec wrote: Mon Feb 06, 2023 11:01 pm Maybe

Code: Select all

SetForegroundWindow_(hwnd.i)
is enough. I can not test it, because I can not reproduce the behaviour.
You are correct infratec that's all that is required. I just tested it by adding your line to a simple console application. When your line is added, the window always opens in the foreground. When I remove your line, it reverts back to opening the console in the background.

In other words, this works fine...

Code: Select all

; **
; **  Jesse's console code 7-Feb-2023
; **
If OpenConsole()
  SetForegroundWindow_(hwnd.i)
  Print("> ")
  ikey$ = Input()
  End  
EndIf
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

DeanH wrote: Mon Feb 06, 2023 10:50 pm Did a bit of experimenting with Oso's nice code. Thank you for providing. SetWindowPos_ seems to be the function that makes it open properly with the focus on the console. It works as expected with the executable format set to Windows. I can't explain why a console window would open behind the file explorer. For regular windows I use SetForegroundWindow_() and SetActiveWindow() to make sure they are always on top when opened.
Thanks DeanH I can't take the credit for the code, as it is Mijikai's. This code has appeared in many threads over the past few weeks, for one reason or another. It's kind of a universal console, impossible to live without it. :D

I think perhaps the OpenConsole() as a function is less widely used and therefore the behaviour might not have been noticed. For me, I tend to work most of the time on writing back-office server processes, so I develop them with the console to get them working first.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Executable restarts after END statement

Post by DeanH »

I regularly use consoles in little programs that do specific things and not meant to be used by anyone else, like converting data from one database system to another. I'm an old DOS programmer (ZBasic) and before that 8-bit systems like Commodore 64, Amstrad and even TRS-80 model 1. Still frequently use upper case for variable names, out of habit, and command windows a lot. What I haven't stumbled upon is code that can set the number of rows of a console and the font for a console window.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

DeanH wrote: Wed Feb 08, 2023 12:36 am I regularly use consoles in little programs that do specific things and not meant to be used by anyone else, like converting data from one database system to another. I'm an old DOS programmer (ZBasic) and before that 8-bit systems like Commodore 64, Amstrad and even TRS-80 model 1. Still frequently use upper case for variable names, out of habit, and command windows a lot. What I haven't stumbled upon is code that can set the number of rows of a console and the font for a console window.
I agree, consoles seem to incur the least amount of 'getting started' time, with the possible exception of using the debugger for output, which I notice many others do when testing. With more recent versions of Windows, the console actually looks very good with its Consolas font and the 120 x 30 rows. It provides a very workable back-end interface and you can display a lot of data. I'm not sure when that recent Windows console was launched, but it's present on my Windows Server 2016 system, but not on Server 2012. When I tried my console app on Windows 7 recently, I was surprised how archaic it looked with 80 x 25 rows and a less-attractive font. I'm also interested to find out if there is a way to set the window size programmatically — I suspect it might be possible through the API.

Very interesting to hear other folks' backgrounds. I too started in the early 1980s with TRS-80 (Microsoft) Basic but on the EACA Video Genie I computer, which is a clone of the Tandy model II. It was called the Dick Smith System 80 in Aus. I used the Tandy EDTASM assembler quite a bit but have forgotten literally everything :(
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Executable restarts after END statement

Post by blueb »

@Dean H
What I haven't stumbled upon is code that can set the number of rows of a console and the font for a console window.
NOTE: As far as setting 'rows'... I found some very old code that runs in ver. 5.46, but haven't had time to see what the problem is in 6.01.
PM me and I'll send it to you.

Here's how to adjust the console font...

Code: Select all

; =================================================================
;
;        Author:       Mijikai 
;          Date:       July 3, 2020
;       Explain:       Change Console fonts
;           
; Typical Usage:
;
;     File Name:       Change Console fonts.pb   
; =================================================================

EnableExplicit
;DisableExplicit

EnableExplicit

Import "kernel32.lib"
  SetCurrentConsoleFontEx.i(hConsoleOutput.i,bMaximumWindow.i,*lpConsoleCurrentFontEx)
EndImport

Structure CONSOLE_FONT_INFO_EX
  cbSize.l
  nFont.l
  dwFontSize.COORD
  FontFamily.l
  FontWeight.l
  FaceName.w[#LF_FACESIZE]
EndStructure

Procedure.i ConsoleFont(*Buffer,BufferSize.i,FontName.s,FontSize.i)
  Protected cfiex.CONSOLE_FONT_INFO_EX
  Protected res.i
  Protected length.i
  Protected font_resource.i
  With cfiex
    length = Len(FontName)
    If length < #LF_FACESIZE
      \cbSize = SizeOf(CONSOLE_FONT_INFO_EX)
      \nFont = #Null
      \dwFontSize\x = #Null
      \dwFontSize\y = FontSize
      \FontFamily = #FF_DONTCARE
      \FontWeight = #FW_DONTCARE
      If *Buffer And BufferSize
        AddFontMemResourceEx_(*Buffer,BufferSize,#Null,@res)
      EndIf
      length << 1
      CopyMemory(@FontName,@\FaceName[0],length)
      ProcedureReturn SetCurrentConsoleFontEx(GetStdHandle_(#STD_OUTPUT_HANDLE),#False,@cfiex)
    EndIf
    ProcedureReturn #False
  EndWith
EndProcedure

Procedure.i Main()
  If OpenConsole()
    Debug ConsoleFont(#Null,#Null,"Lucida Console",40)
    PrintN("Hello World!")
    Input()
     Debug ConsoleFont(#Null,#Null,"Lucida Console",10)
    PrintN("Teststring!")
    Input()
    CloseConsole()
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Executable restarts after END statement

Post by infratec »

User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Executable restarts after END statement

Post by DeanH »

That's great! Thankyou! I also just found some code that can change the width and height of a console. Modified from RSBasic's very useful Win API Library.

Code: Select all

EnableExplicit

Procedure ResizeConsole(width.i,height.i)
	Protected stdout.i,mySize.i
	Protected NewSize.SMALL_RECT
	stdout=GetStdHandle_(#STD_OUTPUT_HANDLE)  
	NewSize.SMALL_RECT
	NewSize\top = 0
	NewSize\left = 0
	NewSize\bottom=height
	NewSize\right=width
	SetConsoleWindowInfo_(stdout, 1, @NewSize)
	mySize = ((height+1) << 16) + (width+1)
	SetConsoleScreenBufferSize_(stdout, mySize) 
EndProcedure

If OpenConsole()
	ResizeConsole(90, 35)
	Input()
	CloseConsole()
EndIf
Post Reply