Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Moving objects with snapgrid functionalityGo to Tommy Andersen's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
N/A
Skill Level:
Scoring:
Last Update:
11/05/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Moving objects snapgrid
Times Scored:
3
Visits:
2385
Uploader: Tommy Andersen
Company: EasyWare
Reference: http://www.easyware.org/
 
Question/Problem/Abstract:
Here's an object that enables you to move your objects on a form easily during runtime...
Answer:



  TObjectMover = class(TObject)
  Private
    { Private declarations }
    MousePosInfo  : TPoint;
    CurrentObject : TControl;
    WindowHandle  : THandle;
    fSnapGrid     : Integer;

    Procedure WndProc(var Msg: TMessage);
  Protected
    { Protected declarations }
  Public
    { Public declarations }
    Constructor Create;
    Destructor Destroy; override;

    Function BeginMove(Obj: TControl) : Boolean;
  Published
    { Published declarations }
    Property SnapGrid : Integer read fSnapGrid write fSnapGrid;
  End;


Constructor TObjectMover.Create;
Begin
   Inherited Create;

   WindowHandle := AllocateHwnd(WndProc);

   fSnapGrid     := 8;
   MousePosInfo  := Point(0, 0);
   CurrentObject := NIL;
End;

Destructor TObjectMover.Destroy;
Begin
   DeallocateHwnd(WindowHandle);

   Inherited Destroy;
End;

Function TObjectMover.BeginMove(Obj: TControl) : Boolean;
Begin
   Result := False;

   IF (Assigned(Obj)) Then
   Begin
      IF (Assigned(Obj.Parent)) Then
      Begin
         Result := True;

         CurrentObject := Obj;
         MousePosInfo := CurrentObject.ScreenToClient(Mouse.CursorPos);
         CurrentObject.BringToFront;
         SetCapture(WindowHandle);
      End;
   End;
End;

Procedure TObjectMover.WndProc(var Msg: TMessage);
Var
  P : TPoint;

Begin
   IF (Msg.Msg = WM_MouseMove) Then
   Begin
      IF (Assigned(CurrentObject)) Then
      Begin
         P := Mouse.CursorPos;
         P := CurrentObject.Parent.ScreenToClient(P);
         Dec(P.x, MousePosInfo.x);
         Dec(P.y, MousePosInfo.y);

         CurrentObject.Left := P.x - (P.x mod fSnapGrid);
         CurrentObject.Top := P.y - (P.y mod fSnapGrid);
      End;
   End;

   IF (Msg.Msg = WM_LBUTTONUP) Then
   Begin
      IF (Assigned(CurrentObject)) Then
      Begin
         CurrentObject := NIL;
         ReleaseCapture;
      End;
   End;
End;





To test the new object:

Procedure TForm1.FormCreate(Sender: TObject);
Begin
   ObjectMover := TObjectMover.Create;
End;

Procedure TForm1.FormDestroy(Sender: TObject);
Begin
   ObjectMover.Free;
End;

Procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Begin
   IF (not ObjectMover.BeginMove(ListBox1)) Then ShowMessage('Failed to move object!');
End;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Something wrong?
    Mark (Nov 5 2002 1:16PM)

Hi,

I try to use your code but I think I need the class definition . Is  something missing because I can't reconstruct the class?

Thanks

Mark
Respond

RE: Something wrong?
Mark (Nov 5 2002 1:23PM)

OK first think then ask:

TObjectMover = class(TObject)
    private
      WindowHandle : HWND;
      fSnapGrid : integer;
      MousePosInfo : TPoint;
      CurrentObject : TControl;
    public
      constructor create;
      Destructor Destroy; override;
      Function BeginMove(Obj: TControl) : Boolean;
      Procedure WndProc(var Msg: TMessage);
  end;

Thanks

Mark
Respond

RE: RE: Something wrong?
Tommy Andersen (Nov 5 2002 2:23PM)

Sorry Mark,
I was a bit fast when posting so I forgot to attach the class declaration.
I've updated the article so it should be correct now. Thanks for reminding me!


Best Regards
Tommy Andersen
EasyWare.org

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)