[4.20 Win32] Snap mouse?

Just starting out? Need help? Post your questions and find answers here.
kawasaki
Enthusiast
Enthusiast
Posts: 182
Joined: Thu Oct 16, 2003 8:09 pm

[4.20 Win32] Snap mouse?

Post by kawasaki »

Hello yet again,

Another quick one, Is there a way to snap a mouse to a predefined grid on a window rect?

So for instance if the grid is 5x5 and you move the mouse over the window which contains the grid, the mouse will automatically jump 5 pixels left, right, up or down, depending on the next grid point?



Thanks,

Mike
User avatar
idle
Always Here
Always Here
Posts: 5921
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

You may be able to do that with SetCursorPos_(x,y) with a call to BlockInput_(#true or #false) with either Sendinput or mouse_event to do mouse clicks.

I guess it really depends upon what your trying to achieve by having the mouse snap to a grid.

I'd probably try SetCursorPos first but I posted a tip on using hooks and SendInput and kept the mouse and keyboard routines separate.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

You don't snap the mouse, you snap the object:

Code: Select all

#GRIDSIZE = 10

Procedure WindowCallback(hWnd,uMsg,wParam,lParam)
	Select uMsg
		Case #WM_PAINT
		hdc = BeginPaint_(hwnd,ps.PAINTSTRUCT)	
		For x=0 To WindowWidth(0) Step #GRIDSIZE
			For y=0 To WindowHeight(0) Step #GRIDSIZE
				SetPixel_(hdc,x,y,0)
			Next
		Next	
		EndPaint_(hwnd,ps)
	EndSelect
	
	ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure GagdetProc(hWnd,uMsg,wParam,lParam)
	Shared lpPrevFunc
	Static bDragging, MTX, MTY

	Select uMsg
		Case #WM_LBUTTONDOWN
		MTX = lParam & $FFFF
		MTY = lParam >> 16
		bDragging = 1

		Case #WM_MOUSEMOVE
		If bDragging
			GetCursorPos_(cpt.POINT)
			ScreenToClient_(WindowID(0),cpt)
			GetClientRect_(hWnd,crc.RECT)
			MoveWindow_(hWnd,(cpt\x - MTX) / #GRIDSIZE * #GRIDSIZE,(cpt\y - MTY) / #GRIDSIZE * #GRIDSIZE,crc\right,crc\bottom,1)
		EndIf

		Case #WM_LBUTTONUP
		bDragging = 0
	EndSelect
	
	ProcedureReturn CallWindowProc_(lpPrevFunc,hWnd,uMsg,wParam,lParam)
EndProcedure

OpenWindow(0,0,0,420,320,"void",#WS_OVERLAPPEDWINDOW | 1)
CreateGadgetList(WindowID(0))
ButtonGadget(0,10,10,120,30,"DRAG ME")

lpPrevFunc = SetWindowLong_(GadgetID(0),#GWL_WNDPROC,@GagdetProc())

SetWindowCallback(@WindowCallback())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Last edited by Fluid Byte on Sat Sep 13, 2008 5:56 pm, edited 1 time in total.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

@Fluid Byte: Brilliant! Something for the CodeArchiv!
PureBasic for Windows
User avatar
idle
Always Here
Always Here
Posts: 5921
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

Fluid Byte wrote:You don't snap the mouse, you snap the object:
:lol: Well spotted Fluid Byte, the operative word missing from the question was object.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [4.20 Win32] Snap mouse?

Post by netmaestro »

I remember seeing a good solution from Sparkie, it should serve your needs. The code is a bit old so you will have to fix WindowFromPoint_() and take out the CreateGadgetList but it still works well:

http://www.purebasic.fr/english/viewtop ... 13&t=28870

Just in case you aren't familiar with the change:

Code: Select all

hwin = ChildWindowFromPoint_(WindowID(0), WindowMouseX(0) | (WindowMouseY(0)<<32) )
fixes it.
BERESHEIT
Post Reply