如何获取鼠标相对于屏幕的以像素为单位的位置?

chuyangguangshin 2005-07-28 02:52:41
如题。
...全文
259 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuyangguangshin 2005-07-28
  • 打赏
  • 举报
回复
谢谢chendazhi(不务正业-何苦),但是Cursor.Position.X 返回的是像素! 并不是毫米!
泡沫游走 2005-07-28
  • 打赏
  • 举报
回复
上面得到的单位是毫米
1英寸=25.4毫米
像素的多少取决于显示器和分辨率设定
一般而言是96dpi(每英寸96个点)

知道怎么算了!
泡沫游走 2005-07-28
  • 打赏
  • 举报
回复
鼠标相对于屏幕的位置
'1.获取鼠标的坐标 Timer1.Interval = 18
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString & "," & System.Windows.Forms.Cursor.Position.Y.ToString
TextBox3.Text = Me.MousePosition.X & "," & Me.MousePosition.Y

End Sub

'2.用API获取鼠标的坐标:///////////////
Public Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As pointapi) As Integer

Public Structure pointapi
Dim x As Integer
Dim y As Integer
End Structure

Private Sub NotifyIcon1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDown
If e.Button = MouseButtons.Right Then
Dim aa As New pointapi
If GetCursorPos(aa) = 1 Then
' TextBox1.Text = aa.x

Dim f2 As New Form2
f2.StartPosition = FormStartPosition.Manual
f2.Location = New Point(aa.x - f2.Width, aa.y - f2.Height)
'f2.Left = aa.x - f2.Width
'f2.Top = aa.y - f2.Height
f2.TopMost = True
f2.Show()
End If
End If

End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim p As New pointapi
If GetCursorPos(p) = 1 Then
TextBox4.Text = p.x & "," & p.y
End If
End Sub
'3.直接获取
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox3.Text = Me.MousePosition.X & "," & Me.MousePosition.Y
End Sub

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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