vb.netrect的简单介绍

vb.net中GetClientRect()函数的问题对不起vb.netrect,vb.netrect我没有学过vb.netvb.netrect , 但是学过vb,希望这个可以
解决您程序的毛病 。首先您注意以下两种GetClientRect
声明的方法vb.netrect:
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类型
(结构体)的指针,然后传递 。
希望能对你有所帮助 。
VB.NET我要用鼠标轨迹画一个矩形框 然后选中控件 。就像星际和魔兽争霸里对部队单位的选中一样~等大神回答这个类继承自Panelvb.netrect , 把它加到vb.netrect你vb.netrect的项目里面vb.netrect,先运行一下,然后从工具箱里把它拖到窗体上,然后再向里面添加其它控件就可以vb.netrect了,支持Shift加选 , Alt减选
Imports System.Linq
Imports System.Collections
Public Class MyPanel
Inherits Panel
' 选择模式 , 相交还是包含
Enum SelectMode
Intersects
Contains
End Enum
Dim down As New Point(-1, -1)
Dim rect As Rectangle
Dim selected As New List(Of Control)
Dim editting As IEnumerable(Of Control)
Dim mode As SelectMode = SelectMode.Contains
Dim shift, alt As Boolean
Public Sub New()
Me.DoubleBuffered = True
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
down = e.Location
editting = selected.ToArray().ToList()
OnMouseMove(e)
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim loc As New Point(Math.Min(down.X, e.X), Math.Min(down.Y, e.Y))
Dim size As New Size(Math.Abs(down.X - e.X), Math.Abs(down.Y - e.Y))
rect = New Rectangle(loc, size)
Dim cs As New List(Of Control)
For Each c In Controls
cs.Add(c)
Next
Dim a = cs.Where(Function(n As Control) (mode = SelectMode.Contains And rect.Contains(n.Bounds)) Or (mode = SelectMode.Intersects And rect.IntersectsWith(n.Bounds)))
If shift Then editting = a.Union(selected) Else If alt Then editting = selected.Except(a) Else editting = a
Invalidate()
End If
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
MyBase.OnMouseUp(e)
down = New Point(-1, -1)
selected = editting.ToList()
【vb.netrect的简单介绍】editting = Nothing
Invalidate()
End Sub
Protected Overrides Function ProcessKeyPreview(ByRef m As Message) As Boolean
Dim KeyCode As Keys = CInt(m.WParam) And CInt(Keys.KeyCode)

推荐阅读