Jump to content
Tuts 4 You

Embed Form Into Other Apps Window [delphi]


Departure

Recommended Posts

Posted

Just wondering if anyone has infomation about embedding your delphi app into another window, For example adding a richedit control from your delphi form into another app or even the whole form.

I assume its done by using API calls but im not sure which ones, My guess is to find the handel of the control in other apps and set delphi form as child??

Posted

I dont think you can embed it, but you can use api's (Like setwindowpos) to put your control over the other window and keep it there.

Posted (edited)

Yeah when i said embed it I did'nt actually mean true embedding, I know how to do it with vb6, I wonder if anyone could translate for me to delphi or an example??

Vb6:

Private Sub Form_Load()
Dim otherappwindowclass As Long
Dim XPos As Long, YPos As Longotherappwindowclass = FindWindow("dlggroupchat window class", vbNullString) Call SetParent(Form1.hwnd, otherappwindowclass)
XPos = 360
YPos = 0
Call SetWindowPos(Form1.hwnd, 1, XPos, YPos, 100, 40, 1)
If otherappwindowclass = 0 Then
Exit Sub
End If
end sub
Edited by Departure
Posted (edited)

Well your if statement isnt right first off. You want to if before you attempt to setparent. But I think I get what are looking to do. Delphi isn't one of my strong languages, but I can provide you with a rough sketch in asm.

invoke FindWindow,addr buffer,NULL
mov ebx,eax;Do this as eax will be overwritten
.if ebx!=0
invoke SetParent,ebx,hWin
invoke SetWindowPos,ebx,HWND_TOPMOST,XPos,YPos,100,40,1
.endif

Of course you would have to set the variables, but thats the general idea.

Edited by Pimp.exe
Posted (edited)

Just wrote this, and tested...

Still needs some fixing :)

But it will get you a bit on the way...

procedure TForm1.Button1Click(Sender: TObject);
var
XPos: integer;
YPos: integer;
hWindow: hwnd;
begin
hWindow := FindWindow(nil,PChar('TranWin')); //Replace TranWin with what you want :)
if hWindow <> 0 then
begin
Form1.ParentWindow := hWindow;
XPos := 0;
YPos := 30;
SetWindowPos(Form1.ParentWindow, 100, XPos, YPos, 100, 40, SWP_SHOWWINDOW);
end
else
ShowMessage('Window not found!');
end;
Edited by ante0
Posted

Thanks guys i'll try it out, the delphi code that ante0 did looks nearly the same as vb6, So this is exactly what i needed.

P.s yes i just relized I put the if statment in the wrong spot should of benn at the begining...

If otherappwindowclass = 0 Then

Exit Sub

Else

Call SetParent(Form1.hwnd, otherappwindowclass)

XPos = 360

YPos = 0

Call SetWindowPos(Form1.hwnd, 1, XPos, YPos, 100, 40, 1)

End if

End sub

I was half a sleep when writting that hehehe,

Thanks again guys, Im new to delphi but im getting there.....

Posted (edited)
Just wrote this, and tested...

Still needs some fixing :)

But it will get you a bit on the way...

procedure TForm1.Button1Click(Sender: TObject);
var
XPos: integer;
YPos: integer;
hWindow: hwnd;
begin
hWindow := FindWindow(nil,PChar('TranWin')); //Replace TranWin with what you want :)
if hWindow <> 0 then
begin
Form1.ParentWindow := hWindow;
XPos := 0;
YPos := 30;
SetWindowPos(Form1.ParentWindow, 100, XPos, YPos, 100, 40, SWP_SHOWWINDOW);
end
else
ShowMessage('Window not found!');
end;

Oh, in SetWindowPos

Change Form1.ParentWindow to Form1.Handle

otherwise it wont place itself on X,Y.

It will move the Window that you captured. hehe.

You can open up Delphi Help and search for SetWindowPos if you want to have some more options, and to understand it better. Though I guess it's pretty similar to VB6.

Oh, and one more thing.

Change SWP_SHOWWINDOW to SWP_NOSIZE or something else.

SHOWWINDOW makes the Form unmovable.

You could also add an Edit to the form, if you don't know what window you want to capture.

hWindow := FindWindow(nil,PChar('TranWin'));

to

hWindow := FindWindow(nil,PChar(Edit1.Text));
Edited by ante0
Posted (edited)

Okay the code you gave did'nt work correctly for me so I changed delphi setparent to windows.setparent (child,Parent), now it works fine,

Thanks very much for the input :thumbsup:

procedure TForm1.Button1Click(Sender: TObject);
var
XPos: integer;
YPos: integer;
hWindow: hwnd;begin
hWindow := FindPalWindowByCaption('- Voice Room'); //this here just calls another function to find the window
if hWindow <> 0 then
begin
windows.SetParent(form1.WindowHandle,hWindow); //Here is where I changed
XPos := 360;
YPos := 0;
SetWindowPos(Form1.WindowHandle, 1, XPos, YPos, 321, 25, SWP_SHOWWINDOW); //And ofcause here for the form handel
end
else
ShowMessage('Window not found!');
end;

//Edit

I just seen the post above this one, So yeah I did what you said before I read your post.

Thanks again for all your help :D

Edited by Departure
Posted

Ah, my bad.

Thanks for adding Comments, they're really useful, and I tend to forget to add em. lol

Oh btw, If you maximize Parent window after executing the code, is the child still movable for you?

I can't move it when I maximize, if I restore it I can...

I think I'll add the same as you, windows.SetParent(form1.WindowHandle,hWindow);

Though I'm at work now, so I'll have to try it later :)

Happy coding!

Posted

actually windows.setparent seems to have no problem moving the child form around in the parent (other app) while maximized, So yes the child is still movable, I set my forms border style to none then ofcause the form is not movable in the parent window, but for testing puposes I did change it back to single border and toolbar border and both where movable...

Posted
actually windows.setparent seems to have no problem moving the child form around in the parent (other app) while maximized, So yes the child is still movable, I set my forms border style to none then ofcause the form is not movable in the parent window, but for testing puposes I did change it back to single border and toolbar border and both where movable...

Ah good, I will add this later on to my sources :)

Hmm, if you don't mind me asking, what are you going to use this for? :)

Posted

I am making a text fader that "embeds" a richtext control into the toolbar of paltalk, Its all working good but I have one problem..

If I close paltalk room window before I restore the embeded form I loose the form and can'nt get it back and some times gives errors, So i have close it by either task manger or the taskbar. I have tryed a couple of things but can'nt seem to get it..

Example:

Things to note here is that I change the borderstyle to none when i position it in the paltalk toolbar...

procedure TForm1.Timer1Timer(Sender: TObject);
var
palwin : Hwnd;
begin
palwin := FindPalWindowByCaption('- Voice Room'); // just another function to find the correct window
if (palwin <> 1) and (form1.BorderStyle = bsNone) then //This was my plan until I relized it wont work because the form got killed with the paltalk room window :(begin
windows.SetParent(form1.WindowHandle,0); // take my form out of the paltalk window
form1.Height := 245; // reset my hight
form1.Width := 332; // reset my width
form1.BorderStyle := bsSingle; // change the border style back
end
end;

Anyway I think the only way to get around this is hooking the windows message to close the paltalk window, then you could "restore" the delphi form before the message got through..

I dont really know but thats my theory, Hooking in delphi is something I need to read up on, It was hard enough in vb6 let alone delphi

Posted (edited)

Ah, yes, I have the same problem.

Well, when I tried it in Delphi it's easy cause you can reset the program.

Since the Parent is closed, the Child still stays in it.

Then you'd have to force close it.

I was thinking something like this...

first set hWindow as a global variable.

var
Form1: TForm; //This will be ther already.
hWindow: hwnd;

and remove it in the other procedure as a variable

in a timer you can use.

if hWindow = 0 then
begin
Form1.Application.Terminate;
end;
end;

I think this will do it... I don't have Delphi here so I can't try it out :(

You could set the timer to go off every 5 seconds, and it could be enabled right after you set

SetWindowPos(Form1.WindowHandle, 1, XPos, YPos, 321, 25, SWP_SHOWWINDOW); 
Timer1.Enabled := True;

I'm not at all positive this will work, since the form is set as child...

But it's no harm in trying it out :)

Edited by ante0
Posted

thats pretty much what I had in post before except I was tryingto restore the form not terminate it, Needs to be restored so i can use it in another window, Unless ofcause I could program it to creat a .bat file which then executes the delphi app exe then after deltes the .bat file ............ Lmao danmm thats the long way around but its easyer than hooking the close message from window and restoring before the actuall window recives the message.

Thanks for all your help mann its greatly appreciated

Posted
thats pretty much what I had in post before except I was tryingto restore the form not terminate it, Needs to be restored so i can use it in another window, Unless ofcause I could program it to creat a .bat file which then executes the delphi app exe then after deltes the .bat file ............ Lmao danmm thats the long way around but its easyer than hooking the close message from window and restoring before the actuall window recives the message.

Thanks for all your help mann its greatly appreciated

I guess...

var
myFile : TextFile;
text : string;
File1 : string;beginCreateFile(pchar(ExtractFilePath(ParamStr(0))'Test.bat'),GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
File1 := ExtractFilePath(ParamStr(0))+'Test.bat';
AssignFile(myFile, 'Test.bat);
ReWrite(myFile);
WriteLn(myFile, 'Hello');
WriteLn(myFile, 'World');
CloseFile(myFile);
ShellExecute(Handle,'open', pChar(File1),nil, nil, SW_SHOWNORMAL);
end;Remember to add ShellAPI; to Uses clause.

Maybe that will help you with the .bat file ;)

I think it would be easier with just coding though. hehe

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...