Solved

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

Posted on 2011-03-12
10
2,725 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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