?
Solved

Stick to window?

Posted on 2010-03-25
5
Medium Priority
?
275 Views
Last Modified: 2012-08-13
I have an application, which creates additional forms. How can I make these additional forms stick to the main application window (right side). I want the top left of the modal form to stick to the top right of the main application form. when the main application moves, the other form should move with it, and visa versa

thanks
0
Comment
Question by:DelphiRulez
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 28667360
there might be other ways with not too old Delphi version, but one that would work always is to act on the onShow event of your subforms, and onMove of your main form (which is not exposed but can be)

about the onMove, if your other form are MODAL, you can't interact with the main form while those are open, so you don't have to bother with that and only need onShow
//=======================================================================
//== this should be put in a tool unit, as it will be used by many forms 

unit StickTools;

interface

Uses Form;

procedure StickToTopRight(formToStick,Mainform:TForm);

implementation

procedure StickToTopRight(formToStick,Mainform:TForm);
begin
 formToStick.Left:=Mainform.Left+Mainform.Width;
 formToStick.Top:=Mainform.Top;
end;

end.

//=======================================================================
// main form unit

Unit MainForm;

interface

type
 TForm1=class(TForm)
 protected
  procedure OnMove(var Msg: TWMMove); message WM_MOVE;
 end;

implementation

uses Unitform2,Unitform3, StickTools;

procedure TForm1.OnMove(var Msg: TWMMove);
begin
 inherited;
 if Assigned(form2) Then StickToTopRight(form2,self);
 if Assigned(form3) Then StickToTopRight(form3,self);
end

//=======================================================================
// modal form unit

type
 TForm2=class(TForm)
  procedure FormShow(Sender: TObject);
 public
//...
 end;

implementation

Uses StickTools, MainForm;

procedure TForm2.FormShow(Sender: TObject);
begin
 StickToTopRight(self, form1); 
end;

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 28709180
1.) When I run my application and display the secondary form. It comes up in the middle of the screen (do to the fact that position is set to poScreenCenter. It does not attach to the main form. Only when i move the main form, does it attach.

2.) Works if I move the main form, but not the secondary form. Can i make the main form move with the secondary form when the secondary form is moved?

3.) How do i make it work for modal forms as well

thanks

0
 
LVL 25

Expert Comment

by:epasquier
ID: 28710198
1) don't use poScreenCenter but poDesigned      
that way, the StickToTopRight call in FormShow will apply and not be override by poScreenCenter

2) create a StickToTopLeft procedure in StickTools
procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 formToStick.Left:=Mainform.Left-formToStick.Width;
 formToStick.Top:=Mainform.Top;
end;

declare onMove also on secondary form
procedure TForm2.OnMove(var Msg: TWMMove);  // message WM_MOVE;
begin
 StickToTopLeft(form1, Self);
end;

3) You just can't move a form (or click or input key or whatever act on it) when another is displayed above it with ShowModal
0
 

Author Comment

by:DelphiRulez
ID: 28714942
there is a problem somwheres. when i implement your code, my program runs, but my main form is hidden off the screen.

if i disabled the two lines below

procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 //formToStick.Left:=Mainform.Left-Mainform.Width;
 //formToStick.Top:=Mainform.Top;
end;

, then the main form is visable again, and works like before

i.e. move the main form and the secondary form moves with it
0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 28725665
I should have thought bout that, that's probably a typical case of events cross-talk.
event A is doing something that triggers event B that's doing somthing that should trigger A... until something really wrong happen.

in your case, when you show or move a form it wants to reposition the other form, then removes the first form...

solution in these case : disable the mechanism before doing the treatment, and reenable after
unit StickTools;

interface

Uses Form;

procedure StickToTopRight(formToStick,Mainform:TForm);

implementation

Const
 DisableStick:Boolean=False;

procedure StickToTopRight(formToStick,Mainform:TForm);
begin
 if DisableStick Then Exit;
 DisableStick:=True;
 formToStick.Left:=Mainform.Left+Mainform.Width;
 formToStick.Top:=Mainform.Top;
 DisableStick:=False;
end;

procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 if DisableStick Then Exit;
 DisableStick:=True;
 formToStick.Left:=Mainform.Left-formToStick.Width;
 formToStick.Top:=Mainform.Top;
 DisableStick:=False;
end;

end.

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

593 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