Solved

drag button within panel

Posted on 2001-09-09
11
249 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
Comment Utility
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
Comment Utility
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
Comment Utility
listening...
0
 

Author Comment

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

Expert Comment

by:Jacco
Comment Utility
Hi ItsMe,

Which solution do you want explanation about?

Regards Jacco
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

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

Expert Comment

by:zebada
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
i forgot *gg*
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

15 Experts available now in Live!

Get 1:1 Help Now