vb.net中GetClientRect()函数的问题
如题,其中有一个参数RECT,无论自己写结构体还是用.net自带的Rectangle都不行,难道真的是vb.net对API的支持不好吗?提示这个错误:“尝试读取或写入受保...
如题,其中有一个参数RECT,无论自己写结构体还是用.net自带的Rectangle都不行,难道真的是vb.net对API的支持不好吗?提示这个错误:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
希望有高手能给我解答,最好有实例。不要从网上沾vb6的来!
而且vb.net真的用API不好吗? 展开
希望有高手能给我解答,最好有实例。不要从网上沾vb6的来!
而且vb.net真的用API不好吗? 展开
展开全部
对不起,我没有学过vb.net,但是学过vb,希望这个可以
解决您程序的毛病。首先您注意以下两种GetClientRect
声明的方法:
Option Explicit
Private Declare Function GetClientRect Lib "user32" ( _
ByVal hwnd As Long, _
ByRef lpRect As RECT _
) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim r As RECT
Me.AutoRedraw = True
GetClientRect Me.hwnd, r
Print r.Left
Print r.Right
Print r.Top
Print r.Bottom
End Sub
----------------------------------------------------------------------------
Option Explicit
Private Declare Function GetClientRect Lib "user32" ( _
ByVal hwnd As Long, _
ByVal lpRect As Long _
) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim r As RECT
Me.AutoRedraw = True
GetClientRect Me.hwnd, VarPtr(r)
Print r.Left
Print r.Right
Print r.Top
Print r.Bottom
End Sub
看出问题了没有,就在GetClientRect的第二个参数上:
一个是按地址传递,另一个是按值传递:
ByRef lpRect As RECT 用 GetClientRect Me.hwnd, r
ByVal lpRect As Long 用 GetClientRect Me.hwnd, VarPtr(r)
据我所知vb.net按值传递的比较多,应该用VarPtr获取RECT类型
(结构体)的指针,然后传递。
希望能对你有所帮助。
解决您程序的毛病。首先您注意以下两种GetClientRect
声明的方法:
Option Explicit
Private Declare Function GetClientRect Lib "user32" ( _
ByVal hwnd As Long, _
ByRef lpRect As RECT _
) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim r As RECT
Me.AutoRedraw = True
GetClientRect Me.hwnd, r
Print r.Left
Print r.Right
Print r.Top
Print r.Bottom
End Sub
----------------------------------------------------------------------------
Option Explicit
Private Declare Function GetClientRect Lib "user32" ( _
ByVal hwnd As Long, _
ByVal lpRect As Long _
) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim r As RECT
Me.AutoRedraw = True
GetClientRect Me.hwnd, VarPtr(r)
Print r.Left
Print r.Right
Print r.Top
Print r.Bottom
End Sub
看出问题了没有,就在GetClientRect的第二个参数上:
一个是按地址传递,另一个是按值传递:
ByRef lpRect As RECT 用 GetClientRect Me.hwnd, r
ByVal lpRect As Long 用 GetClientRect Me.hwnd, VarPtr(r)
据我所知vb.net按值传递的比较多,应该用VarPtr获取RECT类型
(结构体)的指针,然后传递。
希望能对你有所帮助。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询