Jump to content


Photo

[How to] Enable Drag for a Static Control ?


  • Please log in to reply
26 replies to this topic

#1 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 23 May 2007 - 11:19 PM

How to enable Drag for a Static Control ?
http://www.autohotke...p=123732#123732

For a Picture Puzzle game, I had to enable Drag and Drop for Static Controls. Lucky me, I found a simple way.

Run the following Copy/Paste/Try example - Should be self explanatory!


Credit: Thanks to adamrgolf for his suggestion to add a WinSet, Redraw command.

Loop 6
 Gui, Add, Picture, Icon%A_Index% gControlMove, User32.dll
Gui, Add, Text, w200 h30 +0x201 +Border gControlMove, Static Text Control
Gui, Show, w400 h300, Click'N'Drag the Icons!
Return

ControlMove:
  MouseGetPos,,,,sHwnd, 2
  PostMessage, 0x112,0xF012,0,,ahk_id %sHwnd% ; [ WM_SYSCOMMAND+SC_MOVE ]
  Winset,Redraw,,ahk_id %sHwnd% ; Thanks to adamrgolf
Return

Edit: 2010-Jun-11
______________________


MouseGetPos produces bizarre results when we try to move a fan-stacked series of images. The wrong control was being moved: Read Discussion. To overcome the said effect, we may use a matching pair of pseudo-arrays ( one for 'control hwnd' and the other for 'variable name' ) to determine the exact control that was clicked. Here follows a working example:

IfNotExist, *.png
  Loop 5
   UrlDownloadtoFile, http://www.autohotkey.net/~goyyah/pic%A_Index%.png, pic%A_Index%.png

Loop *.png
   Gui, 1:Add, Picture, xp+64 y32 hwndhWnd%A_Index% Border vI%A_Index% gControlMove
                       +0x4000000, %A_LoopFileLongPath%   ; WS_CLIPSIBLINGS := 0x4000000

Gui, 1:Show, w800 h600, Draggable-Controls
Return

ControlMove:
  Ix := SubStr( A_GuiControl,2 ), sHwnd := hWnd%Ix%
  DllCall( "SetWindowPos", UInt,sHwnd, UInt,0, UInt,0, UInt,0, UInt,0, UInt,0, UInt,0x43 )
  Sleep -1     ; ^ SWP_NOMOVE := 0x2 | SWP_NOSIZE := 0x1 | SWP_SHOWWINDOW := 0x40 = 0x43
  SendMessage, 0x112, 0xF012, 0,, ahk_id %sHwnd%             ; WM_SYSCOMMAND and SC_MOVE
Return


Related Post: How to enable Drag for a GUI without a Titlebar ?



#2 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 23 May 2007 - 11:28 PM

How to enable Drag for Any Control

SetBatchlines -1
SetControlDelay, -1
#SingleInstance force
	WM_LBUTTONDOWN := 0x201
	WM_LBUTTONUP := 0x202
	WM_MOUSEMOVE := 0x200

	onmessage(WM_LBUTTONDOWN, "OnClickDown")
	onmessage(WM_LBUTTONUP, "OnClickUp")
	onmessage(WM_MOUSEMOVE, "OnMouseMove")




	Gui +LastFound +resize
	handle := WinExist()
	dc	:= DllCall("GetDC", "uint", handle)
	
	Gui Show, h300 w300
	gui add, Button, x100 y100 w80 h30, MyControl
return


OnClickDown(){
	global

	if A_GuiControl = MyControl
		mode = move
		
}

OnClickUp(){
	global
	if A_GuiControl = MyControl
		mode = static
}

OnMouseMove()
{
	global
	

	if mode = move
	{
		MouseGetPos mx, my
		ControlGetPos x, y, w, h, MyControl, ahk_id %handle%
		controlmove MyControl, mx,my

	}
}


#3 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 24 May 2007 - 02:09 AM

Very nice effects! I suppose you got some snap-to-grid functionality, and redraw of the control, to make it useful for games, like Go, or chess. Please post those, too! Congratulations!

#4 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 24 May 2007 - 06:13 AM

@majkinetor: Very nice :D. Thanks for sharing the technique.
@Laszlo: My Picture Puzzle somewhat demonstartes a crude method to snap controls to the virtual grid.

Thanks. :)

#5 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 24 May 2007 - 09:07 AM

majkinetor, this is interesting, but you should distinguish simple click from held click & move: after moving the button, its action is triggered.
And it might need some adjustments to avoid moving the top-left button to the cursor position.
Beside, we rarely need to move (with mouse) non-static controls, but yet the demonstration is interesting.

Skan, your solution is very interesting by its simplicity.
I provided a much more convoluted solution a long time ago, based on Veovis' code, seen in its sigma game.
strange results when attempting a Gui Drag event...
I also made a splitter bar using the above code: Splitter bar window control with AHk
I guess it is time to put these scripts to the trash can... :-)

#6 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 24 May 2007 - 10:07 AM

majkinetor, this is interesting, but you should distinguish simple click from held click & move: after moving the button, its action is triggered. And it might need some adjustments to avoid moving the top-left button to the cursor position.


I didn't inted to provide full blown example, just to see is it possible. I made this for toralf and SGUI long time ago.

Beside, we rarely need to move (with mouse) non-static controls, but yet the demonstration is interesting.

I don't see why you constantly talk about if something is useful and how much is it useful. If you need it, its useful, if you don't need it, even the most useful stuff is unuseful. Like I told you, I created this for toralf and SGUI which is extremely useful...

Anyway, there is a great feedback you get just by knowing how some stuff can be done and fullfiling your curiosity and xperience. I guess you forgot that. Perhaps... to old ? :D

#7 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 24 May 2007 - 10:33 AM

I didn't inted to provide full blown example, just to see is it possible.

I understood it as such, my remarks aren't to deprecate your work, but to complement it, in case somebody wants to use it.

I don't see why you constantly talk about if something is useful and how much is it useful.

Where did I wrote that it isn't useful? Would I wrote "interesting" (twice! :-P) for something I judge useless?

I created this for toralf and SGUI which is extremely useful...

SGUI is very useful indeed, and falls in the category of "rare use", no? Unless you provide an option to dynamically change the GUI of all your applications.

Anyway, there is a great feedback you get just by knowing how some stuff can be done and fullfiling your curiosity and xperience.

That's exactly what I meant by "interesting".

Perhaps... to old ? :D

Perhaps... too touchy?

#8 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 24 May 2007 - 11:19 AM

Skan, your solution is very interesting by its simplicity.


Thanks! :)

I provided a much more convoluted solution a long time ago, based on Veovis' code, seen in its sigma game.
strange results when attempting a Gui Drag event...
I also made a splitter bar using the above code: Splitter bar window control with AHk


Yes! I remember it. I was a newbie by then and could not understand much of it.

:)

#9 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 24 May 2007 - 12:16 PM

Unless you provide an option to dynamically change the GUI of all your applications.

Acctually, I was thinking about this.

Perhaps... too touchy?

Definitely. Didn't you learn that already about me ?
Anyway, its not as bad as too old :p (btw, how old are you :D )

#10 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 24 May 2007 - 12:23 PM

Anyway, its not as bad as too old :p (btw, how old are you :D )


46 - 48 would be my best guess! :roll:

#11 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 24 May 2007 - 12:34 PM

Hey, I won't be 46 before October 6!
And I am much younger in mind! :-P (what do you mean by "it shows"?)

#12 adamrgolf

adamrgolf
  • Members
  • 440 posts

Posted 13 July 2007 - 01:17 PM

Regarding click'n'drag --

If you use skans example a few posts back, if you drag an icon ontop of another icon, and then move the bottommost icon -- it still shows whatever part the of the top icon was directly above it

if you add a redraw it seems to help a bit:

Loop 6
  Gui, Add, Picture, Icon%A_Index% gControlMove, User32.dll
Gui, Add, Text, w200 h30 +0x201 +Border gControlMove, Static Text Control
Gui, Show, w400 h300, Click'N'Drag the Icons!
Return

ControlMove:
  MouseGetPos,,,,sHwnd, 2
  PostMessage, 0x112,0xF012,0,,ahk_id %sHwnd% ; [ WM_SYSCOMMAND+SC_MOVE ]
[color=red]  Winset,Redraw,,Click'N'Drag the Icons![/color]      ; here
Return


#13 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 13 July 2007 - 02:28 PM

Thanks! I have updated the post :)

#14 Trente

Trente
  • Guests

Posted 26 May 2010 - 05:30 PM

How to enable Drag for a Static Control ?
http://www.autohotke...p=123732#123732

Loop 6
 Gui, Add, Picture, Icon%A_Index% gControlMove, User32.dll
Gui, Add, Text, w200 h30 +0x201 +Border gControlMove, Static Text Control
Gui, Show, w400 h300, Click'N'Drag the Icons!
Return

ControlMove:
  MouseGetPos,,,,sHwnd, 2
  PostMessage, 0x112,0xF012,0,,ahk_id %sHwnd% ; [ WM_SYSCOMMAND+SC_MOVE ]
  Winset,Redraw,,ahk_id %sHwnd% ; Thanks to adamrgolf
Return

Related Post: How to enable Drag for a GUI without a Titlebar ?

:idea: :D



SKAN, I have a few questions for you about this wonderful snippet of code...


1. How can I distinguish between the drag action, and a click action? And since it seems that all dragable images would have the same action code, is there a way to reference the variable of whichever image was clicked?

2. If I drag an image passing completely over another one, there are no glitches. But if I let go of the drag while one image is on top of another, then as soon as I drag that top image away, it retains whatever portion of the bottom image it was covering. I have to click that top image a second time in order to clear that. Is there a way to fix that visual glitch?

#15 tank

tank
  • Members
  • 4100 posts

Posted 26 May 2010 - 06:11 PM

in its most simplistic form
#InstallMouseHook 
SetTimer,mouse,10
mouse:
	if !drag := GetKeyState("LButton", "P") 
		ToolTip % "Mouse Up"
	Else ToolTip % "Holding down"
Return