// if (Rect.Bottom - Rect.Top <> Cy) or //尺寸变更必要时 // (Rect.Right - Rect.Left <> Cx) then // uFlag2 := uFlag2 and (not SWP_NOSIZE就不会改变 form2 的 height ...
var F1Top, F1Left: integer; procedure TForm1.FormShow(Sender: TObject); begin if not form2.Visible then begin form2.Show; form2.Top := 400; end; F1Top := self.Top; F1Left := self.Left; end;const uFlag = SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE;procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged); var Rect: TRect; X, Y, Cx, Cy: Integer; uFlag2: UINT; begin inherited; if Form2 <> nil then //如果子窗体存在 begin GetWindowRect(Form2.Handle, Rect); //获得子窗体的位置 with Msg do begin X := Form2.Left + (WindowPos^.x - F1Left); //新X坐标 Y := Form2.Top + (WindowPos^.y - F1Top); //新Y坐标 Cx := WindowPos^.cx; //新宽度 Cy := WindowPos^.cy; //新高度 end; uFlag2 := uFlag; //移动必要时 if (Rect.Top <> Y) or (Rect.Left <> X) then uFlag2 := uFlag2 and (not SWP_NOMOVE); // if (Rect.Bottom - Rect.Top <> Cy) or //尺寸变更必要时 // (Rect.Right - Rect.Left <> Cx) then // uFlag2 := uFlag2 and (not SWP_NOSIZE); if uFlag <> uFlag2 then //都必要时 SetWindowPos(Form2.Handle, 0, X, Y, Cx, Cy, uFlag2); end; F1Top := self.Top; F1Left := self.Left; end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin FreeAndNil(Form2); end;
>>这个是的,但是怎么也能吸附在左边呢?而且只是隔20像素时, 加个判断: procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin FreeAndNil(Form2); end;var F1Top, F1Left: integer;procedure TForm1.FormShow(Sender: TObject); begin if not form2.Visible then begin form2.Show; form2.Top := 400; F1Top := self.Top; F1Left := self.Left; end; end;const uFlag = SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE;procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged); var Rect: TRect; X, Y, Cx, Cy: Integer; uFlag2: UINT; begin inherited; if Form2 <> nil then //如果子窗体存在 begin if (Form2.Left - (F1Left + Form1.Width)) < 20 then begin GetWindowRect(Form2.Handle, Rect); //获得子窗体的位置 with Msg do begin X := Form2.Left + (WindowPos^.x - F1Left); //新X坐标 Y := Form2.Top + (WindowPos^.y - F1Top); //新Y坐标 Cx := WindowPos^.cx; //新宽度 Cy := WindowPos^.cy; //新高度 end; uFlag2 := uFlag; //移动必要时 if (Rect.Top <> Y) or (Rect.Left <> X) then uFlag2 := uFlag2 and (not SWP_NOMOVE); if uFlag <> uFlag2 then //都必要时 SetWindowPos(Form2.Handle, 0, X, Y, Cx, Cy, uFlag2); end; end; F1Top := self.Top; F1Left := self.Left; end;
修改成这样好点: if Form2 <> nil then //如果子窗体存在 begin if (abs((Form2.Left - (F1Left + Form1.Width))) < 20) or (abs(Form2.top - (F1Top + Form1.Height)) < 20) or (abs(F1Left - (Form2.Left + Form2.Width)) < 20) or (abs(F1Top - (Form2.Top + Form2.Height)) < 20) then
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
procedure WMWindowPosChanged(var Msg: TWMWindowPosChanged);message WM_WINDOWPOSCHANGED; public
{ Public declarations }
end;var
Form1: TForm1;const uFlag = SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE;implementationuses Unit2;{$R *.dfm}{ TForm1 }procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
var
Rect: TRect;
X, Y, Cx, Cy: Integer;
uFlag2: UINT;
begin
inherited;
if Form2 <> nil then //如果子窗体存在
begin
GetWindowRect(Form2.Handle, Rect); //获得子窗体的位置
with Msg do
begin
X := WindowPos^.x + WindowPos^.cx; //新X坐标
Y := WindowPos^.y; //新Y坐标
Cx := WindowPos^.cx; //新宽度
Cy := WindowPos^.cy; //新高度
end;
uFlag2 := uFlag; //移动必要时
if (Rect.Top <> Y) or (Rect.Left <> X) then
uFlag2 := uFlag2 and (not SWP_NOMOVE);
if (Rect.Bottom - Rect.Top <> Cy) or //尺寸变更必要时
(Rect.Right - Rect.Left <> Cx) then
uFlag2 := uFlag2 and (not SWP_NOSIZE);
if uFlag <> uFlag2 then //都必要时
SetWindowPos(Form2.Handle, 0, X, Y, Cx, Cy, uFlag2);
end;
end;
是不是form2里面自己有修改的代码
不过太复杂了,我试改了一部分,大部分还在想,五颗星,能在帮帮吗?我急用。
// (Rect.Right - Rect.Left <> Cx) then
// uFlag2 := uFlag2 and (not SWP_NOSIZE就不会改变 form2 的 height ...
procedure TForm1.FormShow(Sender: TObject);
begin
if not form2.Visible then
begin
form2.Show;
form2.Top := 400;
end; F1Top := self.Top;
F1Left := self.Left;
end;const uFlag = SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE;procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
var
Rect: TRect;
X, Y, Cx, Cy: Integer;
uFlag2: UINT;
begin
inherited;
if Form2 <> nil then //如果子窗体存在
begin
GetWindowRect(Form2.Handle, Rect); //获得子窗体的位置
with Msg do
begin
X := Form2.Left + (WindowPos^.x - F1Left); //新X坐标
Y := Form2.Top + (WindowPos^.y - F1Top); //新Y坐标
Cx := WindowPos^.cx; //新宽度
Cy := WindowPos^.cy; //新高度
end;
uFlag2 := uFlag; //移动必要时
if (Rect.Top <> Y) or (Rect.Left <> X) then
uFlag2 := uFlag2 and (not SWP_NOMOVE);
// if (Rect.Bottom - Rect.Top <> Cy) or //尺寸变更必要时
// (Rect.Right - Rect.Left <> Cx) then
// uFlag2 := uFlag2 and (not SWP_NOSIZE);
if uFlag <> uFlag2 then //都必要时
SetWindowPos(Form2.Handle, 0, X, Y, Cx, Cy, uFlag2);
end; F1Top := self.Top;
F1Left := self.Left;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(Form2);
end;
加个判断:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(Form2);
end;var F1Top, F1Left: integer;procedure TForm1.FormShow(Sender: TObject);
begin
if not form2.Visible then
begin
form2.Show;
form2.Top := 400;
F1Top := self.Top;
F1Left := self.Left;
end;
end;const uFlag = SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE;procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
var
Rect: TRect;
X, Y, Cx, Cy: Integer;
uFlag2: UINT;
begin
inherited;
if Form2 <> nil then //如果子窗体存在
begin
if (Form2.Left - (F1Left + Form1.Width)) < 20 then
begin GetWindowRect(Form2.Handle, Rect); //获得子窗体的位置
with Msg do
begin
X := Form2.Left + (WindowPos^.x - F1Left); //新X坐标
Y := Form2.Top + (WindowPos^.y - F1Top); //新Y坐标
Cx := WindowPos^.cx; //新宽度
Cy := WindowPos^.cy; //新高度
end;
uFlag2 := uFlag; //移动必要时
if (Rect.Top <> Y) or (Rect.Left <> X) then
uFlag2 := uFlag2 and (not SWP_NOMOVE); if uFlag <> uFlag2 then //都必要时
SetWindowPos(Form2.Handle, 0, X, Y, Cx, Cy, uFlag2);
end;
end;
F1Top := self.Top;
F1Left := self.Left;
end;
if Form2 <> nil then //如果子窗体存在
begin
if (abs((Form2.Left - (F1Left + Form1.Width))) < 20) or
(abs(Form2.top - (F1Top + Form1.Height)) < 20) or
(abs(F1Left - (Form2.Left + Form2.Width)) < 20) or
(abs(F1Top - (Form2.Top + Form2.Height)) < 20) then