Solved

drag button within panel

Posted on 2001-09-09
11
252 Views
Last Modified: 2010-04-06
hi! i'm creating a various number of buttons. i would like that my users can position them within a panel component where they want, by moving them with the mouse. i'm using d4 standard but a special button component which does not support dock/drag propertys.

kind regards
ItsMe
0
Comment
Question by:ItsMe
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 6469020
Hi ItsMe,

Here is a way you can move all TWinControls. I don't know if the TControl.WondowProc is supported under D4 but I can write a replacement function for it.

Just attach the control you want to move. And drag the control. Call detach if you want to no longer drag the control.

Regards Jacco

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TMover = class
  private
    FControl: TControl;
    FOldProc: TWndMethod;
    FDragging: Boolean;
    FOfsX: Integer;
    FOfsY: Integer;
    FLeft: Integer;
    FTop: Integer;
  public
    destructor Destroy; override;
    procedure NewWndProc(var Message: TMessage);
    procedure Attach(AControl: TControl);
    procedure Detach;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FMover: TMover;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
begin
  FMover.Attach(Button1);
end;

{ TMover }

procedure TMover.Attach(AControl: TControl);
begin
  if FControl <> AControl then
  begin
    if FControl <> nil then
      Detach;
    FControl := AControl;
    if Assigned(FControl) then
    begin
      FOldProc := AControl.WindowProc;
      AControl.WindowProc := NewWndProc;
    end;
  end;
end;

destructor TMover.Destroy;
begin
  Detach;
  inherited Destroy;
end;

procedure TMover.Detach;
begin
  if Assigned(FControl) then
  begin
    FControl.WindowProc := FOldProc;
    FControl := nil;
    FOldProc := nil;
  end;
end;

procedure TMover.NewWndProc(var Message: TMessage);
var
  lbHandled: Boolean;
  liX, liY: Integer;
  lRect: TRect;
begin
  lbHandled := False;
  with Message do
  begin
    if Msg = WM_LBUTTONDOWN then
    begin
      lbHandled := True;
      FDragging := True;
      FOfsX := LParam and $FFFF;
      FOfsY := LParam shr 16;
      FLeft := FControl.Left;
      FTop := FControl.Top;
      with FControl.Parent do
      begin
        lRect.TopLeft     := Point(Left, Top);
        lRect.BottomRight := Point(Left+Width, Top+Height);
      end;
      SetCapture(TWinControl(FControl).Handle);
      ClipCursor(@lRect);
    end;
    if Msg = WM_LBUTTONDBLCLK then
      lbHandled := True;
    if Msg = WM_MOUSEMOVE then
    begin
      if FDragging then
      begin
        liX := LParam and $FFFF;
        liY := LParam shr 16;
        FControl.SetBounds(FLeft + (liX - FOfsX), FTop + (liY - FOfsY),FControl.Width, FControl.Height);
        FLeft := FControl.Left;
        FTop := FControl.Top;
        lbHandled := True;
      end;
    end;
    if Msg = WM_LBUTTONUP then
    begin
      lbHandled := True;
      FDragging := False;
      ClipCursor(nil);
      ReleaseCapture;
    end;
  end;
  if not lbHandled then
    FOldProc(Message);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FMover := TMover.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FMover.Free;
end;

end.
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 6469085
MUCH less code than that would be:

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
  const
  SC_DragMove = $F012;  { a magic number }
  begin
  ReleaseCapture;
  Button1.perform(WM_SysCommand, SC_DragMove, 0);
end;


(this is from my website: www.drdelphi.com)

-Good luck!!

0
 
LVL 6

Expert Comment

by:zebada
ID: 6469173
listening...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:ItsMe
ID: 6469719
i guess i don't need to understand this ? :-)
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6469871
Hi ItsMe,

Which solution do you want explanation about?

Regards Jacco
0
 

Author Comment

by:ItsMe
ID: 6470621
hi jacco! the last, short one :-)
0
 
LVL 6

Expert Comment

by:zebada
ID: 6470633
If you're referring to the "listening..." comment, that's just so I get email notification when someone adds a comment. I am also interested in how this is done.
0
 

Author Comment

by:ItsMe
ID: 6470644
hihi no, i ment the code from drdelphi. it works pretty fine but i don't know why :-)
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6471005
Eseentially what's happening is that you are telling the form NOT to process the mouse move (ReleaseCapture), then you send the SC_Drag message to the button (or a panel, editbox, whatever). Real easy! :-)  Now, how about some points?<g>

-Dave
0
 

Author Comment

by:ItsMe
ID: 6471352
i forgot *gg*
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6473016
thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question