Toggle window title (caption)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2806
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Toggle window title (caption)

Post by Michael Vogel »

I'd like to toggle the window caption on/off. My workaround is creating a new window but this flickers...

Code: Select all

wx=100
wy=100
w=400
h=250

rect.Rect
win.Rect
dot.Point

CompilerIf Not(Defined(DwmSetWindowAttribute, #PB_Prototype))
	Prototype.i DwmSetWindowAttribute(hWnd.i, dwAttribute.i, pvAttribute.i, cbAttribute.i)
CompilerEndIf

If w>0 And h>0
	sticky=1

	OpenWindow(0,wx,wy,w,h,"",#PB_Window_Invisible|#PB_Window_SystemMenu)
	GetWindowRect_(WindowID(0),win)
	CloseWindow(0)

	OpenWindow(0,wx,wy,w,h,"Screeny Light",#PB_Window_BorderLess|#PB_Window_Invisible)
	GetWindowRect_(WindowID(0),rect)
	zx=(win\right-rect\right)/2
	zy=(win\bottom-rect\bottom-zx)

	CanvasGadget(0,0,0,w,h);,#PB_Canvas_ClipMouse)
	StickyWindow(0,sticky)
	AddKeyboardShortcut(0,#PB_Shortcut_Space,999)
	
	HideWindow(0,0)
	
	Repeat

		Select WaitWindowEvent(5)

		Case #PB_Event_Gadget
			Select EventType()

			EndSelect

		Case #PB_Event_Menu
			If EventMenu()=999

				title!1
				;CloseWindow(0)
				n=#PB_Window_Invisible
				If title
					n|#PB_Window_SystemMenu
				Else
					n|#PB_Window_BorderLess
				EndIf
				OpenWindow(0,wx-zx*title,wy-zy*title,w,h,"Screeny Light by Michael Vogel",n)
				CanvasGadget(0,0,0,w,h);,#PB_Canvas_ClipMouse)
				StickyWindow(0,sticky)
				AddWindowTimer(0,0,250)

				hDwmapi = OpenLibrary(#PB_Any, "dwmapi.dll")
				If hDwmapi
					DwmSetWindowAttribute_.DwmSetWindowAttribute = GetFunction(hDwmapi, "DwmSetWindowAttribute")
					If DwmSetWindowAttribute_
						#DWMWA_TRANSITIONS_FORCEDISABLED=3
						attrib=1
						DwmSetWindowAttribute_(WindowID(0),#DWMWA_TRANSITIONS_FORCEDISABLED,@attrib,SizeOf(attrib))
					EndIf
				EndIf
				CloseLibrary(hDwmapi)
				AddKeyboardShortcut(0,#PB_Shortcut_Space,999)
				HideWindow(0,0)

			EndIf


		Case #PB_Event_CloseWindow
			quit=1

		EndSelect

	Until quit
EndIf
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Toggle window title (caption)

Post by Axolotl »

If you are on windows and not afraid about API..... you can try this:

Code: Select all

Procedure ShowWindowCaption(Window, State)  ; VOID 
  Protected hWnd 
  
  hWnd = WindowID(Window) 
  If State ; == WindowsTitle == On 
    SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) | #WS_CAPTION) 
  Else ; == WindowsTitle == Off 
    SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) & ~#WS_CAPTION) 
  EndIf 
EndProcedure 

Define State 

If OpenWindow(0, 0, 0, 300, 30, "Position of the mouse on the desktop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadget(0, 10, 6, 200, 20, "")

  AddWindowTimer(0, 1, 2000)     
  Repeat
    Event = WaitWindowEvent(20)
    
    If Event = 0 ; No more events in the queue, so let's display the mouse coordinates
      SetGadgetText(0, "Coordinates: " + Str(DesktopMouseX()) + "," + Str(DesktopMouseY()))  
    ElseIf Event = #PB_Event_Timer 
      State ! 1 
      ShowWindowCaption(0, State) 
    EndIf 

  Until Event = #PB_Event_CloseWindow
EndIf
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

Re: Toggle window title (caption)

Post by AZJIO »

Code: Select all

EnableExplicit
#Window = 0
#mDummy1 = 0
Define State = 1
Global Win.RECT
Global WinClient.RECT

Procedure ShowWindowCaption(Window, State)
	Protected hWnd, x, y, w, h
; 	Protected WinRect.RECT

	hWnd = WindowID(Window)
	If State ; == WindowsTitle == On
		SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) | #WS_CAPTION)
		With Win
			ResizeWindow(#Window, \left, \top, \right, \bottom)
		EndWith
	Else ; == WindowsTitle == Off
		SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) & ~#WS_CAPTION)
; 		GetClientRect_(hWnd, @WinRect)
; 		ClientToScreen_(hWnd, @WinRect)
		With WinClient
			ResizeWindow(#Window, \left, \top, \right, \bottom)
		EndWith
	EndIf
EndProcedure


If OpenWindow(#Window, 0, 0, 400, 400, "Position of the mouse on the desktop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	With Win
		\left = WindowX(#Window)
		\top = WindowY(#Window)
		\right = WindowWidth(#Window)
		\bottom = WindowHeight(#Window)
	EndWith
	With WinClient
		\left = WindowX(#Window, #PB_Window_InnerCoordinate)
		\top = WindowY(#Window, #PB_Window_InnerCoordinate)
		\right = WindowWidth(#Window, #PB_Window_InnerCoordinate)
		\bottom = WindowHeight(#Window, #PB_Window_InnerCoordinate)
	EndWith

	AddKeyboardShortcut(#Window, #PB_Shortcut_Space, #mDummy1)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Menu
				Select EventMenu()
					Case #mDummy1
						State ! 1
						ShowWindowCaption(#Window, State)
				EndSelect
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf
User avatar
Michael Vogel
Addict
Addict
Posts: 2806
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Toggle window title (caption)

Post by Michael Vogel »

Thanks, works perfect!
Post Reply