Solved

drag button within panel

Posted on 2001-09-09
11
250 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 259
Delphi TcxGrid group footer summary 3 203
FMX enumerated colours 2 84
Delphi 2 46
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now