讨论一下control.PointToClient的性能问题好吗?
pslh 2006-04-09 02:03:43 用.net reflector工具查看了一下control.PointToClient,发现:
Public Function PointToClient(ByVal p As Point) As Point
Return Me.PointToClientInternal(p)
End Function
再查Me.PointToClientInternal :
Friend Function PointToClientInternal(ByVal p As Point) As Point
Dim point1 As New POINT(p.X, p.Y)
UnsafeNativeMethods.MapWindowPoints(NativeMethods.NullHandleRef, New HandleRef(Me, Me.Handle), point1, 1)
Return New Point(point1.x, point1.y)
End Function
此时,发现一个问题,在PointToClientInternal过程中:第一行定义的point1 其实就是参数p ,第三行返回的值其实也是参数p (只不过值改变了)。他们为什么要多New两次呢,这样做会不会影响性能呢?不如改为下面的好:
Friend Function PointToClientInternal(ByVal p As Point) As Point
UnsafeNativeMethods.MapWindowPoints(NativeMethods.NullHandleRef, New HandleRef(Me, Me.Handle), p, 1)
Return p
End Function
大家认为如何呢?
因为在我的程序中有一个Hook,用来监控鼠标的move,在每一次move中,都要进入一个循环,在循环中要反复判断control.PointToClient的值,所以对此比较关心一点,大家不要笑我。