Solved

dragging and dropping forms

Posted on 2002-06-18
7
350 Views
Last Modified: 2010-04-04
Hi there,

I have a few standard little forms in my application. I show these forms on top of my application, they are pretty small because all they contain are some textboxes. On the main form, I want to be able to drag these standard forms to the leftside of the main form and 'snap' them to that side. Just like toolbars you can drag/drop. Any idea how to accomplish this?

Bye,
Jeroen
0
Comment
Question by:jccommandeur
  • 3
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
edey earned 100 total points
ID: 7090031
you can call manulaDock to get the little forms docked, the tricky part is knowing when. TCustomForm doesn't have a OnMoving event, but the form will get WM_MOVE and WM_WINDOWPOSCHANGING messages. All you have to do is catch either one, test whether the form is closed enough to the edge of the first yet, and if it is then call manualDock.

GL
Mike

an example for the form to be docked:

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
  private
         procedure doMove(var msg : TWMMOVE);message WM_MOVE;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.doMove(var msg : TWMMOVE);
begin
     if abs(msg.XPos - form1.left) < 20 then
     begin
          if abs(form1.Top+(form1.Height div 2)-msg.YPos) <= form1.Height div 2 then
             manualDock(form1,nil,alLeft);
     end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
     show;
end;

end.
0
 

Author Comment

by:jccommandeur
ID: 7090829
Thanks edey, this is starting to look like it. But... after docking the form, I also want to be able to undock and move it around like usual. You know, just like ordinary toolbars etc. Is that possible?

Jeroen
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7091584
Something like this?

Form1:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ToolWin, ComCtrls;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    procedure ToolBar1UnDock(Sender: TObject; Client: TControl;
      NewTarget: TWinControl; var Allow: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ToolBar1UnDock(Sender: TObject; Client: TControl;
  NewTarget: TWinControl; var Allow: Boolean);
begin
  Allow := True;
end;

end.



And the form content:

object Form1: TForm1
  Left = 220
  Top = 138
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ToolBar1: TToolBar
    Left = 0
    Top = 0
    Width = 688
    Height = 29
    AutoSize = True
    Caption = 'ToolBar1'
    DockSite = True
    DragKind = dkDock
    TabOrder = 0
    OnUnDock = ToolBar1UnDock
  end
end
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7091586
Form2's code:

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDblClick(Sender: TObject);
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormStartDock(Sender: TObject;
      var DragObject: TDragDockObject);
  private
    { Private declarations }
  public
    { Public declarations }
    isMouseDown: Boolean;
    OldWidth, OldHeight: Integer;
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormCreate(Sender: TObject);
begin
  isMouseDown := False;
end;

procedure TForm2.FormDblClick(Sender: TObject);
begin
  ShowMessage('Hi!');
end;

procedure TForm2.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMouseDown := True;
end;

procedure TForm2.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMouseDown := False;
end;

procedure TForm2.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  APoint: TPoint;
begin
  if isMouseDown then
    if not Floating then
    begin
      isMouseDown := False;
      APoint := ClientToScreen(Point(X, Y));
      ManualFloat(Rect(APoint.x, APoint.y,
        OldWidth + APoint.x, APoint.y + OldHeight));
    end;
end;

procedure TForm2.FormStartDock(Sender: TObject;
  var DragObject: TDragDockObject);
begin
  OldWidth := Width;
  OldHeight := Height;
end;

end.



And the content:

object Form2: TForm2
  Left = 657
  Top = 101
  Width = 284
  Height = 227
  Caption = 'Form2'
  Color = clYellow
  DragKind = dkDock
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Visible = True
  OnCreate = FormCreate
  OnDblClick = FormDblClick
  OnStartDock = FormStartDock
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 276
    Height = 200
    Align = alClient
    BevelOuter = bvNone
    Caption = 'Panel1'
    Color = clYellow
    TabOrder = 0
    OnMouseDown = Panel1MouseDown
    OnMouseMove = Panel1MouseMove
    OnMouseUp = Panel1MouseUp
  end
end










HTH
DragonSlayer
0
 

Author Comment

by:jccommandeur
ID: 7091996
DragonSlayer,

I don't get what you're trying to do here. Can you explain?

Jeroen
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7092072
umm... create two forms, form1 and form2.

at form1, right-click and choose view as text, then replace the content with the form1 content (above). also, replace the form's code with the form1 code above. Repeat for form2. Then run the programme.
0
 

Author Comment

by:jccommandeur
ID: 7092142
I understand that part, but I don't see what the program is supposed to do. Are you trying to dock form2 in the toolbar of form1? If so, it doesn't work here... maybe you can send me a working example to jeroen.commandeur@hccnet.nl? thanks in advance!

Jeroen
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

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