Page 1 of 1

[Windows] Some thing to do with invisible windows

Posted: Sat Jul 09, 2011 4:02 pm
by Nituvious
I am terrible with thread topic names.. Anyway, I was going through my Folder of Folders and cleaning it up a bit when I came across this bit of code that I made a while back. It displays a drawn image and moves it around the mouse cursor. I'm sure there are better ways of doing this, though. But the Transparent window is pretty neat to have.

Code: Select all

; last modified: March 6th, 2011 @ 9:38:56 AM

;IncludeFile "WinAPI.pbi"
Procedure OpenTransparentWindow(transColor, x, y, width, height) ;-- Create an invisible, borderless, and transparent window
	; HideWindow(winResult, 0) after gadgets have been placed. Otherwise you may see flicker.
	; I do not know if this works on all versions of windows or just Vista / 7
	; I have only tested on both x86 and x64 Windows 7
	
	winResult = OpenWindow(#PB_Any, 1, 1, 1, 1, "",#PB_Window_BorderLess| #PB_Window_Invisible)
	SetWindowColor(winResult,transColor)

	SetWindowLongPtr_(WindowID(winResult),#GWL_EXSTYLE,  #WS_EX_LAYERED | #WS_EX_TOOLWINDOW | #WS_EX_TOPMOST)  ; -- According to the MSDN, this function will work with both x86 and x64 ( http://msdn.microsoft.com/en-us/library/ms644898(v=VS.85).aspx )
	SetWindowPos_(WindowID(winResult),#Null, x, y, width, height, #Null) ; -- Must redraw window, according to the MSDN

	SetLayeredWindowAttributes_(WindowID(winResult),transColor,#Null,#LWA_COLORKEY)

	ProcedureReturn winResult

EndProcedure

ExamineDesktops()

window = OpenTransparentWindow(RGB(254,0,0),0,0,DesktopWidth(0),DesktopHeight(0))
StickyWindow(window,1)
HideWindow(window,0)

Structure pt
	x.l
	y.l
EndStructure
msPos.pt

Repeat : eventID = WaitWindowEvent(1)
	Delay(1)
	GetCursorPos_(msPos)
	s.f = 20 * Sin(a.f) : c.f = 20 * Cos(a.f) : s2.f = 10 * Sin(a2.f)
	c2.f = 10 * Cos(a2.f) : s3.f = 25 * Sin(a3.f) : c3.f = 25 * Cos(a3.f)
	StartDrawing(WindowOutput(window))
		Circle(msPos\x+s,msPos\y+c, 2, RGB(Random(255),Random(255),Random(255)))
		Circle(msPos\x+s+s2,msPos\y+c+c2, 4, RGB(Random(255),Random(255),Random(255)))
		Circle(msPos\x+s+s2+c3,msPos\y+c+c2+s3, 4, RGB(50,0,0))
		Circle(msPos\x+s+s2+c3,msPos\y+c+c2+s3, 3, RGB(100,0,0))
		Circle(msPos\x+s+s2+c3,msPos\y+c+c2+s3, 2, RGB(200,0,0))
	StopDrawing()
	InvalidateRect_(WindowID(window),0,#True)
	a.f + 0.03 : a2.f + 0.08 : a3.f + 0.1
Until eventID = #PB_Event_CloseWindow ; -- LOL
[edit] omg, im really dumb. Originally posted around IMG tags, then edited to URL tags..................

Re: [Windows] Some thing to do with invisible windows

Posted: Sat Jul 09, 2011 6:48 pm
by netmaestro
There's nothing wrong at all with your code, thanks for sharing it. But I hope you won't mind if I have a kick at this fun little cat. Alt-Space ends the program:

Code: Select all

Global KeyboardHook, Pulse, kill=0
#LLKHF_ALTDOWN = $20

CompilerIf Defined(KBDLLHOOKSTRUCT, #PB_Structure) = 0
  Structure KBDLLHOOKSTRUCT
    vkCode.l
    scanCode.l
    flags.l
    time.l
    dwExtraInfo.l
  EndStructure
CompilerEndIf

Procedure OpenTransparentWindow()
  winResult = OpenWindow(#PB_Any, 0, 0, 150, 150, "",#PB_Window_BorderLess| #PB_Window_Invisible)
  SetWindowLongPtr_(WindowID(winResult),#GWL_EXSTYLE,  #WS_EX_LAYERED | #WS_EX_TOOLWINDOW | #WS_EX_TOPMOST) 
  ProcedureReturn winResult
EndProcedure

Procedure Draw(uID, uMsg, dwUser, dw1, dw2)
  window=dwUser
  Static a.f, a2.f, a3.f
  If Not kill
    s.f = 20 * Sin(a.f)+25 : c.f = 20 * Cos(a.f) +25: s2.f = 10 * Sin(a2.f)+25
    c2.f = 10 * Cos(a2.f) +25: s3.f = 25 * Sin(a3.f) +25: c3.f = 25 * Cos(a3.f)+25
    hdc = StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_AllChannels)
      Box(0,0,150,150,RGBA(0,0,0,0))
      Circle(s,c,2, RGBA(Random(255),Random(255),Random(255),255))
      Circle(s+s2,c+c2, 4, RGBA(Random(255),Random(255),Random(255),255))
      Circle(s+s2+c3,c+c2+s3, 4, RGBA(50,0,0,255))
      Circle(s+s2+c3,c+c2+s3, 3, RGBA(100,0,0,255))
      Circle(s+s2+c3,c+c2+s3, 2, RGBA(200,0,0,255))
      sz.SIZE\cx=150:sz\cy=150
      ContextOffset.POINT 
      BlendMode.BLENDFUNCTION 
      BlendMode\SourceConstantAlpha = 255 
      BlendMode\AlphaFormat = 1
      UpdateLayeredWindow_(WindowID(window), 0, 0, @sz, hDC, @ContextOffset, 0, @BlendMode, 2)
    StopDrawing()
    a.f + 0.03 : a2.f + 0.08 : a3.f + 0.1
    GetCursorPos_(msPos.point)
    ResizeWindow(window,msPos\x+10,msPos\y+10,150,150)
  EndIf
EndProcedure

Procedure KeyboardProc(ncode,wparam,lparam)
  If ncode<0
    ProcedureReturn CallNextHookEx_( KeyboardHook, nCode, wParam, lParam )
  EndIf
  *keycode.KBDLLHOOKSTRUCT=lparam
  If *keycode\vkCode = #VK_SPACE
    If *keycode\flags & #LLKHF_ALTDOWN ; Check for ALT-SPACE key combo
      kill=1                           ; Trigger program end
      ProcedureReturn -1               ; Prevent ALT-SPACE going on to program with keyboard focus
    EndIf
  EndIf
  ProcedureReturn CallNextHookEx_( KeyboardHook, nCode, wParam, lParam )
EndProcedure

CreateImage(0,150,150,32|#PB_Image_Transparent)

window = OpenTransparentWindow()
StickyWindow(window,1)
HideWindow(window,0)

KeyboardHook = SetWindowsHookEx_(#WH_KEYBOARD_LL,@KeyboardProc(),GetModuleHandle_(0),0)

Pulse = timeSetEvent_(7,1,@Draw(),window,#TIME_PERIODIC)

Repeat
  eventID = WaitWindowEvent(1)
Until kill

timeKillEvent_(Pulse)
Delay(1)
End

Re: [Windows] Some thing to do with invisible windows

Posted: Sat Jul 09, 2011 7:05 pm
by netmaestro
Hehe, it does get somewhat annoying if you leave it going while you work on your computer. One way you could earn some points would be to use a semi-transparent picture of your significant other instead of the moving balls. Then your sweetness is sure to be rewarded someway or other :mrgreen:

Re: [Windows] Some thing to do with invisible windows

Posted: Sun Jul 10, 2011 6:12 pm
by Kwai chang caine
Too funny, i love this little balls :D
Thanks a lot at you two for sharing 8)

Re: [Windows] Some thing to do with invisible windows

Posted: Sun Jul 10, 2011 6:53 pm
by rsts
Fun. Thanks for the diversion.

(Mr netmaestro's code displays on multi screen set-ups. Mr Nituvious' code only on the primary display).

cheers