Solved

dragging and dropping forms

Posted on 2002-06-18
7
333 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

17 Experts available now in Live!

Get 1:1 Help Now