windows – 如何在不禁用子控件功能的情况下从客户区拖动表单?

我有一个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是关键想法.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...