我有一个Delphi XE2项目,其中包含Label1,BitBtn1和
Image1等组件.我已经实现了表单拖动而没有标题栏写下面的代码:
private { Private declarations } procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest;
和
procedure TMainForm.WMNCHitTest(var Msg: TWMNCHitTest) ; begin inherited; if Msg.Result = htClient then Msg.Result := htCaption; end;
在我的表单中,Image1.OnMouseMove和Label1.OnClick事件对我的项目是强制性的,但它们不起作用.如何从客户区拖动表单,除了Image1和Label1区域?我可以做一件事,我可以使用一个TPanel,但它会破坏我的表单的GlassFrame和SheetofGlass属性.
解决方法
您需要使用WM_NCHITTEST消息中包含的位置信息.用它来确定此时是否存在控件.例如,您可以使用ControlAtPos方法.
procedure TMainForm.WMNCHitTest(var Msg: TWMNCHitTest); begin inherited; if ControlAtPos(ScreenToClient(Msg.Pos),True,True)=nil then if Msg.Result=htClient then Msg.Result := htCaption; end;
仅当您单击窗体上没有控件的点时,才允许拖动.您可能希望使用替代标准,但使用Msg.Pos是关键想法.