
正文
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类型
(结构体)的指针,然后传递。
希望能对你有所帮助。
相关问答
Q1: 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()
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)
Dim d As Boolean
If m.Msg = H100 Or m.Msg = H104 Then d = True Else If m.Msg = H101 Or m.Msg = H105 Then d = False Else Return MyBase.ProcessKeyPreview(m)
If KeyCode = Keys.ShiftKey Then
shift = d
ElseIf KeyCode = Keys.Menu Then
alt = d
End If
Return MyBase.ProcessKeyPreview(m)
End Function
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
For Each c As Control In IIf(editting Is Nothing, selected, editting)
e.Graphics.DrawRectangle(New Pen(Color.Gray) With {.DashStyle = Drawing2D.DashStyle.DashDot}, c.Left - 1, c.Top - 1, c.Width + 1, c.Height + 1)
Next
If (down.X 0) Then e.Graphics.DrawRectangle(New Pen(Color.Gray) With {.DashStyle = Drawing2D.DashStyle.DashDot}, rect)
End Sub
End Class
Q2: VB.net GetWindowRect 问题
把这个函数改成这样就好vb.netrect了Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer
Q3: vb.net,dim rects as rectangle()()={a,b,c,d,e,f} 我忘了双小括号的
Dim a As New System.Drawing.Rectangle(2,1,20,40)
Dim b As New System.Drawing.Rectangle(4,4,40,30)
Dim c As New System.Drawing.Rectangle(6,7,60,20)
Dim d As New System.Drawing.Rectangle(8,10,80,10)
Dim rectabcd = {a, b, c, d} ''数组(a、b、c、d)
Public Sub 调用组()
rectabcd.GetValue(2) ''获取rectabcd{c}返回值 Object。
……ListRect.Item(2).Height
rectabcd.ToList.Item(0) ''获取或设置rectabcd.ToList[a]的值。
End Sub
Public ListRect As List(Of Drawing.Rectangle) = {New Rectangle(0, 0, 10, 10), New Rectangle(10, 1, 20, 20)}.ToList ''ListRect共2个Rectangle。
Q4: VB.NET想用GetWindowRECT获取某窗口的坐标,但测试结果是L,T,R,B显示都是0,0,0,0
很简单,原因有二。第一,VB里long是32位,但是VB.NET里是64位,Dim ksWND As Integer才对。第二,VB在API里默认传址,而VB.NET默认传值,所以API里要添加Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,添加"ByRef" lpRect As RECT) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,byref lpRect As RECT) As Integer
Private Structure RECT
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer
End Structure
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ksWND As integer
Dim ksTitle As String
Dim winS As RECT
If TextBox1.Text "" Then
ksTitle = TextBox1.Text
ksWND = FindWindow(vbNullString, ksTitle)
GetWindowRect(ksWND, winS)
MsgBox("左上角坐标(" winS.Left "," winS.Top ")" vbCrLf "右下角坐标(" winS.Right "," winS.Bottom ")" vbCrLf "窗口高" winS.Bottom - winS.Top "窗口宽" winS.Right - winS.Left)
Else
MsgBox("请填写窗口名称")
End If
End Sub
关于vb.netrect和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







