Drag a list item to an 'empty' button to 'fill' it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MikeyG12
Posts: 30
Joined: 03 May 2018, 14:21

Drag a list item to an 'empty' button to 'fill' it

21 Oct 2019, 18:10

This is a little bit of a strange request but can someone help me understand how to build a GUI that can take a list of items and allow for those items to be dragged on to a button in the same GUI window to change the button's name? I will try to provide a little more detail below:

Code: Select all

__________________________________________
| [] [] [] [] [] []              |Item2  |
| [Item1] [] [] [] []            |Item3  |
| [] [] [] [] [Item5] []         |Item4  |
|                                |Item6  |
|                                |       |
|                                |       |
|                                |       |
|________________________________|_______|
The brackets are the empty buttons and Item1 & 5 have been dragged into the buttons to update their name.

I'm having trouble conceptualizing how this would be done but I'm hoping someone can point me in the right direction. I've looked at a few posts but they don't give a clear indication on how this can be achieved: https://autohotkey.com/board/topic/41367-i-want-movable-buttons-any-ideas/ https://autohotkey.com/board/topic/72107-drag-and-drop-ahk-gui-controls/
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Drag a list item to an 'empty' button to 'fill' it

21 Oct 2019, 18:28

Proof of concept.
Would need refinement, but shows that it's well within reach.

Code: Select all

#SingleInstance,Force


ButtonAssigned:=[0,0,0,0,0]

Gui,1:+AlwaysOnTop -DPIScale -Theme
Gui,1:Color,222222,333333
Gui,1:Font,cWhite s8,Segoe UI

Loop,5  {
   Gui,1:Add,Button,hwndButton%A_Index%,
}

Gui,1:Add,ListBox,x+200 ym vLB gSomelabel,Item 1||Item 2|Item 3|Item 4|Item 5|


Gui,1:Show,,PROOF OF CONCEPT




return
GuiClose:
GuiContextMenu:
*Esc::
   ExitApp
   
   
Somelabel:
   GuiControlGet,output,1:,LB
   While(GetKeyState("LButton")){
      sleep, 10
   }
   MouseGetPos,,,,ctrl,2
   Loop,5   {
      if(ctrl=Button%A_Index%&&!ButtonAssigned[A_Index]){
         Gui,Fake:Add,Button,vfbt,% outPut
         GuiControlGet,pos,Fake:pos,fbt
         Gui,Fake:Destroy
         GuiControl,1:Move,% Button%A_Index%,% "w" posW
         GuiControl,1:,% Button%A_Index%,% output
         ButtonAssigned[A_Index]:=1
      }
   }
   return

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Drag a list item to an 'empty' button to 'fill' it

21 Oct 2019, 19:19

A button that doesn't do anything when you press it isn't much of a button.

So here is attaching a label to the button once it is set.

Code: Select all

#SingleInstance,Force
ButtonAssigned:=[0,0,0,0,0]
Gui,1:+AlwaysOnTop -DPIScale -Theme
Gui,1:Color,222222,333333
Gui,1:Font,cWhite s8,Segoe UI
Loop,5  {
   Gui,1:Add,Button,hwndButton%A_Index%,
}
Gui,1:Add,ListBox,x+200 ym vLB gSomelabel,Item 1||Item 2|Item 3|Item 4|Item 5|
Gui,1:Show,,PROOF OF CONCEPT
return
GuiClose:
GuiContextMenu:
*Esc::
   ExitApp

Somelabel:
   GuiControlGet,output,1:,LB
   While(GetKeyState("LButton")){
      sleep, 10
   }
   MouseGetPos,,,,ctrl,2
   Loop,5   {
      if(ctrl=Button%A_Index%&&!ButtonAssigned[A_Index]){
         Gui,Fake:Add,Button,vfbt,% outPut
         GuiControlGet,pos,Fake:pos,fbt
         Gui,Fake:Destroy
         GuiControl,1:Move,% Button%A_Index%,% "w" posW
         GuiControl,1:,% Button%A_Index%,% output
         ButtonAssigned[A_Index]:=1
         GuiControl,% "1:+g" StrReplace(output," ","_"),% Button%A_Index%
      }
   }
   return
   
Item_1:
   Gui,1:+OwnDialogs
   msgbox, Item 1 Label Triggered
   return
  
Item_2:
   Gui,1:+OwnDialogs
   msgbox, Item 2 Label Triggered
   return

Item_3:
   Gui,1:+OwnDialogs
   msgbox, Item 3 Label Triggered
   return
   
Item_4: 
   Gui,1:+OwnDialogs
   msgbox, Item 4 Label Triggered
   return
   
Item_5: 
   Gui,1:+OwnDialogs
   msgbox, Item 5 Label Triggered
   return 
   
MikeyG12
Posts: 30
Joined: 03 May 2018, 14:21

Re: Drag a list item to an 'empty' button to 'fill' it

22 Oct 2019, 23:01

@Hellbent Thank you thank you thank you! This works great and there are portions of code that I've not had experience with so it's great seeing new concepts. I did want to see if I can get some clarifications on some parts of the code. For the most part I understand what's going on and the AutoHotKey Help filled in some blanks. These things I wasn't able to find in the Help.

When you write Gui, "1:Add, Button" I wasn't sure how the 1: bit was working. Is it just creating ownership to the 1st Gui or simply setting the name of the Gui Control? The same question with "Gui, Fake:Add".

Additionally, I'm confused with the addition of the % after the comma: "Gui,Fake:Add,Button,vfbt,% outPut" What is the significance of that as I noticed it doesn't work without it.

And to provide some clarification for the end result of this application, basically I have a piece of hardware at my job similar to a xkeys device. The mappings of the control surface are saved in an XML file and I wanted a way to graphically arrange the buttons and then output a replacement xml file for the hardware to read. I know how to read the xml file to generate the list in the list box, and then store it using AHK so that part should be covered. Any assistance would be greatly appreciated, your assistance so far has greatly reduced the amount of time I thought this project would take! :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 314 guests