Solved

moving Controls At Run Time ( Code supplied )

Posted on 2004-10-05
2
314 Views
Last Modified: 2010-04-05
I need some assistance on the following code:
It is used to "Move Controls" at "Run Time".

With this code in place, you are able to move not only the Controls.
But the Form as well. And I DO NOT want the form to move in the
Project that this code is going to be used in.
I am going to use it as a basis of a "Desktop" type of program.
So, like your Desktop, it does not move.

Anyway, can someone please assist me in the following code.?
I just want the Control that " I Specify " to be moved at "Run Time"

Thank you All
Carrzkiss

//=========Code Start=============\\
unit Unit1;

interface

uses
  Windows,WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls, Menus, registry;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    ListBox1: TListBox;
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

const
  SC_SIZE = $F012;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  iTemp: Integer;
  r : TRegIniFile;
begin
  (* Open the key in the registry where we store all the positions *)
  r := TRegIniFile.Create('Resize Test');

  (* Loop through all the components on a form *)
  for iTemp := 0 to ComponentCount -1 do
    (* Get the position of all controls *)
    if Components[iTemp] is TControl then
    with Components[iTemp] as TControl Do
    begin
      left:=r.readinteger(Name,'Left',left);
      top:=r.readinteger(Name,'Top',Top);
      (* Make there cursors crosses *)
      cursor:=crCross;
    end;
  (* Release the registry object *)
  r.free;

  (* Use our own message handler for all message sent to the application *)
  Application.OnMessage := AppMessage;
end;

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  (* Only do anything if the left mouse button is pressed *)
  if  msg.message = wm_LBUTTONDOWN then
  begin
   (* Release the mouse capture *)
   WinProcs.ReleaseCapture;
   (* Send a message to the control under the mouse to go into move mode *)
   postmessage    ( msg.hwnd,WM_SysCommand, SC_SIZE, 0 );
   (* Say we have handled this message *)
   Handled:=true;
  end;
end;


procedure TForm1.FormDestroy(Sender: TObject);
var
  iTemp: Integer;
  r : TRegIniFile;
begin
  (* As in the form create loop through all the components But this *)
  (* time write the left and top properties to the registry         *)

  r := TRegIniFile.Create('Resize Test');
  for iTemp := 0 to ComponentCount -1 do
    if Components[iTemp] is TControl then
    with Components[iTemp] as TControl Do
    begin
      r.writeinteger(Name,'Left',left);
      r.writeinteger(Name,'Top',Top);
    end;
  r.free;
  end;
end.
//===========End of Code==============\\


0
Comment
Question by:Wayne Barron
[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
2 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 12235204
Hi,

Perhaps you could check if the control under the mouse is the form itself or not:

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  (* Only do anything if the left mouse button is pressed *)
  if  msg.message = wm_LBUTTONDOWN then
  begin
    if msg.hwnd <> Form1.Handle then begin // <- added
     (* Release the mouse capture *)
     WinProcs.ReleaseCapture;
     (* Send a message to the control under the mouse to go into move mode *)
     postmessage    ( msg.hwnd,WM_SysCommand, SC_SIZE, 0 );
     (* Say we have handled this message *)
     Handled:=true;
    end;  // <- added
  end;
end;

Regards, Geo
0
 
LVL 31

Author Comment

by:Wayne Barron
ID: 12236816
Hello [geobul];

  Great, great work.

  I have an unlimited amount of control on the forms that are to stay
In there default position. And having to add in each control when designing
Could be a little tedious (to say the least), But atleast everything is working like it suppose too.

  Thank you very much.
Carrzkiss
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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