Solved

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

Posted on 2011-03-12
10
2,828 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

856 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