Solved

Delphi 7 - How do I make a panel be able to drag on mouse down?

Posted on 2011-03-12
10
3,002 Views
Last Modified: 2012-05-11
I need to make a panel where the user have the ability to drag it around the form, without moving the form itself, just the panel inside it.

How would I be able to do this?

Thanks!
0
Comment
Question by:John86a
[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
  • 4
10 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35119836
Set the TPanel Property "DragMode" to "dmAutomatic" and set a suitable cursor such as "DragCursor", then at the FormDragDrop and FormDragOver put the following code:
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if Source is TPanel then
    begin
      TPanel(Source).Left := X;
      TPanel(Source).Top := Y;
    end;
end;

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if Source is TPanel then
    begin
      Accept := True;
    end;
end;

Open in new window

Other properties (Form & Panel) remain with the default.
0
 

Author Comment

by:John86a
ID: 35119878
Hi, thanks for the answer. I tried your code, nothing happened.
I actually need only panel2 to be movable, since it will be mimicking a title bar aligned to the top of panel1.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35119888
Is your Panel placed directly on the form or there is any component (such as another panel, image,...) beneath it?
0
Technology Partners: 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!

 
LVL 24

Expert Comment

by:jimyX
ID: 35119922
Here is a similar question you can review it:
http://www.experts-exchange.com/Software/Photos_Graphics/Q_26863904.html
0
 

Author Comment

by:John86a
ID: 35119974
Thank you for the interest, to better illustrate it I made a sample on delphi of what I need.

In a nutshell: Panel2 inside Panel1, Panel2 aligned on top of Panel1 to mimic a title bar. Panel2, acting as a title bar, drags Panel1 around.
jimy.zip
0
 

Author Comment

by:John86a
ID: 35119995
By the way, I'd like to make that panel being able to drag smoothly throughout the form, as if it was a window.
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 35120221
Better approach is to use MouseDown and MouseUp:
var // global var
  MDown: Boolean = false;
  OPnt, NPnt : TPoint;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  If MDown then
    begin
      GetCursorPos(NPnt);
      Panel1.Left :=  Panel1.Left- OPnt.X + NPnt.X ;
      Panel1.Top := Panel1.Top- OPnt.Y + NPnt.Y   ;
      OPnt := NPnt;
    end;
end;

procedure TForm1.Panel2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MDown := True;
  GetCursorPos(OPnt);
end;

procedure TForm1.Panel2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MDown := False;
end;

Open in new window


Check the attachment please:
DragDrop-with-Mouse-down---up.zip
0
 

Expert Comment

by:ypenia
ID: 35120273
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  panel1.Perform(wm_nclbuttondown,HTCAPTION,0);
end;

end.

hope its help :)
0
 

Author Closing Comment

by:John86a
ID: 35121651
Perfect answer, thank you jimy.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

705 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