vb 调用 MoveToEx 的 bug
MoveToEx 这个函数竟然会修改参数的值,百思不得其解代码如下
程序代码:
Option Explicit Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Long) As Long Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Sub Command1_Click() Dim drawhdc As Long Dim ret As Long drawhdc = Me.hdc Debug.Print drawhdc Debug.Print MoveToEx(Me.hdc, 10, 10, 0&) Debug.Print LineTo(Me.hdc, 100, 10) Debug.Print MoveToEx(drawhdc, 10, 10, 0&) Debug.Print drawhdc Debug.Print LineTo(drawhdc, 10, 100) Me.Refresh End Sub
打印的结果:
1593906163
1
1
1
10
0
可以看到drawhdc已经从 1593906163被修改为10,导致最后一次LineTo函数的调用失败。
如果 一直用Me.hdc做参数的话,有时会导致vb的崩溃,无法捕捉任何错误信息。
更加奇怪的事情是,如果在Dim drawhdc As Long 后面再声明一个变量Dim ret As Long,那就完全正确了。
崩溃啊