Console invisible or hide [WINDOWS ONLY]

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Console invisible or hide [WINDOWS ONLY]

Post by minimy »

Hi friends! Many time searching how make invisible the console window. Well, here is a little trip.

Code: Select all

OpenConsole("MyConsole"):Delay(1000)
Opacity= 100:hwnd = FindWindow_(0,"MyConsole"); put 0 to invisible, 255 opaque
SetWindowLong_(hwnd, #GWL_EXSTYLE, $00080000):If OpenLibrary(1, "user32.dll"):CallFunction(1, "SetLayeredWindowAttributes", hwnd, 0, Opacity, 2):CloseLibrary(1):EndIf 
Input()
I hope you enjoy!!
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Console invisible or hide [WINDOWS ONLY]

Post by netmaestro »

Are you using the demo version? If not, that stuff is native. Also, there's a better way to get the console hwnd:

Code: Select all

Import ""
  GetConsoleWindow_() As "_GetConsoleWindow@0"
EndImport

OpenConsole("MyConsole")
hwnd = GetConsoleWindow_()
Opacity= 100 ; put 0 To invisible, 255 opaque
SetWindowLong_(hwnd, #GWL_EXSTYLE, #WS_EX_LAYERED) : SetLayeredWindowAttributes_(hwnd, 0, Opacity, 2)
Input()
GetConsoleWindow is in kernel32.lib which is always open so a null import is all you need.
BERESHEIT
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Console invisible or hide [WINDOWS ONLY]

Post by minimy »

netmaestro wrote:Are you using the demo version? If not, that stuff is native. Also, there's a better way to get the console hwnd:

Code: Select all

Import ""
  GetConsoleWindow_() As "_GetConsoleWindow@0"
EndImport

OpenConsole("MyConsole")
hwnd = GetConsoleWindow_()
Opacity= 100 ; put 0 To invisible, 255 opaque
SetWindowLong_(hwnd, #GWL_EXSTYLE, #WS_EX_LAYERED) : SetLayeredWindowAttributes_(hwnd, 0, Opacity, 2)
Input()
GetConsoleWindow is in kernel32.lib which is always open so a null import is all you need.
Hi NetMaestro, Thanks for your time!
Not.. :shock: Im using 5.21LTS, Im registered user! Have 5.22 but not installed because have Pure_color and others.. :D
And i have to say, im very happy with the price and product! (Thanks Fred & Cia.!) :D

Sorry NetMaestro, but is the same to get the hwnd of the window? I think, can you explain a little why is better the other way? (I dont know) :?:

I try this other way with your code:

Code: Select all

Import "":GetConsoleWindow_() As "_GetConsoleWindow@0":EndImport
win=OpenWindow(#PB_Any,0,0,300,100,"Dummy Invisible",#PB_Window_Invisible)
OpenConsole("MyConsole") : hwnd = GetConsoleWindow_() : SetParent_(hwnd,WindowID(win))
Input()
CloseWindow(win):End
No task in taskbar with this code. This is the right way to do this?

Nunca te acostarás sin saber algo nuevo. (Spanish proverb) :wink:
If translation=Error: reply="Sorry, Im Spanish": Endif
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Console invisible or hide [WINDOWS ONLY]

Post by Little John »

This looks pretty cool. 8) Thanks to both of you!

I've updated netmaestro's code, so that it runs with the most recent PureBasic x86 and x64 versions on Windows 11:

Code: Select all

; PB 6.03 beta 5

Import ""
   CompilerIf #PB_Compiler_32Bit
      GetConsoleWindow_() As "_GetConsoleWindow@0"
   CompilerElseIf #PB_Compiler_64Bit
      GetConsoleWindow_() As "GetConsoleWindow"
   CompilerEndIf   
EndImport

OpenConsole("MyConsole")
hwnd = GetConsoleWindow_()
Opacity = 160    ; 0: invisible, 255: opaque
SetWindowLongPtr_(hwnd, #GWL_EXSTYLE, #WS_EX_LAYERED)
SetLayeredWindowAttributes_(hwnd, 0, Opacity, 2)
Input()
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Console invisible or hide [WINDOWS ONLY]

Post by Michael Vogel »

The GetConsoleWindow function does only work when creating a 'Windows' executable, when using 'Console' FindWindow must be used (as seen in the first post and below, keep in mind not to remove the delay command).

Creating a console executable seems to have other limitations as well, I am not able to remove the window titlebar (GWL_STYLE)...

Code: Select all

OpenConsole("MyConsole")
Delay(100)

Opacity=200
hwnd=FindWindow_(0,"MyConsole")

SetWindowLong_(hwnd, #GWL_EXSTYLE, $00080000)
If OpenLibrary(1, "user32.dll")
	CallFunction(1, "SetLayeredWindowAttributes", hwnd, 0, Opacity, 2)
	CloseLibrary(1)
EndIf

ShowWindow_(hwnd,#SW_MAXIMIZE)
SetWindowLongPtr_(hwnd,#GWL_STYLE,GetWindowLongPtr_(hwnd,#GWL_STYLE)&~#WS_THICKFRAME&~#WS_DLGFRAME)

PrintN("Done.")
Input()
Post Reply