Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[How to] Enable Drag for a Static Control ?


  • Please log in to reply
26 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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, https://ahknet.autohotkey.com/~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 ?



majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
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

	}
}

Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@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. :)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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... :-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

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
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

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?
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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.

:)

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

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 )
Posted Image

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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


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

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Hey, I won't be 46 before October 6!
And I am much younger in mind! :-P (what do you mean by "it shows"?)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

adamrgolf
  • Members
  • 442 posts
  • Last active: May 22 2017 09:16 PM
  • Joined: 28 Dec 2006
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


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks! I have updated the post :)

Trente
  • Guests
  • Last active:
  • Joined: --

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?

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
in its most simplistic form
#InstallMouseHook 
SetTimer,mouse,10
mouse:
	if !drag := GetKeyState("LButton", "P") 
		ToolTip % "Mouse Up"
	Else ToolTip % "Holding down"
Return

Never lose.
WIN or LEARN.