使用API可以获取鼠标在屏幕的坐标点值,以下代码是累计鼠标在屏幕行程的代码,你可修改来满足你的要求: Option Explicit Private Type PointAPI X As Long Y As Long End Type Dim MousePos As PointAPI Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long Dim OldX As Long Dim OldY As Long Dim NewX As Long Dim NewY As Long Dim Distance As Double Dim Unit As Integer Dim UnitValue As Long Dim UnitName As String Dim FormatStr As String Const FormatStr1 = "000000.00" Const FormatStr2 = "0000.0000"Private Sub Command1_Click() End End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Command1.FontBold = True End SubPrivate Sub Form_Load() UnitValue = 1440 UnitName = "英寸" FormatStr = FormatStr1 Timer1.Enabled = True GetCursorPos MousePos OldX = MousePos.X * Screen.TwipsPerPixelX OldY = MousePos.Y * Screen.TwipsPerPixelY End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Command1.FontBold = False End SubPrivate Sub OptUnits_Click(Index As Integer) Dim i As Integer For i = 0 To 5 If OptUnits(i).Value = True Then Unit = i Next i Select Case Unit Case 0 UnitValue = 1440 UnitName = "英寸" FormatStr = FormatStr1 Case 1 UnitValue = 567 UnitName = "厘米" FormatStr = FormatStr1 Case 2 UnitValue = 14400 UnitName = "英尺" FormatStr = FormatStr1 Case 3 UnitValue = 56700 UnitName = "米" FormatStr = FormatStr1 Case 4 UnitValue = 144000000 UnitName = "英里" FormatStr = FormatStr2 Case 5 UnitValue = 56700000 UnitName = "公里" FormatStr = FormatStr2 End Select End SubPrivate Sub Timer1_Timer() Label1.Caption = Format(Distance / UnitValue, FormatStr) & UnitName GetCursorPos MousePos NewX = MousePos.X * Screen.TwipsPerPixelX NewY = MousePos.Y * Screen.TwipsPerPixelY Distance = Distance + Sqr((NewX - OldX) * (NewX - OldX) + _ (NewY - OldY) * (NewY - OldY)) OldX = NewX OldY = NewY End Sub
见8楼的回复! LZ:你的问帖是问测量屏幕任意两点间的距离,所以给你1楼的代码. 如果是你在9楼的图,就使用: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Debug.Print X Debug.Print Y End Sub原点在窗体的最左,最上处(窗体标题栏不在内)
1)建议安装MSDN,从中获取帮助,或者买本VB6的参考书来逐步提高. 2)在给一次代码: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Open "c:\sj.txt" For Append As #1 Print #1, X, Y Close End Sub
距离=sqr((xb-xa)^2+(yb-ya)^2)
使用API可以获取鼠标在屏幕的坐标点值,以下代码是累计鼠标在屏幕行程的代码,你可修改来满足你的要求:
Option Explicit
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI
Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Dim OldX As Long
Dim OldY As Long
Dim NewX As Long
Dim NewY As Long
Dim Distance As Double
Dim Unit As Integer
Dim UnitValue As Long
Dim UnitName As String
Dim FormatStr As String
Const FormatStr1 = "000000.00"
Const FormatStr2 = "0000.0000"Private Sub Command1_Click()
End
End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.FontBold = True
End SubPrivate Sub Form_Load()
UnitValue = 1440
UnitName = "英寸"
FormatStr = FormatStr1
Timer1.Enabled = True
GetCursorPos MousePos
OldX = MousePos.X * Screen.TwipsPerPixelX
OldY = MousePos.Y * Screen.TwipsPerPixelY
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.FontBold = False
End SubPrivate Sub OptUnits_Click(Index As Integer)
Dim i As Integer
For i = 0 To 5
If OptUnits(i).Value = True Then Unit = i
Next i
Select Case Unit
Case 0
UnitValue = 1440
UnitName = "英寸"
FormatStr = FormatStr1
Case 1
UnitValue = 567
UnitName = "厘米"
FormatStr = FormatStr1
Case 2
UnitValue = 14400
UnitName = "英尺"
FormatStr = FormatStr1
Case 3
UnitValue = 56700
UnitName = "米"
FormatStr = FormatStr1
Case 4
UnitValue = 144000000
UnitName = "英里"
FormatStr = FormatStr2
Case 5
UnitValue = 56700000
UnitName = "公里"
FormatStr = FormatStr2
End Select
End SubPrivate Sub Timer1_Timer()
Label1.Caption = Format(Distance / UnitValue, FormatStr) & UnitName
GetCursorPos MousePos
NewX = MousePos.X * Screen.TwipsPerPixelX
NewY = MousePos.Y * Screen.TwipsPerPixelY
Distance = Distance + Sqr((NewX - OldX) * (NewX - OldX) + _
(NewY - OldY) * (NewY - OldY))
OldX = NewX
OldY = NewY
End Sub
平面两点距离计算只需要两个点的坐标就能计算了, 不需要知道原点在哪里.但是计算机图像默认屏幕处于平面坐标系第四象限, 即以屏幕左上角为原点. 即使你更改了坐标原点, 那么几乎你所有用到的关于图像输入输出的API坐标你统统都要换算成你的自定义坐标系. 实在看不出这样做有什么好处, 你简直是在和整个系统作对.
见8楼的回复!
LZ:你的问帖是问测量屏幕任意两点间的距离,所以给你1楼的代码.
如果是你在9楼的图,就使用:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print X
Debug.Print Y
End Sub原点在窗体的最左,最上处(窗体标题栏不在内)
坐标系统概述
本章描述的每一个图形操作(包括调整大小、移动和绘图),都要使用绘图区或容器的坐标系统。虽然用坐标系统能达到成功的图形效果,然而,如何用坐标系统定义窗体和控件在应用程序中的位置,也是很重要的。
坐标系统是一个二维网格,可定义屏幕上、窗体中或其它容器中(如:图片框或 Printer 对象)的位置。使用窗体中的坐标,可定义网格上的位置:
(x, y)
x 值是沿 x 轴点的位置,最左端是缺省位置 0。y 值是沿 y 轴点的位置,最上端是缺省位置 0。以下规则用于 Visual Basic 坐标系统: 当移动控件或调整控件的大小时,使用控件容器的坐标系统。如果直接在窗体上绘制对象时,窗体就是容器。如果在框架或图片框里绘制控件时,框架或控件是容器。
所有的图形和 Print 方法,使用容器的坐标系统。例如,那些在图片框里绘制控件的语句,使用的是控件的坐标系统。
一些用来调整窗体大小或移动窗体的语句,用缇来表示窗体的位置和大小。
当创建用来调整窗体大小或移动窗体的代码时,应先检查 Screen 对象的 Height 属性和 Width 属性,以确保窗体在屏幕上大小合适。屏幕的左上角总是(0,0)。任何容器的缺省坐标系统,都是由容器的左上角(0,0)坐标开始。
沿这些坐标轴定义位置的测量单位,统称为刻度。在 Visual Basic 中,坐标系统的每个轴都有自己的刻度。坐标轴的方向、起点和坐标系统的刻度,都是可以改变的,但是,对现在来说,使用的是缺省系统。本章后面的“改变对象的坐标系统”中将讨论如何改动。缇的解释
所有 Visual Basic 的移动、调整大小和图形绘制语句,根据缺省规定,使用缇为单位。缇是打印机的一磅的 1/20(1,440 缇等于一英寸;567 缇等于一厘米)。这些测量值指示对象打印后的大小。屏幕上的物理实际距离根据监视器的大小变化。“改变对象的坐标系统”中描述了如何选择缇以外的单位。
┌───→
│
│
│
↓可以用Scale (x1, y1) - (x2, y2)重新定义坐标系。
LZ:你还需加强语文学习,我在17楼的回复说得非常明白,你用1楼代码(API),原点在屏幕左上角.
你用15楼的代码,原点在窗体上红色点处.
Debug.Print X
Debug.Print Y
End Sub
Text1 = "12345678"
End Sub
2)在给一次代码:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Open "c:\sj.txt" For Append As #1
Print #1, X, Y
Close
End Sub