Page 2 of 3

Posted: Mon Mar 14, 2005 4:09 pm
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

Posted: Mon Mar 14, 2005 4:09 pm
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.

Posted: Mon Mar 14, 2005 4:24 pm
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

Posted: Mon Mar 14, 2005 4:31 pm
by chippy73
El_Choni,

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

Alan

Posted: Mon Mar 14, 2005 5:27 pm
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.

Posted: Mon Mar 14, 2005 7:11 pm
by chippy73
Thanks everyone,

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

Thanks again.

Alan

Re: Positioning DOS window

Posted: Sun May 06, 2018 4:14 am
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]

Re: Positioning DOS window

Posted: Sun May 06, 2018 8:04 am
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).

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 5:15 am
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

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 9:29 am
by Kwai chang caine
Works great here with W7 X86 / v5.61 X86
Thanks 8)

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 11:55 am
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

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 12:55 pm
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   
---------------------------

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 1:18 pm
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?

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 1:30 pm
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.

Re: Positioning DOS window

Posted: Wed Jun 27, 2018 1:55 pm
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.