Positioning DOS window

Windows specific forum
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Platform SDK: DLLs, Processes, and Threads
GetConsoleWindow

The GetConsoleWindow function retrieves the window handle used by the console associated with the calling process.

HWND GetConsoleWindow(void);
Parameters
This function has no parameters.
Return Values

The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.
Remarks

To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0500 or later. For more information, see Using the Windows Headers.
Requirements
Client Requires Windows XP or Windows 2000 Professional.
Server Requires Windows Server 2003 or Windows 2000 Server.
Header

Declared in Wincon.h; include Windows.h.
Library

Link to Kernel32.lib.
DLL Requires Kernel32.dll.
See Also

Character-Mode Applications Overview, Console Functions
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Forgot to embed a thread id check.
I pass GetCurrentThreadId_() but i do nnot compare the hWnd being created in this thread.

It's easier to set a truly odd title and abandon my thread id stuff.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

chippy73
Sometimes debuging your values will help you alot

Code: Select all

hDosWin = FindWindow_(#Null, "edit.com")
Debug hDosWin
hDosWin returns 0 on wim98

But

Code: Select all

hDosWin = FindWindow_(#Null, "MS-DOS Editor")
Debug hDosWin
is valid

Use Freaks

Code: Select all

SendMessage_(hDosWin, #WM_CLOSE, 0, 0)
to close the window

This workes on win98

Code: Select all

If OpenWindow(0, 580, 0, 250, 100, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Test") And CreateGadgetList(WindowID(0))
  ;--> Just to keep our window on top
  SetWindowPos_(WindowID(0), #HWND_TOPMOST, -1, -1, -1, -1, #SWP_NOMOVE | #SWP_NOSIZE | #SWP_SHOWWINDOW)
  ButtonGadget(1, 75, 10, 100, 20, "close Dos Window")
  RunProgram("edit.com")
     
  Repeat
    event = WaitWindowEvent()
    If event = #PB_EventGadget And EventGadgetID() = 1
      ; hDosWin = FindWindow_(#Null, "edit.com")
       
       ;Edit's name under win98 is "MS-DOS Editor" 
       hDosWin = FindWindow_(#Null, "MS-DOS Editor")
       Debug hDosWin
       SendMessage_(hDosWin, #WM_CLOSE, 0, 0) 
 
 ;     DestroyWindow_(hDosWin)
    EndIf
  Until event = #PB_Event_CloseWindow
EndIf
End
Henrik
chippy73
User
User
Posts: 59
Joined: Wed Feb 16, 2005 6:11 pm
Location: S.W.Wales

Post by chippy73 »

El_Choni,

Many thanks for solution. It works OK. Now on to my next problem !!

Alan
PB v3.94 PVXP v2.13 JaPBe v2.5.4.22
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Why do i have the feeling you totally skipped my version?
Looking for a non-unique string as above is not the best way.
chippy73
User
User
Posts: 59
Joined: Wed Feb 16, 2005 6:11 pm
Location: S.W.Wales

Post by chippy73 »

Thanks everyone,

That's a lot of useful info and have saved it for future reference.

Thanks again.

Alan
PB v3.94 PVXP v2.13 JaPBe v2.5.4.22
Poplar
User
User
Posts: 16
Joined: Sun Apr 30, 2017 12:27 pm

Re: Positioning DOS window

Post by Poplar »

Code updated For 5.62

Code: Select all

RunProgram("edit.com")
DosWindowTitle$ = "C:\Windows\system32\edit.com"
Delay(2000)
hDosWin = FindWindow_("ConsoleWindowClass", DosWindowTitle$)
Debug hDosWin 

SetWindowPos_(hDosWin, 0, 10, 100, 600,400, #SWP_SHOWWINDOW)

Delay(2000)
SendMessage_(hDosWin, #WM_CLOSE, 0, 0)
[/color]
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Positioning DOS window

Post by Dude »

Poplar wrote:Code updated For 5.62
Hmm, it doesn't close the DOS window on Win XP; and doesn't even work on Vista and later at all (no DOS window gets opened because "edit.com" doesn't exist).
User avatar
Pierre Bellisle
User
User
Posts: 35
Joined: Wed Jun 27, 2018 5:12 am

Re: Positioning DOS window

Post by Pierre Bellisle »

What if you try those APIs...

Code: Select all

EnableExplicit

#SM_CXSCREEN = 0
#SM_CYSCREEN = 1

Import "kernel32.lib"
  GetConsoleWindow()
EndImport
  
Define.rect ConsoleRect
Define.l WindowWidth
Define.l WindowHeight
Define.l hConsole
Define.Size ConsoleSize

OpenConsole()                               
ConsoleTitle ("PureBasic - Center console on screen") 
RunProgram("Cmd.exe")
hConsole = GetConsoleWindow()                                         ;Get console handle
GetWindowRect_(hConsole, ConsoleRect)                                 ;Get console rectangle
ConsoleSize\cx = ConsoleRect\Right  - ConsoleRect\Left                ;Compute console width
ConsoleSize\cy = ConsoleRect\Bottom - ConsoleRect\Top                 ;Compute console height
SetWindowPos_(hConsole, #HWND_TOP,                                    ;Console handle. top zOrder
              (GetSystemMetrics_(#SM_CXSCREEN) - ConsoleSize\cx) / 2, ;Compute console x center, GetSystemMetrics_() retreive screen size
              (GetSystemMetrics_(#SM_CYSCREEN) - ConsoleSize\cy) / 2, ;Compute console y center, GetSystemMetrics_() retreive screen size
              ConsoleSize\cx, ConsoleSize\cy, #SWP_SHOWWINDOW)        ;SizeX, SizeY, Show the window
CloseConsole()
End
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Positioning DOS window

Post by Kwai chang caine »

Works great here with W7 X86 / v5.61 X86
Thanks 8)
ImageThe happiness is a road...
Not a destination
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Positioning DOS window

Post by Mijikai »

Here is my version :)

Code: Select all

Import "kernel32.lib"
  GetConsoleWindow_() As "GetConsoleWindow"
EndImport

Procedure.i SetConsolePosition(X.i,Y.i)
  ProcedureReturn SetWindowPos_(GetConsoleWindow_(),#Null,X,Y,#Null,#Null,#SWP_NOZORDER|#SWP_NOSIZE|#SWP_SHOWWINDOW)
EndProcedure

Procedure.i SetConsolePosition_XP(X.i,Y.i)
  Protected WindowHandle.i
  Protected Buffer.s = Space(1024)
  If GetConsoleTitle_(@Buffer,1024)
    WindowHandle = FindWindow_(#Null,Buffer)
    If WindowHandle
      ProcedureReturn SetWindowPos_(WindowHandle,#Null,X,Y,#Null,#Null,#SWP_NOZORDER|#SWP_NOSIZE|#SWP_SHOWWINDOW)
    EndIf
  EndIf
EndProcedure

If OpenConsole()
  SetConsolePosition(10,10)
  ;SetConsolePosition_XP(10,10)
  Input()
EndIf
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Positioning DOS window

Post by Dude »

Mijikai, your version doesn't work for me with Win 7:

Code: Select all

---------------------------
PureBasic - Linker error
---------------------------
POLINK: error: Unresolved external symbol 'GetConsoleWindow'.
POLINK: fatal error: 1 unresolved external(s).
---------------------------
OK   
---------------------------
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Positioning DOS window

Post by Mijikai »

Dude wrote:Mijikai, your version doesn't work for me with Win 7...
Should work, thats the OS im currently using.
Mby some silly AV is blocking?
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Positioning DOS window

Post by blueb »

Mijikai....

Tested on Windows 10 Pro:
5.46 LTS (x86)
5.62 (x86)
5.62 (x64)

5.70 b1 (x86)


Polink errors on all versions... except 5.62 (x64)

It worked.
- 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
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Positioning DOS window

Post by Mijikai »

blueb wrote:Mijikai....

Tested on Windows 10 Pro:
5.46 LTS (x86)
5.62 (x86)
5.62 (x64)

5.70 b1 (x86)


Polink errors on all versions... except 5.62 (x64)

It worked.
I also did some testing
the PB version i used on my old PC is 5.60
-> x64 is working and x86 is not
so clearly PB has a Bug.
Post Reply