procedure TWinControl.WndProc(var Message: TMessage);
var
  Form: TCustomForm;
  LMouseEvent: TTrackMouseEvent;
  P: TPoint;
  Target: TControl;
begin
  case Message.Msg of
    CM_UNTHEMECONTROL:
      if (csDesigning in ComponentState) and ThemeServices.ThemesAvailable then
      begin
        SetWindowTheme(Handle, ' ', ' ');
        SetWindowPos(Handle, 0, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER or SWP_NOACTIVATE or SWP_SHOWWINDOW or SWP_FRAMECHANGED);
      end;
    CM_SETACTIVECONTROL:
      begin
        Form := GetParentForm(Self);
        if (Form <> nil) and (Form <> Self) then
          Form.Perform(CM_SETACTIVECONTROL, Message.WParam, Message.LParam);
      end;
    WM_SETFOCUS:
      begin
        Form := GetParentForm(Self);
        if (Form <> nil) and (not (csDesigning in Form.ComponentState) or (Form.Parent = nil)) then
          if not Form.SetFocusedControl(Self) then Exit;
      end;
    WM_KILLFOCUS:
      if csFocusing in ControlState then Exit;
    WM_NCHITTEST:
      begin
        inherited WndProc(Message);
        if (Message.Result = HTTRANSPARENT) and (ControlAtPos(ScreenToClient(
          SmallPointToPoint(TWMNCHitTest(Message).Pos)), False) <> nil) then
          Message.Result := HTCLIENT;
        Exit;
      end;
    WM_MOUSELEAVE:
      begin
        FMouseInClient := False;
        if FMouseControl <> nil then
          FMouseControl.Perform(CM_MOUSELEAVE, 0, 0)
        else
          Perform(CM_MOUSELEAVE, 0, 0);
        FMouseControl := nil;
      end;
    WM_MOUSEFIRST..WM_MOUSELAST:
      begin
        if Message.Msg = WM_MOUSEMOVE then
        begin
          P := ClientToScreen(Point(TWMMouse(Message).XPos, TWMMouse(Message).YPos));
          CaptureControl := GetCaptureControl;
          if CaptureControl = nil then
            Target := FindDragTarget(P, True)
          else
            Target := CaptureControl;
          if (FMouseControl <> Target) then
          begin
            if ((FMouseControl <> nil) and (CaptureControl = nil)) or
               ((CaptureControl <> nil) and (FMouseControl = CaptureControl)) or
               ((CaptureControl is TControl) and (CaptureControl.Parent = FMouseControl)) then
              FMouseControl.Perform(CM_MOUSELEAVE, 0, 0);
            if FMouseControl <> nil then
              FMouseControl.RemoveFreeNotification(Self);
            FMouseControl := Target;
            if FMouseControl <> nil then
              FMouseControl.FreeNotification(Self);
            if ((FMouseControl <> nil) and (CaptureControl = nil)) or
               ((CaptureControl <> nil) and (FMouseControl = CaptureControl)) then
              FMouseControl.Perform(CM_MOUSEENTER, 0, 0);
          end;
          if not FMouseInClient then
          begin
            FMouseInClient := True;
            // Register for a WM_MOUSELEAVE message which ensures CM_MOUSELEAVE
            // is called when the mouse leaves the TWinControl
            LMouseEvent.cbSize := SizeOf(LMouseEvent);
            LMouseEvent.dwFlags := TME_LEAVE;
            LMouseEvent.hwndTrack := Handle;
            LMouseEvent.dwHoverTime := HOVER_DEFAULT;
            _TrackMouseEvent(@LMouseEvent);
          end;
        end;
        if IsControlMouseMsg(TWMMouse(Message)) then
        begin
          { Check HandleAllocated because IsControlMouseMsg might have freed the
            window if user code executed something like Parent := nil. }
          if (Message.Result = 0) and HandleAllocated then
            DefWindowProc(Handle, Message.Msg, Message.wParam, Message.lParam);
          Exit;
        end;
      end;
    WM_MOUSEACTIVATE:
      if IsControlActivateMsg(TWMMouseActivate(Message)) then
      begin
        if (Message.Result = 0) and HandleAllocated then
          inherited WndProc(Message);
        Exit;
      end;
    WM_KEYFIRST..WM_KEYLAST:
      if Dragging then Exit;
    WM_CANCELMODE:
      if (GetCapture = Handle) and (CaptureControl <> nil) and
        (CaptureControl.Parent = Self) then
        CaptureControl.Perform(WM_CANCELMODE, 0, 0);
    CM_DESTROYHANDLE:
      begin
        if Boolean(Message.WParam) then // Sender has csRecreating set
          UpdateRecreatingFlag(True);
        try
          DestroyHandle;
        finally
          if Boolean(Message.WParam) then
            UpdateRecreatingFlag(False);
        end;
        Exit;
      end;
  end;
  inherited WndProc(Message);  if Message.Msg = WM_UPDATEUISTATE then
    Invalidate; // Ensure control is repainted
end;