Form1为调用的窗体.
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
private
procedure CMMouseButton(var Message: TMessage); message WM_LBUTTONDOWN;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
end;
implementation
uses unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TForm2.Create(Form2) do try
ShowModal;
finally
Free;
end;
end;
procedure TForm1.CMMouseButton(var Message: TMessage);
begin
if (message.WParam = MK_LBUTTON) and inForm then begin
inMain := true;
showmessage('老兄,别在窗口外乱点。');
SetCapture(Application.MainForm.handle);
inMain := false;
end;
end;
procedure TForm1.CMMouseEnter(var Message: TMessage);
begin
if not assigned(form2) then
SetCapture(Application.MainForm.handle);
end;
procedure TForm1.CMMouseLeave(var Message: TMessage);
begin
ReleaseCapture
end;
Form2为ShowModal出的窗体.
TForm2 = class(TForm)
private
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
end;
var
Form2: TForm2;
inForm: boolean = false;
inMain: boolean =false;
implementation
procedure TForm2.CMMouseEnter(var Message: TMessage);
begin
ReleaseCapture;
inForm:=false;
end;
procedure TForm2.CMMouseLeave(var Message: TMessage);
begin
if inMain then exit;
SetCapture(Application.MainForm.handle);
inForm:=true;
end;