[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-03-12
10
Medium Priority
?
3,283 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
Industry Leaders: 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
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…
Suggested Courses

656 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