Click on window while user use mouse

Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Click on window while user use mouse

Post by srod »

Sorry, you've lost me... why click into a client area? Will the Java app respond to the click?

To click into a client-area then use something like :

Code: Select all

SendMessage_(hWnd, #WM_LBUTTONDOWN, #MK_LBUTTON, x + y<<16)
SendMessage_(hWnd, #WM_LBUTTONUP, #MK_LBUTTON, x + y<<16)
or use PostMessage_() in place of send message.

You may need to bring the window into the foreground first with SetForegroundWindow_() etc.
I may look like a mule, but I'm not a complete ass.
oftit
User
User
Posts: 52
Joined: Sun Apr 25, 2010 5:08 am

Re: Click on window while user use mouse

Post by oftit »

I've just seen it somewhere before so I thought it was possible?
I think I'll have to use this posted by RASHAD:

Code: Select all

RunProgram("notepad.exe")
Repeat
  hWnd = FindWindow_(0, "Untitled - Notepad")
Until hWnd
ShowWindow_(hWnd, #SW_SHOWMAXIMIZED)
GetCursorPos_(cp.POINT)
SetCursorPos_(5,29)
mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
SetCursorPos_(cp\x,cp\y)
??
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Click on window while user use mouse

Post by srod »

Well, I'm still not sure what it is you are trying to accomplish... I mean why is it you want to simulate a client-area click? What is it that you hope to achieve by this? What will be the consequence of such an action?

Some clear answers to these questions would make life a lot easier.

Sending messages works fine here with a litte program I threw up. I can click a button in a separate application by either sending command messages to the parent window or mouse-messages direct to the button etc. Works fine.

Anyhow...
I may look like a mule, but I'm not a complete ass.
oftit
User
User
Posts: 52
Joined: Sun Apr 25, 2010 5:08 am

Re: Click on window while user use mouse

Post by oftit »

Ok.

Forget about. It was a stupid question anyway.

Thank you though
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Click on window while user use mouse

Post by Michael Vogel »

I'd need to simulate a click but interestingly I must do simulate two clicks to get a single click working.

Below is the code which waits until the key 'c' gets pressed. Then a crosshair should be seen following the mouse position. A left mouse click should hide the crosshair and do the click.

Code: Select all

; Define

	EnableExplicit

	Enumeration
		#Win
	EndEnumeration

	Enumeration
		#Snapshot
		#Image
	EndEnumeration

	Structure FrameType
		Mode.i
		Left.i
		Top.i
		Right.i
		Bottom.i
		Width.i
		Height.i
	EndStructure

	#DpiBits=16

	Global Frame.FrameType
	Global Scale=GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)<<#DpiBits/96
	;Global Scale=96<<#DpiBits/GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)
	Global SX=GetSystemMetrics_(#SM_CXSCREEN)
	Global SY=GetSystemMetrics_(#SM_CYSCREEN)

	Global Dim Inputs.INPUT(0)

; EndDefine

Procedure Max(a,b)

	If a<b
		ProcedureReturn b
	Else
		ProcedureReturn a
	EndIf

EndProcedure
Procedure ClickMouse()

	Protected ret

	BlockInput_(#True)
	Inputs(0)\type = #INPUT_MOUSE
	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTDOWN

	SendInput_(1, @Inputs(), SizeOf(INPUT))
	Sleep_(20)

	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTUP
	ret=SendInput_(1, @Inputs(), SizeOf(INPUT))

	BlockInput_(#False)

	ProcedureReturn ret

EndProcedure
Procedure Snapshot(x,y,width,height)

	Protected image
	Protected dcimage,dcscreen

	image=CreateImage(#Snapshot,width,height,24)
	If image
		dcimage=StartDrawing(ImageOutput(#Snapshot))
		dcscreen=GetDC_(GetDesktopWindow_())
		BitBlt_(dcimage,0,0,width,height,dcscreen,x,y,#SRCCOPY)
		ReleaseDC_(GetDesktopWindow_(),dcscreen)
		StopDrawing()
	EndIf

	ProcedureReturn image

EndProcedure
Procedure FrameDraw()

	Protected cursor.Point
	Protected update
	Protected color

	#ColorBorder=	$A00000D0
	#ColorCross=	$A0D00000
	#ColorCenter=	$80FFFFFF
	#Gaps=			4
	#Gapl=			6
	#Minw=			24
	#Minh=			20

	GetCursorPos_(@cursor)


	With Frame
		If \Mode=3
			color=#ColorBorder
		Else
			color=#ColorCross
		EndIf

		If \Mode-1

			If \Left<>cursor\x Or \Top<>cursor\y
				\Left=cursor\x
				\Top=cursor\y

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)

				LineXY(0,	\Top-1,	\Left-#Gapl,	\Top-1,	color)
				LineXY(0,	\Top,	\Left-#Gaps,  	\Top,	#ColorCenter)
				LineXY(0,	\Top+1,	\Left-#Gapl,	\Top+1,	color)

				LineXY(\Left+#Gapl,	\Top-1,	SX,	\Top-1,	color)
				LineXY(\Left+#Gaps,	\Top,	SX,	\Top,	#ColorCenter)
				LineXY(\Left+#Gapl,	\Top+1,	SX,	\Top+1,	color)

				LineXY(\Left-1,	0,	\Left-1,	\Top-#Gapl,	color)
				LineXY(\Left,  	0,	\Left,	\Top-#Gaps,  	#ColorCenter)
				LineXY(\Left+1,	0,	\Left+1,	\Top-#Gapl,	color)

				LineXY(\Left-1,	\Top+#Gapl,	\Left-1,	SY,	color)
				LineXY(\Left,  	\Top+#Gaps,	\Left,  	SY,	#ColorCenter)
				LineXY(\Left+1,	\Top+#Gapl,	\Left+1,	SY,	color)

				GetCursorPos_(@cursor)
				DrawText(10,20,Str(cursor\x)+"|"+Str(cursor\y)+" > "+Str(WindowFromPoint_(PeekQ(cursor))),$FFFFFFFF,$FF000000)
				DrawText(10,40,Str(GetForegroundWindow_()),$FFFFFFFF,$FF000000)
				DrawText(10,60,Str(GetActiveWindow_()),$FFFFFFFF,$FF000000)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))



			EndIf

		Else
			If \Right<>cursor\x Or \Bottom<>cursor\y

				\Right=Max(cursor\x,\Left+#Minw)
				\Bottom=Max(cursor\y,\Top+#Minh)

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)
				LineXY(\Left-1, \Top-1,\Right+1,\Top-1,color)
				LineXY(\Left, \Top,	\Right,\Top,#ColorCenter)
				LineXY(\Left+1, \Top+1,\Right-1,\Top+1,color)

				LineXY(\Left-1,\Top,	\Left-1,\Bottom,color)
				LineXY(\Left,\Top,	\Left,\Bottom,#ColorCenter)
				LineXY(\Left+1,\Top+2,	\Left+1,\Bottom-2,color)

				LineXY(\Right-1,\Top+2,	\Right-1,\Bottom-#Gapl,color)
				LineXY(\Right,\Top,	\Right,\Bottom-#Gaps,#ColorCenter)
				LineXY(\Right+1,\Top,	\Right+1,\Bottom-#Gapl,color)


				LineXY(\Left+1,\Bottom-1,	\Right-#Gapl,\Bottom-1,color)
				LineXY(\Left,\Bottom,	\Right-#Gaps,\Bottom,#ColorCenter)
				LineXY(\Left-1,\Bottom+1,	\Right-#Gapl,\Bottom+1,color)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))

			EndIf
		EndIf
	EndWith

EndProcedure

Procedure Main()

	Protected cursor.POINT
	Protected hwnd

	OpenWindow(#Win,0,0,SX,SY,"Test",#PB_Window_BorderLess|#PB_Window_Invisible)
	ImageGadget(#Win,0,0,SX,SY,0)
	AddKeyboardShortcut(#Win,#PB_Shortcut_Space,666)
	RegisterHotKey_(WindowID(#Win),1001,#Null,#VK_SPACE)
	RegisterHotKey_(WindowID(#Win),1002,#Null,#VK_C)

	CreateImage(#Image,SX,SY,24);		#PB_Image_Transparent)
	SmartWindowRefresh(#Win,1);			flickers when doing HideWindow(...,0)
	StickyWindow(#Win,1)
	;HideWindow(#Win,0)
	;AddWindowTimer(#Win,0,250)

	Repeat
		Select WaitWindowEvent()

		Case #WM_HOTKEY
			If Frame\Mode=#Null
				Snapshot(0,0,SX,SY)
				Frame\Mode=2+Bool(EventwParam()-1001)
				Frame\Top=-1
				Frame\Bottom=-1
				FrameDraw()
				HideWindow(#Win,0)
			Else
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

			;Case #PB_Event_Timer
			;check=1

		Case #WM_MOUSEMOVE
			FrameDraw()

		Case #WM_RBUTTONDOWN
			If Frame\Mode
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

		Case #WM_LBUTTONDOWN
			If Frame\Mode=2
				Frame\Mode=1
				FrameDraw()
			Else
				Frame\Mode=0
				HideWindow(#Win,1)

				If 0
					cursor.Point
					GetCursorPos_(@cursor)
					hwnd=WindowFromPoint_(PeekQ(cursor))
					SetForegroundWindow_(hwnd)
					SetActiveWindow_(hwnd)
					hwnd=GetForegroundWindow_()
					SendMessage_(hWnd, #WM_LBUTTONDOWN, #MK_LBUTTON, PeekQ(cursor))
					SendMessage_(hWnd, #WM_LBUTTONUP, #MK_LBUTTON, PeekQ(cursor))
				EndIf

				ClickMouse();            >Click< 
				Beep_(1000,100)
				ClickMouse();             Why the second click is needed?

			EndIf

		Case #WM_LBUTTONUP
			If Frame\Mode
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

		EndSelect

	ForEver

EndProcedure

Main()

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Click on window while user use mouse

Post by RASHAD »

Hi MV
HideWindow() doesn't mean it's hidden from the Mouse Movent & Mouse event
It's just hidden from the view point
Use ResizeWindow() out & in it's much better
Next is a speed hack
Adapt it for your need
I hope you like it
Remember it's a speed hack :wink:

Code: Select all

; Define

	;EnableExplicit

	Enumeration
		#Win
	EndEnumeration

	Enumeration
		#Snapshot
		#Image
	EndEnumeration

	Structure FrameType
		Mode.i
		Left.i
		Top.i
		Right.i
		Bottom.i
		Width.i
		Height.i
	EndStructure

	#DpiBits=16

	Global Frame.FrameType
	Global Scale=GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)<<#DpiBits/96
	;Global Scale=96<<#DpiBits/GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)
	Global SX=GetSystemMetrics_(#SM_CXSCREEN)
	Global SY=GetSystemMetrics_(#SM_CYSCREEN)

	Global Dim Inputs.INPUT(0)
	Global hWnd

Prototype SwitchTohWnd(hWnd, value=1)

OpenLibrary(0, "user32")
Global SwitchTohWnd.SwitchTohWnd = GetFunction(0, "SwitchToThisWindow")

Procedure Max(a,b)

	If a<b
		ProcedureReturn b
	Else
		ProcedureReturn a
	EndIf

EndProcedure
Procedure ClickMouse()

	Protected ret

	BlockInput_(#True)
	Inputs(0)\type = #INPUT_MOUSE
	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTDOWN

	SendInput_(1, @Inputs(), SizeOf(INPUT))
	Sleep_(20)

	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTUP
	ret=SendInput_(1, @Inputs(), SizeOf(INPUT))

	BlockInput_(#False)

	ProcedureReturn ret

EndProcedure
Procedure Snapshot(x,y,width,height)

	Protected image
	Protected dcimage,dcscreen

	image=CreateImage(#Snapshot,width,height,24)
	If image
		dcimage=StartDrawing(ImageOutput(#Snapshot))
		dcscreen=GetDC_(GetDesktopWindow_())
		BitBlt_(dcimage,0,0,width,height,dcscreen,x,y,#SRCCOPY)
		ReleaseDC_(GetDesktopWindow_(),dcscreen)
		StopDrawing()
	EndIf

	ProcedureReturn image

EndProcedure
Procedure FrameDraw()

	Protected cursor.Point
	Protected update
	Protected color

	#ColorBorder=	$A00000D0
	#ColorCross=	$A0D00000
	#ColorCenter=	$80FFFFFF
	#Gaps=			4
	#Gapl=			6
	#Minw=			24
	#Minh=			20

	GetCursorPos_(@cursor)


	With Frame
		If \Mode=3
			color=#ColorBorder
		Else
			color=#ColorCross
		EndIf

		If \Mode-1

			If \Left<>cursor\x Or \Top<>cursor\y
				\Left=cursor\x
				\Top=cursor\y

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)

				LineXY(0,	\Top-1,	\Left-#Gapl,	\Top-1,	color)
				LineXY(0,	\Top,	\Left-#Gaps,  	\Top,	#ColorCenter)
				LineXY(0,	\Top+1,	\Left-#Gapl,	\Top+1,	color)

				LineXY(\Left+#Gapl,	\Top-1,	SX,	\Top-1,	color)
				LineXY(\Left+#Gaps,	\Top,	SX,	\Top,	#ColorCenter)
				LineXY(\Left+#Gapl,	\Top+1,	SX,	\Top+1,	color)

				LineXY(\Left-1,	0,	\Left-1,	\Top-#Gapl,	color)
				LineXY(\Left,  	0,	\Left,	\Top-#Gaps,  	#ColorCenter)
				LineXY(\Left+1,	0,	\Left+1,	\Top-#Gapl,	color)

				LineXY(\Left-1,	\Top+#Gapl,	\Left-1,	SY,	color)
				LineXY(\Left,  	\Top+#Gaps,	\Left,  	SY,	#ColorCenter)
				LineXY(\Left+1,	\Top+#Gapl,	\Left+1,	SY,	color)

				GetCursorPos_(@cursor)
				DrawText(10,20,Str(cursor\x)+"|"+Str(cursor\y)+" > "+Str(WindowFromPoint_(PeekQ(cursor))),$FFFFFFFF,$FF000000)
				DrawText(10,40,Str(GetForegroundWindow_()),$FFFFFFFF,$FF000000)
				DrawText(10,60,Str(GetActiveWindow_()),$FFFFFFFF,$FF000000)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))



			EndIf

		Else
			If \Right<>cursor\x Or \Bottom<>cursor\y

				\Right=Max(cursor\x,\Left+#Minw)
				\Bottom=Max(cursor\y,\Top+#Minh)

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)
				LineXY(\Left-1, \Top-1,\Right+1,\Top-1,color)
				LineXY(\Left, \Top,	\Right,\Top,#ColorCenter)
				LineXY(\Left+1, \Top+1,\Right-1,\Top+1,color)

				LineXY(\Left-1,\Top,	\Left-1,\Bottom,color)
				LineXY(\Left,\Top,	\Left,\Bottom,#ColorCenter)
				LineXY(\Left+1,\Top+2,	\Left+1,\Bottom-2,color)

				LineXY(\Right-1,\Top+2,	\Right-1,\Bottom-#Gapl,color)
				LineXY(\Right,\Top,	\Right,\Bottom-#Gaps,#ColorCenter)
				LineXY(\Right+1,\Top,	\Right+1,\Bottom-#Gapl,color)


				LineXY(\Left+1,\Bottom-1,	\Right-#Gapl,\Bottom-1,color)
				LineXY(\Left,\Bottom,	\Right-#Gaps,\Bottom,#ColorCenter)
				LineXY(\Left-1,\Bottom+1,	\Right-#Gapl,\Bottom+1,color)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))

			EndIf
		EndIf
	EndWith

EndProcedure

Procedure Main()

	Protected cursor.POINT
	Protected hwnd

	OpenWindow(#Win,-1080,0,1080,1080,"Test",#PB_Window_BorderLess)
	ImageGadget(#Win,0,0,1080,1080,0)
	AddKeyboardShortcut(#Win,#PB_Shortcut_Space,666)
	RegisterHotKey_(WindowID(#Win),1001,#Null,#VK_SPACE)
	RegisterHotKey_(WindowID(#Win),1002,#Null,#VK_C)

	CreateImage(#Image,1080,1080,24);		#PB_Image_Transparent)
	SmartWindowRefresh(#Win,1);			flickers when doing HideWindow(...,0)
	StickyWindow(#Win,1)
	;HideWindow(#Win,0)
	;AddWindowTimer(#Win,0,250)

	Repeat
		Select WaitWindowEvent()

		Case #WM_HOTKEY
			If Frame\Mode=#Null
				Snapshot(0,0,SX,SY)
				Frame\Mode=2+Bool(EventwParam()-1001)
				Frame\Top=-1
				Frame\Bottom=-1
				FrameDraw()
				ResizeWindow(#Win,0,0,1080,1080)
			Else
				Frame\Mode=0
				ResizeWindow(#Win,-1080,0,1080,1080)
			EndIf
	
		Case #WM_MOUSEMOVE
			FrameDraw()
			dx = DesktopMouseX()
			dy = DesktopMouseY()

		Case #WM_RBUTTONDOWN
			If Frame\Mode
				Frame\Mode=0
				ResizeWindow(#Win,-1080,0,1080,1080)
			EndIf

		Case #WM_LBUTTONDOWN
		  ResizeWindow(#Win,-1080,0,1080,1080)
		  hwnd = WindowFromPoint_(dy<<32|dx)
		  SwitchTohWnd(hwnd)

		Case #WM_LBUTTONUP	
 		EndSelect

	ForEver

EndProcedure

Main()

Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Click on window while user use mouse

Post by Michael Vogel »

Rashad, your code doesn't seem to work here (Windows 11).

I did the following tests for 'C' and mouse click:
  • I click on the 'X' of the PB-IDE function bar which should kill the running purebasic program.
  • I click on a desktop icon which should be selected the icon.
As you said, the focus won't be changed when hiding, I did some tests showing the active and foreground window - but I need more time for testing.

Everything seems to be a timing issue, when waiting around 300ms, it works here with a single call for ClickMouse() - below is a modified code (but from the old base) which works here. Anyhow I'm still not happy... :?

Good news - maybe I've found the issue - the additional LButtonUp event (which should appear when releasing the mouse button) may have caused the problems, now I am triggering #WM_LBUTTONUP instead of #WM_LBUTTONDOWN and I do not need any delay before calling ClickMouse().

Code: Select all

; Define

	EnableExplicit

	Enumeration
		#Win
	EndEnumeration

	Enumeration
		#Snapshot
		#Image
	EndEnumeration

	Structure FrameType
		Mode.i
		Left.i
		Top.i
		Right.i
		Bottom.i
		Width.i
		Height.i
	EndStructure

	#DpiBits=16

	Global Frame.FrameType
	Global Scale=GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)<<#DpiBits/96
	;Global Scale=96<<#DpiBits/GetDeviceCaps_(GetDC_(0),#LOGPIXELSX)
	Global SX=GetSystemMetrics_(#SM_CXSCREEN)
	Global SY=GetSystemMetrics_(#SM_CYSCREEN)

	Global Dim Inputs.INPUT(0)
	Global OldWin

; EndDefine
; Define Debug

	OpenWindow(50,10,10,310,140,"Debug-Information",#PB_Window_NoActivate)
	TextGadget(51,10, 5,40,20,"Mouse:")
	TextGadget(52,10,25,40,20,"Point:")
	TextGadget(53,10,45,40,20,"Active:")
	TextGadget(54,10,65,40,20,"Fore:")
	TextGadget(55,10,85,40,20,"Old:")
	TextGadget(56,60, 5,240,20,"-")
	TextGadget(57,60,25,240,20,"-")
	TextGadget(58,60,45,240,20,"-")
	TextGadget(59,60,65,240,20,"-")
	TextGadget(60,60,85,240,20,"-")
	StickyWindow(50,1)
	AddWindowTimer(50,50,200)

	Global Cursor.Point

	Procedure.s StrWinText(handle)

		Protected Temp.s=Space(50)

		GetWindowText_(handle,@temp,50)

		ProcedureReturn Str(handle)+" ("+temp+")"

	EndProcedure
	Procedure ShowWinInfo()

		Protected Cursor.Point

		GetCursorPos_(@cursor)
		SetGadgetText(56,Str(cursor\x)+"|"+Str(cursor\y))
		SetGadgetText(57,StrWinText(WindowFromPoint_(PeekQ(cursor))))
		SetGadgetText(58,StrWinText(GetActiveWindow_()))
		SetGadgetText(59,StrWinText(GetForegroundWindow_()))
		SetGadgetText(60,StrWinText(OldWin))

	EndProcedure


; EndDefine

Procedure Max(a,b)

	If a<b
		ProcedureReturn b
	Else
		ProcedureReturn a
	EndIf

EndProcedure
Procedure ClickMouse()

	Protected ret

	BlockInput_(#True)
	Inputs(0)\type = #INPUT_MOUSE
	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTDOWN

	SendInput_(1, @Inputs(), SizeOf(INPUT))
	Sleep_(20)

	Inputs(0)\mi\dwFlags = #MOUSEEVENTF_LEFTUP
	ret=SendInput_(1, @Inputs(), SizeOf(INPUT))

	BlockInput_(#False)

	ProcedureReturn ret

EndProcedure
Procedure Snapshot(x,y,width,height)

	Protected image
	Protected dcimage,dcscreen

	image=CreateImage(#Snapshot,width,height,24)
	If image
		dcimage=StartDrawing(ImageOutput(#Snapshot))
		dcscreen=GetDC_(GetDesktopWindow_())
		BitBlt_(dcimage,0,0,width,height,dcscreen,x,y,#SRCCOPY)
		ReleaseDC_(GetDesktopWindow_(),dcscreen)
		StopDrawing()
	EndIf

	ProcedureReturn image

EndProcedure
Procedure FrameDraw()

	Protected cursor.Point
	Protected update
	Protected color

	#ColorBorder=	$A00000D0
	#ColorCross=	$A0D00000
	#ColorCenter=	$80FFFFFF
	#Gaps=			4
	#Gapl=			6
	#Minw=			24
	#Minh=			20

	GetCursorPos_(@cursor)


	With Frame
		If \Mode=3
			color=#ColorBorder
		Else
			color=#ColorCross
		EndIf

		If \Mode-1

			If \Left<>cursor\x Or \Top<>cursor\y
				\Left=cursor\x
				\Top=cursor\y

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)

				LineXY(0,	\Top-1,	\Left-#Gapl,	\Top-1,	color)
				LineXY(0,	\Top,	\Left-#Gaps,  	\Top,	#ColorCenter)
				LineXY(0,	\Top+1,	\Left-#Gapl,	\Top+1,	color)

				LineXY(\Left+#Gapl,	\Top-1,	SX,	\Top-1,	color)
				LineXY(\Left+#Gaps,	\Top,	SX,	\Top,	#ColorCenter)
				LineXY(\Left+#Gapl,	\Top+1,	SX,	\Top+1,	color)

				LineXY(\Left-1,	0,	\Left-1,	\Top-#Gapl,	color)
				LineXY(\Left,  	0,	\Left,	\Top-#Gaps,  	#ColorCenter)
				LineXY(\Left+1,	0,	\Left+1,	\Top-#Gapl,	color)

				LineXY(\Left-1,	\Top+#Gapl,	\Left-1,	SY,	color)
				LineXY(\Left,  	\Top+#Gaps,	\Left,  	SY,	#ColorCenter)
				LineXY(\Left+1,	\Top+#Gapl,	\Left+1,	SY,	color)

				GetCursorPos_(@cursor)
				DrawText(10,20,Str(cursor\x)+"|"+Str(cursor\y)+" > "+Str(WindowFromPoint_(PeekQ(cursor))),$FFFFFFFF,$FF000000)
				DrawText(10,40,Str(GetForegroundWindow_()),$FFFFFFFF,$FF000000)
				DrawText(10,60,Str(GetActiveWindow_()),$FFFFFFFF,$FF000000)
				Protected Temp.s=Space(100)
				GetWindowText_(GetForegroundWindow_(),@temp,100)
				GetWindowText_(OldWin,@temp,100)
				DrawText(10,100,temp,$FFFFFFFF,$FF000000)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))



			EndIf

		Else
			If \Right<>cursor\x Or \Bottom<>cursor\y

				\Right=Max(cursor\x,\Left+#Minw)
				\Bottom=Max(cursor\y,\Top+#Minh)

				StartDrawing(ImageOutput(#Image))
				DrawImage(ImageID(#Snapshot),0,0)

				DrawingMode(#PB_2DDrawing_AlphaBlend)
				LineXY(\Left-1, \Top-1,\Right+1,\Top-1,color)
				LineXY(\Left, \Top,	\Right,\Top,#ColorCenter)
				LineXY(\Left+1, \Top+1,\Right-1,\Top+1,color)

				LineXY(\Left-1,\Top,	\Left-1,\Bottom,color)
				LineXY(\Left,\Top,	\Left,\Bottom,#ColorCenter)
				LineXY(\Left+1,\Top+2,	\Left+1,\Bottom-2,color)

				LineXY(\Right-1,\Top+2,	\Right-1,\Bottom-#Gapl,color)
				LineXY(\Right,\Top,	\Right,\Bottom-#Gaps,#ColorCenter)
				LineXY(\Right+1,\Top,	\Right+1,\Bottom-#Gapl,color)


				LineXY(\Left+1,\Bottom-1,	\Right-#Gapl,\Bottom-1,color)
				LineXY(\Left,\Bottom,	\Right-#Gaps,\Bottom,#ColorCenter)
				LineXY(\Left-1,\Bottom+1,	\Right-#Gapl,\Bottom+1,color)

				StopDrawing()
				SetGadgetState(#Win,ImageID(#Image))

			EndIf
		EndIf
	EndWith

EndProcedure

Procedure Main()

	Protected cursor.POINT
	Protected hwnd
	Protected Win

	Win=OpenWindow(#Win,0,0,SX,SY,"Test",#PB_Window_BorderLess|#PB_Window_Invisible|#PB_Window_NoActivate)
	ImageGadget(#Win,0,0,SX,SY,0)
	AddKeyboardShortcut(#Win,#PB_Shortcut_Space,666)
	RegisterHotKey_(WindowID(#Win),1001,#Null,#VK_SPACE)
	RegisterHotKey_(WindowID(#Win),1002,#Null,#VK_C)

	CreateImage(#Image,SX,SY,24);		#PB_Image_Transparent)
	SmartWindowRefresh(#Win,1);			flickers when doing HideWindow(...,0)
	StickyWindow(#Win,1)
	;HideWindow(#Win,0)
	;AddWindowTimer(#Win,0,250)

	Repeat
		Select WaitWindowEvent()

		Case #WM_HOTKEY
			If Frame\Mode=#Null
				Oldwin=GetForegroundWindow_()
				Snapshot(0,0,SX,SY)
				Frame\Mode=2+Bool(EventwParam()-1001)
				Frame\Top=-1
				Frame\Bottom=-1
				FrameDraw()
				HideWindow(#Win,0)
				StickyWindow(50,1)
			Else
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

			;Case #PB_Event_Timer
			;check=1

		Case #WM_MOUSEMOVE
			If Frame\Mode
				FrameDraw()
			EndIf

		Case #WM_RBUTTONDOWN
			If Frame\Mode
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

		Case #WM_LBUTTONDOWN; -> Changed to #WM_LBUTTONUP
			If Frame\Mode=2
				Frame\Mode=1
				FrameDraw()
			Else
				Frame\Mode=0
				HideWindow(#Win,1)
				;StickyWindow(#Win,0)
				;ShowWindow_(Win,#SW_MINIMIZE)
				SetForegroundWindow_(OldWin)
				SetActiveWindow_(OldWin)
				If 0
					cursor.Point
					GetCursorPos_(@cursor)
					hwnd=WindowFromPoint_(PeekQ(cursor))
					SetForegroundWindow_(hwnd)
					SetActiveWindow_(hwnd)
					hwnd=GetForegroundWindow_()
					SendMessage_(hWnd, #WM_LBUTTONDOWN, #MK_LBUTTON, PeekQ(cursor))
					SendMessage_(hWnd, #WM_LBUTTONUP, #MK_LBUTTON, PeekQ(cursor))
				EndIf

				;PostMessage_(OldWin, #WM_LBUTTONDOWN, #MK_LBUTTON, PeekQ(cursor))
				;PostMessage_(OldWin, #WM_LBUTTONUP, #MK_LBUTTON, PeekQ(cursor))
				
				;ClickMouse()
				Beep_(1000,100)
				Sleep_(200)
				Beep_(2000,100)
				ClickMouse()
				
				;PostMessage_(OldWin, #WM_LBUTTONDOWN, #MK_LBUTTON, PeekQ(cursor))
				;PostMessage_(OldWin, #WM_LBUTTONUP, #MK_LBUTTON, PeekQ(cursor))

			EndIf

		Case #WM_LBUTTONUP
			If Frame\Mode
				Frame\Mode=0
				HideWindow(#Win,1)
			EndIf

		Case #PB_Event_Timer
			ShowWinInfo()


		EndSelect

	ForEver

EndProcedure

Main()

Post Reply