[DONE] Console WinApi - Resize and Move the Window

Just starting out? Need help? Post your questions and find answers here.
User avatar
tft
User
User
Posts: 84
Joined: Mon Dec 29, 2008 9:34 am

[DONE] Console WinApi - Resize and Move the Window

Post by tft »

I'm looking for a way to move the console. Something like ResizeWindow().
But somehow that doesn't work with consoles.

I found this module. But it doesn't work. I want to move the console window.

Code: Select all

DeclareModule CONSOLE
  Declare.i Create()
  Declare.i Resize(Width.i,Height.i,FontWidth.i,FontHeight)
EndDeclareModule

Module CONSOLE
  
  EnableExplicit
  
  Import "kernel32.lib"
    SetCurrentConsoleFontEx.i(hConsoleOutput.i,bMaximumWindow.b,*lpConsoleCurrentFontEx)
    GetConsoleScreenBufferInfoEx.i(hConsoleOutput.i,*lpConsoleScreenBufferInfoEx)
  EndImport
  
  Structure CONSOLE_FONT_INFOEX
    cbSize.l
    nFont.l
    dwFontSize.COORD
    FontFamily.l
    FontWeight.l
    FaceName.u[#LF_FACESIZE]
  EndStructure

  Structure CONSOLE_COORD_STRUCT
    StructureUnion 
      value.l
      coord.COORD
    EndStructureUnion
  EndStructure
  
  Structure CONSOLE_STRUCT
    std_in.i
    std_out.i
  EndStructure
  
  Global console.CONSOLE_STRUCT
  
  Procedure.i Create()
    FreeConsole_()
    If AllocConsole_()
      console\std_in = GetStdHandle_(#STD_INPUT_HANDLE)
      console\std_out = GetStdHandle_(#STD_OUTPUT_HANDLE)
      ProcedureReturn #True
    EndIf
    ProcedureReturn #False
  EndProcedure

  Procedure.i Resize(Width.i,Height.i,FontWidth.i,FontHeight)
    Protected wnd_rect.SMALL_RECT
    Protected scr_size.CONSOLE_COORD_STRUCT
    Protected font.CONSOLE_FONT_INFOEX
    Protected font_name.s
    Protected font_bytes.i
    Protected scr_info.CONSOLE_SCREEN_BUFFER_INFO
    wnd_rect\right = 1
    wnd_rect\bottom = 1
    If SetConsoleWindowInfo_(console\std_out,#True,@wnd_rect);<- make window as small as possible for resize
      scr_size\coord\x = Width
      scr_size\coord\y = Height
      If SetConsoleScreenBufferSize_(console\std_out,scr_size\value);<- create the screen buffer
        If SetConsoleActiveScreenBuffer_(console\std_out);<- use the created screen buffer
          font\cbSize = SizeOf(CONSOLE_FONT_INFOEX)
          font\dwFontSize\x = FontWidth
          font\dwFontSize\y = FontHeight
          font\FontFamily = #FF_DONTCARE
          font\FontWeight = #FW_NORMAL
          font_name = "Consolas"
          font_bytes = (Len(font_name) + 1) * SizeOf(Character)
          If Not font_bytes > #LF_FACESIZE
            CopyMemory(@font_name,@font\FaceName[0],font_bytes)
            If SetCurrentConsoleFontEx(console\std_out,#False,@font);<- set console font 

              ;------ EVERYTHING SEEMS TO WORK UNTIL HERE!!!------

              If GetConsoleScreenBufferInfo_(console\std_out,@scr_info);<- get current info
                
                Debug scr_info\dwMaximumWindowSize\x;<- why? (should show allowed screem size)?
                Debug scr_info\dwMaximumWindowSize\y;<- why?
                

                wnd_rect\right = Width
                wnd_rect\bottom = Height
                
                Debug SetConsoleWindowInfo_(console\std_out,#True,@wnd_rect);why? <- should resize the window!?
             
              EndIf
            EndIf
          EndIf
        EndIf
      EndIf
    EndIf
    ProcedureReturn #False
  EndProcedure
  
EndModule

CONSOLE::Create()
CONSOLE::Resize(1080,720,16,16)

Repeat
  Delay(1)
ForEver
Last edited by tft on Tue Aug 16, 2022 12:30 am, edited 1 time in total.
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Help] Console WinApi - Resize and Move the Window

Post by mk-soft »

...

Code: Select all

Import ""
  GetConsoleWindow(Void)
EndImport

r1 = OpenConsole()
If r1
  hWnd = GetConsoleWindow(0)
  MoveWindow_(hWnd, 600, 200, 800, 600, 1)
  
  Print("Geben Sie Ihren Namen ein und druecken Sie Return: ")
  Name$ = Input()

  PrintN("Hallo " + Name$ + ", nett Sie zu treffen.")
  PrintN("Druecken Sie Return zum Beenden.")
  Input()
EndIf
I wanted to find out how to change the font ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
tft
User
User
Posts: 84
Joined: Mon Dec 29, 2008 9:34 am

Re: [Help] Console WinApi - Resize and Move the Window

Post by tft »

verry nice

THX TFT
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
tft
User
User
Posts: 84
Joined: Mon Dec 29, 2008 9:34 am

Re: [DONE] Console WinApi - Resize and Move the Window

Post by tft »

for replace Font from Console ......

Code: Select all

;{ Console

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

Import ""
  GetConsoleWindow(Void)
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
;}

OpenConsole("SYST")
ConsoleFont(#Null,#Null,"Lucida Console",20)

hWnd = GetConsoleWindow(0)
MoveWindow_(hWnd, 600, 200, 800, 600, 1)
TFT seid 1989
Aktuelles Projekte : Driving School Evergarden
YouTube : Pure Basic to go
FaceBook : Temuçin SourceMagic Games
DISCORD : SourceMagic
W10 , i9 9900K ,32 GB Ram , GTX Titan , 3 Monitore FHD
ARDUINO Freak :-)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [DONE] Console WinApi - Resize and Move the Window

Post by mk-soft »

nice,

but you can use static string for font name.

Code: Select all

;{ Console

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

Import ""
  GetConsoleWindow(Void)
EndImport

Structure CONSOLE_FONT_INFO_EX
  cbSize.l
  nFont.l
  dwFontSize.COORD
  FontFamily.l
  FontWeight.l
  FaceName.s{#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)
      \FaceName = FontName
      ProcedureReturn SetCurrentConsoleFontEx(GetStdHandle_(#STD_OUTPUT_HANDLE),#False,@cfiex)
    EndIf
    ProcedureReturn #False
  EndWith
EndProcedure
;}

If OpenConsole("SYST")
  ConsoleFont(#Null,#Null,"Lucida Console",20)
  
  hWnd = GetConsoleWindow(0)
  MoveWindow_(hWnd, 600, 200, 800, 600, 1)
  PrintN("Hello World!")
  Input()
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply