Solved

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

Posted on 2011-03-12
10
2,571 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
Comment Utility
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
Comment Utility
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
Comment Utility
Is your Panel placed directly on the form or there is any component (such as another panel, image,...) beneath it?
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
Here is a similar question you can review it:
http://www.experts-exchange.com/Software/Photos_Graphics/Q_26863904.html
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:John86a
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect answer, thank you jimy.
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

Title # Comments Views Activity
Delphi selector screen 2 57
Replace a tag with sed 2 41
allStar challenge 1 40
White board coding practice 3 60
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 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

8 Experts available now in Live!

Get 1:1 Help Now