Unable to click custom control at design time

I am creating a user control (inherited from TCustomControl ) in Delphi XE2 (and had this problem in other controls), and during development I cannot click them. I know that this has to do with capturing the mouse and catching mouse events and managing them differently during development than the runtime, but I don’t know how to place them correctly. In other words, from the many works that I can think of, I cannot decide which one is the right (or most effective) way.

I'm sure there must be a very simple standard for this, most likely using ControlStyle or CreateParams , but don't know what.

In this particular control (and I did not see the pattern in this problem), I collect messages, including WM_NCHITTEST and WM_LBUTTONDOWN . At design time, the control is 100% active as if it were a run-time, and when clicked, it would execute the run-time code instead.

I have a feeling that this is in the message handler of the test message, so here is this code (some things have been renamed):

 procedure TMyCustomControl.WMNCHitTest(var Message: TWMNCHitTest); var P: TPoint; Poly: TPoints; X: Integer; I: TMyCollectionItem; Ch: Bool; //Need to improve invalidation begin Ch:= False; P:= ScreenToClient(Point(Message.Pos.X, Message.Pos.Y)); for X := 0 to Items.Count - 1 do begin I:= Items[X]; Poly:= I.Points; FMouseIndex:= -1; FMouseState:= bmNone; if PointInPolygon(P, Poly) then begin //checks if point is within polygon FMouseIndex:= X; FMouseState:= bmHover; Ch:= True; Break; end; end; if Ch then Invalidate; end; 

And also my control constructor (stripped):

 constructor TMyCustomControl.Create(AOwner: TComponent); begin inherited; ControlStyle:= ControlStyle - [csDesignInteractive]; end; 
+4
source share
2 answers

But of course you are right. You do not return anything in the WM_NCHITTEST handler. Your Mmessage.Result is "0" (HTNOWHERE) when the handler is called and you do not assign it anything else.

Either call inherited at some point, or implement your logic and return (set HTCLIENT ) HTCLIENT for points that you consider to be the interior of your control.

This is what the desired runtime behavior is, you can enable development time checking (but I think you should do all these calculations for some reason):

 if csDesigning in ComponentState then Msg.Result := HTCLIENT; 
+6
source

The official way to support mouse interaction during development is to respond with a non-zero result to the CM_DESIGNHITTEST message. Then the component will receive regular mouse messages.

+5
source

Source: https://habr.com/ru/post/1412204/


All Articles