The bottom line is this: Suppose we have a form with a button on it. The coordinate of the button is 33:33. How to make so that the button can be dragged on the form (of course in the running program)? At the same time, upon reaching a certain coordinate, an action of pressing a button was performed.

    3 answers 3

    There are two options. Either manually by using the mouse movement event, so that when the left mouse button is held down, the button on the form follows the cursor, or use standard functions.
    The easy way is via the mouse movement event. We describe boolean drag. At the OnMouseDown event we assign drag: = true, i.e. allow moving. In the OnMouseMove event handler, we assign mouse coordinates to the coordinates of the button, just as we did in the timer, but with the condition if drag then [our actions]. And finally, on the OnMouseUp event, reset the variable - drag: = false.
    Code example:

    {...код...} var drag:boolean=false; {...код...} procedure TForm1.Button1MouseDown; {кнопка нажата и удерживается} begin drag:=true; {можно двигать} end; procedure TForm1.Button1MouseUp; {кнопка отпущена} begin drag:=false; {нельзя двигать} end; procedure TForm1.Button1MouseMove; begin if drag then {если можно двигать, то двигаем} begin {добавляем -5 и -30, чтобы учитывалась рамка формы} button1.left:=mouse.CursorPos.X-form1.Left-5; button1.top:=mouse.CursorPos.y-form1.top-30; end; end; 

    Another way is: assign the dmAutomatic property to the DragMode button (the button moves automatically), and the DragKind property - dkDock (the button will dock with other objects, as I understand it). Further. Some components, especially with a large and empty workspace (form, panel, all sorts of bars), have the DockSite property - in order for the button to sit on this component, this property must be true. True, the button may accidentally end up on its own, not docked, so you need to be careful (it will have a caption and a close button, that is, the user can accidentally destroy it). But it is convenient. By the way, there are additional events: OnDragDrop, OnDragOver, OnStartDock, OnStartDrag, OnEndDock, OnEndDrag. I hope you will deal with them yourself, if necessary.

    • Damn a bit late, while typing) But your answer is more detailed, so +1 to you) - Leshij_2005

    What's the problem?)

    We process the event when we hold the button with the mouse, like MouseDown) Well, for example, there is a boolean Down type variable, assigning true to it. Next, we process the event on the form when we move the mouse cursor, MouseMove. Here we write something like this:

     if( Down = true) begin button1.Left = X-10; button1.Top = Y -10; end; 

    where X, Y - the current coordinates of the mouse;

    If the button position is set through the Location type TPoint, then change them accordingly.

    Further, for example, if we have reached a certain position in X and Y, then we call button1.Click ();

    Well, I think the logic is clear, I don’t have Delphi right now, so I can’t give you a complete code.

    Be creative!

      If helped click +

      Drug and Drop property did not try to use? not the best option but the easiest in the properties of the button change

       DragKind - dkDock DragMode - dmAutomatic 

      or here is the approach to this question (in English, but everything is clear) link text