Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

drag button within panel

Posted on 2001-09-09
11
Medium Priority
?
258 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

610 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