Saturday, 13 June 2020

GetCurrentConsoleWindowRect

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

Use this to get the current console rect.

REM GetCurrentConsoleWindowRect.bat
REM This file compiles GetCurrentConsoleWindowRect.vb to GetWindowRect.exe
REM GetCurrentConsoleWindowRect.exe reports on console's windows position
REM To use 
REM GetCurrentConsoleWindowRect
REM EG
REM GetCurrentConsoleWindowRect
REM
REM Change /target:exe to /target:winexe and uncomment the 
REM msgbox line in main file to make it a non console program
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetCurrentConsoleWindowRect.exe" "%~dp0\GetCurrentConsoleWindowRect.vb"
pause

'GetCurrentConsoleWindowRect.vb
imports System.Runtime.InteropServices 
Public Module GetWindowRect  

   _
 Private Structure RECTL  
  Public Left As Int32
  Public Top As Int32
  Public Right As Int32
  Public Bottom As Int32
 End Structure

 Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd as IntPtr, ByRef Rect as RectL) as Integer
 Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
 Public Declare UNICODE Function GetConsoleTitleW Lib "kernel32" (ByVal lpConsoleTitle As String, ByVal nSize As Integer) As Integer

 Sub Main
  On Error Resume Next
  Dim hWindows as IntPtr
  Dim Ret as Integer
  Dim ConsoleTitle as String
  Dim Size as Integer
  ConsoleTitle = StrDup(1024, ChrW(0))
  Size = 1020
  Ret = GetConsoleTitleW(ConsoleTitle, Size)
  
  hwindows = FindWindowW(vbNullString, ConsoleTitle)
  If hwindows = 0 then
   Msgbox(Command() & " cannot be found.")
  Else
   Dim x as RectL
   Ret = GetWindowRect(hWindows, x)
   If Ret = 0 Then 
    MsgBox("GetWindowRect Error " & Err.LastDllError)
   Else
    'Uncomment the MsgBox line if using as non console program
    'Msgbox(x.left & " " & x.top & " " & x.right & " " & x.bottom)
    Console.Writeline(x.left & " " & x.top & " " & x.right & " " & x.bottom)
   End If
  End If
 End Sub

End Module 

No comments:

Post a Comment