How can I drag a form from my client area without disabling the functionality of child controls?

I have a Delphi XE2 project with components like Label1 , BitBtn1 and Image1 . I implemented drag-and-drop form without a header by entering the following code:

  private { Private declarations } procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest; 

and

 procedure TMainForm.WMNCHitTest(var Msg: TWMNCHitTest) ; begin inherited; if Msg.Result = htClient then Msg.Result := htCaption; end; 

In my form, the events Image1.OnMouseMove and Label1.OnClick are required for my project, but they do not work. How to drag a form from the client area, except for the Image1 and Label1 areas? I can do one thing that I can use one TPanel , but it will destroy the GlassFrame and SheetOfGlass my form.

+4
source share
1 answer

You will need to use the location information that is included in the WM_NCHITTEST message. Use this to determine if there is currently a control. For example, you can use the ControlAtPos method.

 procedure TMainForm.WMNCHitTest(var Msg: TWMNCHitTest); begin inherited; if ControlAtPos(ScreenToClient(Msg.Pos), True, True, True)=nil then if Msg.Result=htClient then Msg.Result := htCaption; end; 

This will allow you to drag and drop only if you clicked on a point in a form that does not have a control. You can use alternative criteria, but using Msg.Pos is the key idea.

+7
source

All Articles