?
Solved

drag button within panel

Posted on 2001-09-09
11
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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