[API]
POINTAPI
ICONINFO
GetCursorPos
GetIconInfo
DeleteObject

[INCLUDE]
LoadCursor
GetOptMonitorFromPoint

[CODE]
Public Sub WindowToCursorPos(ByRef xWindowSize As POINTAPI, ByRef xType1 As POINTAPI)
  Dim xLong1 As Long
  Dim xCursorPlusX As Long
  Dim xCursorPlusX2 As Long
  Dim xCursorPlusY As Long
  Dim xCursorPlusY2 As Long
  Dim xType2 As RECT
  #If GetOptCursorHotSpotOffset Then
    #Else
    Dim xType3 As ICONINFO
    #End If
  
  Call GetCursorPos(xType1)
  Call GetOptMonitorFromPoint(xType1.x, xType1.y, xType2)

  #If GetOptCursorHotSpotOffset Then
    Call GetOptCursorHotSpotOffset(xCursorPlusX, xCursorPlusY, xCursorPlusX2, xCursorPlusY2)
    #Else
    xLong1 = LoadCursor(&H7F00&)
    Call GetIconInfo(xLong1, xType3)
    If Not xType3.hbmMask = 0& Then Call DeleteObject(xType3.hbmMask)
    If Not xType3.hbmColor = 0& Then Call DeleteObject(xType3.hbmColor)
    If xType3.xHotspot > 16& Then
      xCursorPlusX = 36& - xType3.xHotspot
      xCursorPlusX2 = 16&
      Else
      xCursorPlusX = 16&
      xCursorPlusX2 = 4& + xType3.xHotspot
      End If
    If xType3.yHotspot > 16& Then
      xCursorPlusY = 36& - xType3.yHotspot
      xCursorPlusY2 = 16&
      Else
      xCursorPlusY = 16&
      xCursorPlusY2 = 4& + xType3.yHotspot
      End If
    #End If
    
  If xType1.x + xWindowSize.x + xCursorPlusX > xType2.Right Then
    xType1.x = xType1.x - xWindowSize.x - xCursorPlusX2
    'If xType1.x + xWindowSize.x + xCursorPlusX > xType2.Right Then
    If xType1.x + xWindowSize.x > xType2.Right Then
      xType1.x = xType2.Right - xWindowSize.x
      End If
    Else
    xType1.x = xType1.x + xCursorPlusX
    End If
    
  If xType1.y + xWindowSize.y + xCursorPlusY > xType2.Bottom Then
    xType1.y = xType1.y - xWindowSize.y - xCursorPlusY2
    If xType1.y + xWindowSize.y + xCursorPlusY > xType2.Bottom Then
      xType1.y = xType2.Bottom - xWindowSize.y
      End If
    Else
    xType1.y = xType1.y + xCursorPlusY
    End If
  If xType1.x < xType2.Left Then xType1.x = xType2.Left
  If xType1.y < xType2.Top Then xType1.y = xType2.Top
  'Debug.Print xType1.x, xType1.y
  End Sub
