4个回答
展开全部
一、详细用法:
【VB声明】
Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
【说明】
获得整个窗口的范围矩形,窗口的边框、标题栏、滚动条及菜单等都在这个矩形内
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【备注】
如将它与通过GetDesktopWindow获得的句柄联合使用,可获得对整个可视显示区域(桌面)进行说明的矩形
【参数表】
hwnd ----------- Long,想获得范围矩形的那个窗口的句柄
lpRect --------- RECT,屏幕坐标中随同窗口装载的矩形
二、实例
'This project needs
'a Form, called 'Form1'
'a Picture Box, called 'ExplButton' (50x50 pixels)
'a Picture Box with an icon in it, called 'picIcon'
'two timers (Timer1 and Timer2), both with interval 100
'Button, called 'Command1'
'In general section
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
'Declare the API-Functions
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Sub DrawButton(Pushed As Boolean)
Dim Clr1 As Long, Clr2 As Long
If Pushed = True Then
'If Pushed=True then clr1=Dark Gray
Clr1 = &H808080
'If Pushed=True then clr1=White
Clr2 = &HFFFFFF
ElseIf Pushed = False Then
'If Pushed=True then clr1=White
Clr1 = &HFFFFFF
'If Pushed=True then clr1=Dark Gray
Clr2 = &H808080
End If
With Form1.ExplButton
' Draw the button
Form1.ExplButton.Line (0, 0)-(.ScaleWidth, 0), Clr1
Form1.ExplButton.Line (0, 0)-(0, .ScaleHeight), Clr1
Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(.ScaleWidth - 1, 0), Clr2
Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(0, .ScaleHeight - 1), Clr2
End With
End Sub
Private Sub Command1_Click()
Dim Rec As RECT
'Get Left, Right, Top and Bottom of Form1
GetWindowRect Form1.hwnd, Rec
'Set Cursor position on X
SetCursorPos Rec.Right - 15, Rec.Top + 15
End Sub
Private Sub ExplButton_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton True
End Sub
Private Sub ExplButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton False
End Sub
Private Sub ExplButton_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton False
End Sub
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Stretched As Boolean
'picIcon.Visible = False
'API uses pixels
picIcon.ScaleMode = vbPixels
'No border
ExplButton.BorderStyle = 0
'API uses pixels
ExplButton.ScaleMode = vbPixels
'Set graphic mode te 'persistent graphic'
ExplButton.AutoRedraw = True
'API uses pixels
Me.ScaleMode = vbPixels
'Set the button's caption
Command1.Caption = "Set Mousecursor on X"
' If you set Stretched to true then stretch the icon to te Height and Width of the button
' If Stretched=False, the icon will be centered
Stretched = False
If Stretched = True Then
' Stretch the Icon
ExplButton.PaintPicture picIcon.Picture, 1, 1, ExplButton.ScaleWidth - 2, ExplButton.ScaleHeight - 2
ElseIf Stretched = False Then
' Center the picture of the icon
ExplButton.PaintPicture picIcon.Picture, (ExplButton.ScaleWidth - picIcon.ScaleWidth) / 2, (ExplButton.ScaleHeight - picIcon.ScaleHeight) / 2
End If
' Set icon as picture
ExplButton.Picture = ExplButton.Image
End Sub
Private Sub Timer1_Timer()
Dim Rec As RECT, Point As POINTAPI
' Get Left, Right, Top and Bottom of Form1
GetWindowRect Me.hwnd, Rec
' Get the position of the cursor
GetCursorPos Point
' If the cursor is located above the form then
If Point.X >= Rec.Left And Point.X <= Rec.Right And Point.Y >= Rec.Top And Point.Y <= Rec.Bottom Then
Me.Caption = "MouseCursor is on form."
Else
' The cursor is not located above the form
Me.Caption = "MouseCursor is not on form."
End If
End Sub
Private Sub Timer2_Timer()
Dim Rec As RECT, Point As POINTAPI
' Get Left, Right, Top and Bottom of ExplButton
GetWindowRect ExplButton.hwnd, Rec
' Get the position of the cursor
GetCursorPos Point
' If the cursor isn't located above ExplButton then
If Point.X < Rec.Left Or Point.X > Rec.Right Or Point.Y < Rec.Top Or Point.Y > Rec.Bottom Then ExplButton.Cls
End Sub
【VB声明】
Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
【说明】
获得整个窗口的范围矩形,窗口的边框、标题栏、滚动条及菜单等都在这个矩形内
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【备注】
如将它与通过GetDesktopWindow获得的句柄联合使用,可获得对整个可视显示区域(桌面)进行说明的矩形
【参数表】
hwnd ----------- Long,想获得范围矩形的那个窗口的句柄
lpRect --------- RECT,屏幕坐标中随同窗口装载的矩形
二、实例
'This project needs
'a Form, called 'Form1'
'a Picture Box, called 'ExplButton' (50x50 pixels)
'a Picture Box with an icon in it, called 'picIcon'
'two timers (Timer1 and Timer2), both with interval 100
'Button, called 'Command1'
'In general section
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
'Declare the API-Functions
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Sub DrawButton(Pushed As Boolean)
Dim Clr1 As Long, Clr2 As Long
If Pushed = True Then
'If Pushed=True then clr1=Dark Gray
Clr1 = &H808080
'If Pushed=True then clr1=White
Clr2 = &HFFFFFF
ElseIf Pushed = False Then
'If Pushed=True then clr1=White
Clr1 = &HFFFFFF
'If Pushed=True then clr1=Dark Gray
Clr2 = &H808080
End If
With Form1.ExplButton
' Draw the button
Form1.ExplButton.Line (0, 0)-(.ScaleWidth, 0), Clr1
Form1.ExplButton.Line (0, 0)-(0, .ScaleHeight), Clr1
Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(.ScaleWidth - 1, 0), Clr2
Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(0, .ScaleHeight - 1), Clr2
End With
End Sub
Private Sub Command1_Click()
Dim Rec As RECT
'Get Left, Right, Top and Bottom of Form1
GetWindowRect Form1.hwnd, Rec
'Set Cursor position on X
SetCursorPos Rec.Right - 15, Rec.Top + 15
End Sub
Private Sub ExplButton_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton True
End Sub
Private Sub ExplButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton False
End Sub
Private Sub ExplButton_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
DrawButton False
End Sub
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Stretched As Boolean
'picIcon.Visible = False
'API uses pixels
picIcon.ScaleMode = vbPixels
'No border
ExplButton.BorderStyle = 0
'API uses pixels
ExplButton.ScaleMode = vbPixels
'Set graphic mode te 'persistent graphic'
ExplButton.AutoRedraw = True
'API uses pixels
Me.ScaleMode = vbPixels
'Set the button's caption
Command1.Caption = "Set Mousecursor on X"
' If you set Stretched to true then stretch the icon to te Height and Width of the button
' If Stretched=False, the icon will be centered
Stretched = False
If Stretched = True Then
' Stretch the Icon
ExplButton.PaintPicture picIcon.Picture, 1, 1, ExplButton.ScaleWidth - 2, ExplButton.ScaleHeight - 2
ElseIf Stretched = False Then
' Center the picture of the icon
ExplButton.PaintPicture picIcon.Picture, (ExplButton.ScaleWidth - picIcon.ScaleWidth) / 2, (ExplButton.ScaleHeight - picIcon.ScaleHeight) / 2
End If
' Set icon as picture
ExplButton.Picture = ExplButton.Image
End Sub
Private Sub Timer1_Timer()
Dim Rec As RECT, Point As POINTAPI
' Get Left, Right, Top and Bottom of Form1
GetWindowRect Me.hwnd, Rec
' Get the position of the cursor
GetCursorPos Point
' If the cursor is located above the form then
If Point.X >= Rec.Left And Point.X <= Rec.Right And Point.Y >= Rec.Top And Point.Y <= Rec.Bottom Then
Me.Caption = "MouseCursor is on form."
Else
' The cursor is not located above the form
Me.Caption = "MouseCursor is not on form."
End If
End Sub
Private Sub Timer2_Timer()
Dim Rec As RECT, Point As POINTAPI
' Get Left, Right, Top and Bottom of ExplButton
GetWindowRect ExplButton.hwnd, Rec
' Get the position of the cursor
GetCursorPos Point
' If the cursor isn't located above ExplButton then
If Point.X < Rec.Left Or Point.X > Rec.Right Or Point.Y < Rec.Top Or Point.Y > Rec.Bottom Then ExplButton.Cls
End Sub
展开全部
函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。 函数原型:BOOL GetWindowRect(HWND hWnd,LPRECT lpRect); 在Visual Studio 2005中,函数原型为void GetWindowRect(LPRECT lpRect) const; 是属于CWnd类的函数. 参数: hWnd:窗口句柄。 lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。 变量: bottom ;left ;right; top; 分别表示该窗口的底部/左侧右侧/顶部坐标 返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。 速查:Windows NT:3.1以上版本:Windows:95以上版本;Windows CE:1.0以上版本;头文件:Winuser.h;库文件:User32.lib。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。 函数原型:BOOL GetWindowRect(HWND hWnd,LPRECT lpRect); 在Visual Studio 2005中,函数原型为void GetWindowRect(LPRECT lpRect) const; 是属于CWnd类的函数. 参数: hWnd:窗口句柄。 lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。 变量: bottom ;left ;right; top; 分别表示该窗口的底部/左侧右侧/顶部坐标 返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。 速查:Windows NT:3.1以上版本:Windows:95以上版本;Windows CE:1.0以上版本;头文件:Winuser.h;库文件:User32.lib。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
参数:
hWnd:窗口句柄。
lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。
同时其他语言也对它做了重载,void GetWindowRect(LPRECT lpRect) const,返回的是调用者的窗口对象相对屏幕的大小和位置。对数都是结构体指针,在函数中用得到的变量,修改了结构体内容,相当于用函数对参数赋值。
你只要记住,它把一个窗口的四边形赋给一个四边形结构体变量就行了。
事实上我说的结构体是类。你也可以理解为c语言中的结构体,性质差不多
函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
参数:
hWnd:窗口句柄。
lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。
同时其他语言也对它做了重载,void GetWindowRect(LPRECT lpRect) const,返回的是调用者的窗口对象相对屏幕的大小和位置。对数都是结构体指针,在函数中用得到的变量,修改了结构体内容,相当于用函数对参数赋值。
你只要记住,它把一个窗口的四边形赋给一个四边形结构体变量就行了。
事实上我说的结构体是类。你也可以理解为c语言中的结构体,性质差不多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询