quantum2
asked on
Magnetic Windows
Hi,
I want to write a program that will dock and or align (left,right,top,bottom) with another application. Much like the menus in Photoshop how they dock and align with each other. However, my application needs to do this with another running application not of my creation.
So the use case is as follows:
1- Program Is launched
2- Program Detects that 3rd party application is running
3- Based on the third party location on the screen my application will align itself to the left or right hand side of the 3rd party application
4- Based on user settings, this shouls work for top and bottom as well where my program aligns with the bottom or top of the 3rd party applications form.
This of this as making a docking toolbar for notepad. How would you make a bar doc with the window of notepad..?
Thanks
Q2
I want to write a program that will dock and or align (left,right,top,bottom) with another application. Much like the menus in Photoshop how they dock and align with each other. However, my application needs to do this with another running application not of my creation.
So the use case is as follows:
1- Program Is launched
2- Program Detects that 3rd party application is running
3- Based on the third party location on the screen my application will align itself to the left or right hand side of the 3rd party application
4- Based on user settings, this shouls work for top and bottom as well where my program aligns with the bottom or top of the 3rd party applications form.
This of this as making a docking toolbar for notepad. How would you make a bar doc with the window of notepad..?
Thanks
Q2
If You want to keep moving with the other window You can do like this.
That is use timer.
Maybe not the most beautifull, but working.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure PlaceWindow;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
PlaceWindow;
Timer1.Interval := 10;
end;
procedure TForm1.PlaceWindow;
var
h: THandle;
r: TRect;
begin
h := FindWindow('notepad',nil);
if h <> 0 then
begin
getWindowRect(h,r);
Left := r.Right;
Top := r.Top;
Height := r.Bottom - r.Top;
{
Left := r.Left;
Top := r.Bottom;
Width := r.Right - r.Left;
}
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
PlaceWindow;
end;
end.
That is use timer.
Maybe not the most beautifull, but working.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure PlaceWindow;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
PlaceWindow;
Timer1.Interval := 10;
end;
procedure TForm1.PlaceWindow;
var
h: THandle;
r: TRect;
begin
h := FindWindow('notepad',nil);
if h <> 0 then
begin
getWindowRect(h,r);
Left := r.Right;
Top := r.Top;
Height := r.Bottom - r.Top;
{
Left := r.Left;
Top := r.Bottom;
Width := r.Right - r.Left;
}
end;
end;
procedure TForm1.Timer1Timer(Sender:
begin
PlaceWindow;
end;
end.
ASKER
Ideally, the program would "attach" to the parent application (in this case Notepad) if it was within 10 pixels of the notepad.exe window. Otherwise it could be placed anywhere and I could remember its previous location when I start the app.
Do you have an idea on how to make it dock like that (when the right side of my form is 10 pixels from the left of the Notepad window) and the same for the other corresponding sides..?
I will try what you have already sent in...
Thanks
Q2
Do you have an idea on how to make it dock like that (when the right side of my form is 10 pixels from the left of the Notepad window) and the same for the other corresponding sides..?
I will try what you have already sent in...
Thanks
Q2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help. Both solutions did indeed work. Mokule, I will create a question for you to award some points. I do appreciate the help from both of you
-Q2
-Q2
You must first launch notep to see how it works.
procedure TForm1.FormCreate(Sender: TObject);
var
h: THandle;
r: TRect;
begin
h := FindWindow('notepad',nil);
if h <> 0 then
begin
getWindowRect(h,r);
Left := r.Right;
Top := r.Top;
Height := r.Bottom - r.Top;
{
// this code is to place window at the bottom
Left := r.Left;
Top := r.Bottom;
Width := r.Right - r.Left;
}
end;
end;