提示: SetParent 应该 Windows.SetParent, 因为 TForm 的父类有同名方法.
//声明:
{获取父窗口句柄}
GetParent(hWnd: HWND): HWND
{指定父窗口}
SetParent(
hWndChild: HWND
hWndNewParent: HWND {父句柄}
): HWND
{移动窗口}
MoveWindow(
hWnd: HWND
X, Y: Integer
nWidth, nHeight: Integer
bRepaint: BOOL {True 表示刷新
): BOOL
//举例:
unit Unit1
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls
type
TForm1 = class(TForm)
Edit1: TEdit
Button1: TButton
procedure Button1Click(Sender: TObject)
end
var
Form1: TForm1
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject)
begin
if GetParent(Edit1.Handle)=Handle then
begin
Windows.SetParent(Edit1.Handle, Button1.Handle)
MoveWindow(Edit1.Handle, 0,0, Edit1.Width, Edit1.Height, True)
end else begin
Windows.SetParent(Edit1.Handle, Self.Handle)
MoveWindow(Edit1.Handle, 0,0, Edit1.Width, Edit1.Height, True)
end
end
end.