坐标转换的问题

Mclaren 2003-10-18 07:10:04
我想在form上绘出鼠标的轨迹
于是先用getcursorpos得到当前坐标,然后用pset绘图
但getcursorpos返回的是屏幕坐标,pset用的是form的坐标
怎样才能转换呢?

xiele
...全文
39 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ringfo 2003-10-18
  • 打赏
  • 举报
回复
赞同:在窗体的MouseMove中实现比较好
rainstormmaster 2003-10-18
  • 打赏
  • 举报
回复
用DrawIconEx绘制
goodname008 2003-10-18
  • 打赏
  • 举报
回复
' 我觉得在窗体的MouseMove中实现比较好.


Option Explicit

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print X, Y
Debug.Print X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY
End Sub
rainstormmaster 2003-10-18
  • 打赏
  • 举报
回复
api函数ScreenToClient和ClientToScreen负责客户区坐标和屏幕坐标相互转换,下面是一个例子:
'窗体上两个按钮
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Dim P As POINTAPI
Private Sub Form_Load()

Command1.Caption = "Screen Middle"
Command2.Caption = "Form Middle"
'API uses pixels
Me.ScaleMode = vbPixels
End Sub
Private Sub Command1_Click()
'Get information about the screen's width
P.x = GetDeviceCaps(Form1.hdc, 8) / 2
'Get information about the screen's height
P.y = GetDeviceCaps(Form1.hdc, 10) / 2
'Set the mouse cursor to the middle of the screen
ret& = SetCursorPos(P.x, P.y)
End Sub
Private Sub Command2_Click()
P.x = 0
P.y = 0
'Get information about the form's left and top
ret& = ClientToScreen&(Form1.hwnd, P)
P.x = P.x + Me.ScaleWidth / 2
P.y = P.y + Me.ScaleHeight / 2
'Set the cursor to the middle of the form
ret& = SetCursorPos&(P.x, P.y)
End Sub

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧