在VB或C++中,如何记录鼠标的移动轨迹?
把屏幕的下边作为横坐标,左边作为竖坐标,左下角作为原点,每1秒记录鼠标的位置及所有的单击、双击操作。是不是很难啊?...
把屏幕的下边作为横坐标,左边作为竖坐标,左下角作为原点,每1秒记录鼠标的位置及所有 的单击、双击操作。
是不是很难啊? 展开
是不是很难啊? 展开
3个回答
2008-01-31
展开全部
VB绘制鼠标移动轨迹
主要代码及注释如下:
Public Class Form1Class Form1
Dim PtStart As Point '记录绘制直线的起始点
Dim PtEnd As Point '记录绘制直线的终点
Dim ShouldDrawLine As Boolean '是否绘制直线
'记录鼠标左键点击的位置,第二次点击后开始绘制直线
Private Sub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If Not ShouldDrawLine Then
PtStart = New Point(e.X, e.Y)
ShouldDrawLine = True
Else
PtEnd = New Point(e.X, e.Y)
'下面两句根据需要进行取舍
'Call DrawLine(PtStart, PtEnd) '绘制一条直线
Call DrawLines(PtStart, PtEnd) '绘制多条直线
ShouldDrawLine = False
End If
End If
End Sub
'绘制鼠标的移动轨迹(仅在鼠标第一次点击后开始绘制轨迹)
Private Sub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
Static pt As Point
If ShouldDrawLine Then
'鼠标第一次点击的位置(需转化为屏幕坐标)
Dim p As Point = Pic1.PointToScreen(PtStart)
'清除原先绘制的鼠标移动轨迹
If pt <> Nothing Then ControlPaint.DrawReversibleLine(p, pt, Color.Red)
'绘制鼠标移动后新的轨迹
pt = Pic1.PointToScreen(New Point(e.X, e.Y))
ControlPaint.DrawReversibleLine(p, pt, Color.Red)
Else
pt = Nothing '清除鼠标原位置
End If
End Sub
'绘制鼠标两次点击位置之间的直线
Private Sub DrawLine()Sub DrawLine(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
Pic1.Refresh() '用于刷新Picturebox表面
Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) '绘制两点间的直线
End Sub
'绘制多条直线,每两次鼠标点击确定一条线
Private Sub DrawLines()Sub DrawLines(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
'此句不可删除,用于清除鼠标点击前的轨迹
ControlPaint.DrawReversibleLine(Pic1.PointToScreen(mPoint1), Pic1.PointToScreen(mPoint2), Color.Red)
Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) '绘制两点间的直线
End Sub
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
End Class
注:此代码运行需要一个Form,Form上存在一个Pic1的Picturebox控件。
举一反三,附另外一段代码:
如何在窗体上随鼠标的移动快速绘制十字形轨迹源码
Public Class Form2Class Form2
Dim OldPoint As Point
Private Sub Form2_Load()Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
Private Sub Form1_MouseMove()Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Dim p1, p2, p3, p4 As Point
If OldPoint <> Nothing Then
p1 = PointToScreen(New Point(OldPoint.X, 0))
p2 = PointToScreen(New Point(OldPoint.X, Me.ClientSize.Height))
p3 = PointToScreen(New Point(0, OldPoint.Y))
p4 = PointToScreen(New Point(Me.ClientSize.Width, OldPoint.Y))
ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)
End If
p1 = PointToScreen(New Point(e.X, 0))
p2 = PointToScreen(New Point(e.X, Me.ClientSize.Height))
p3 = PointToScreen(New Point(0, e.Y))
p4 = PointToScreen(New Point(Me.ClientSize.Width, e.Y))
ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)
OldPoint = New Point(e.X, e.Y)
End Sub
End Class
再附一段代码,在picturebox上实时绘制鼠标选中的矩形框
Public Class Form2Class Form2
Dim PtStart, pt As Point
Dim RectSize As Size
Private Sub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y)
End Sub
Private Sub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
pt = PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
End If
End Sub
Private Sub DrawRect()Sub DrawRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
Pic1.Refresh()
If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y)
End If
Pic1.CreateGraphics.DrawRectangle(Pens.Blue, New Rectangle(PtStart, RectSize))
MsgBox(String.Format("左上角坐标{0}" + vbCrLf + "矩形大小{1}", PtStart.ToString, RectSize.ToString), MsgBoxStyle.Information, "Info")
End Sub
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
Private Sub Pic1_MouseUp()Sub Pic1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
Call DrawRect(PtStart, New Point(e.X, e.Y))
pt = Nothing
End If
End Sub
End Class
主要代码及注释如下:
Public Class Form1Class Form1
Dim PtStart As Point '记录绘制直线的起始点
Dim PtEnd As Point '记录绘制直线的终点
Dim ShouldDrawLine As Boolean '是否绘制直线
'记录鼠标左键点击的位置,第二次点击后开始绘制直线
Private Sub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If Not ShouldDrawLine Then
PtStart = New Point(e.X, e.Y)
ShouldDrawLine = True
Else
PtEnd = New Point(e.X, e.Y)
'下面两句根据需要进行取舍
'Call DrawLine(PtStart, PtEnd) '绘制一条直线
Call DrawLines(PtStart, PtEnd) '绘制多条直线
ShouldDrawLine = False
End If
End If
End Sub
'绘制鼠标的移动轨迹(仅在鼠标第一次点击后开始绘制轨迹)
Private Sub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
Static pt As Point
If ShouldDrawLine Then
'鼠标第一次点击的位置(需转化为屏幕坐标)
Dim p As Point = Pic1.PointToScreen(PtStart)
'清除原先绘制的鼠标移动轨迹
If pt <> Nothing Then ControlPaint.DrawReversibleLine(p, pt, Color.Red)
'绘制鼠标移动后新的轨迹
pt = Pic1.PointToScreen(New Point(e.X, e.Y))
ControlPaint.DrawReversibleLine(p, pt, Color.Red)
Else
pt = Nothing '清除鼠标原位置
End If
End Sub
'绘制鼠标两次点击位置之间的直线
Private Sub DrawLine()Sub DrawLine(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
Pic1.Refresh() '用于刷新Picturebox表面
Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) '绘制两点间的直线
End Sub
'绘制多条直线,每两次鼠标点击确定一条线
Private Sub DrawLines()Sub DrawLines(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
'此句不可删除,用于清除鼠标点击前的轨迹
ControlPaint.DrawReversibleLine(Pic1.PointToScreen(mPoint1), Pic1.PointToScreen(mPoint2), Color.Red)
Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) '绘制两点间的直线
End Sub
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
End Class
注:此代码运行需要一个Form,Form上存在一个Pic1的Picturebox控件。
举一反三,附另外一段代码:
如何在窗体上随鼠标的移动快速绘制十字形轨迹源码
Public Class Form2Class Form2
Dim OldPoint As Point
Private Sub Form2_Load()Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
Private Sub Form1_MouseMove()Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Dim p1, p2, p3, p4 As Point
If OldPoint <> Nothing Then
p1 = PointToScreen(New Point(OldPoint.X, 0))
p2 = PointToScreen(New Point(OldPoint.X, Me.ClientSize.Height))
p3 = PointToScreen(New Point(0, OldPoint.Y))
p4 = PointToScreen(New Point(Me.ClientSize.Width, OldPoint.Y))
ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)
End If
p1 = PointToScreen(New Point(e.X, 0))
p2 = PointToScreen(New Point(e.X, Me.ClientSize.Height))
p3 = PointToScreen(New Point(0, e.Y))
p4 = PointToScreen(New Point(Me.ClientSize.Width, e.Y))
ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)
OldPoint = New Point(e.X, e.Y)
End Sub
End Class
再附一段代码,在picturebox上实时绘制鼠标选中的矩形框
Public Class Form2Class Form2
Dim PtStart, pt As Point
Dim RectSize As Size
Private Sub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y)
End Sub
Private Sub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
pt = PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
End If
End Sub
Private Sub DrawRect()Sub DrawRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
Pic1.Refresh()
If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y)
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then
PtStart = New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y)
End If
Pic1.CreateGraphics.DrawRectangle(Pens.Blue, New Rectangle(PtStart, RectSize))
MsgBox(String.Format("左上角坐标{0}" + vbCrLf + "矩形大小{1}", PtStart.ToString, RectSize.ToString), MsgBoxStyle.Information, "Info")
End Sub
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
Private Sub Pic1_MouseUp()Sub Pic1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
Call DrawRect(PtStart, New Point(e.X, e.Y))
pt = Nothing
End If
End Sub
End Class
展开全部
#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="Mouse";
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
static int x;
static int y;
static BOOL bnowDraw=FALSE;
switch(iMessage) {
case WM_LBUTTONDOWN:
x=LOWORD(lParam);
y=HIWORD(lParam);
bnowDraw=TRUE;
return 0;
case WM_MOUSEMOVE:
if (bnowDraw==TRUE) {
hdc=GetDC(hWnd);
MoveToEx(hdc,x,y,NULL);
x=LOWORD(lParam);
y=HIWORD(lParam);
LineTo(hdc,x,y);
ReleaseDC(hWnd,hdc);
}
return 0;
case WM_LBUTTONUP:
bnowDraw=FALSE;
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="Mouse";
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
static int x;
static int y;
static BOOL bnowDraw=FALSE;
switch(iMessage) {
case WM_LBUTTONDOWN:
x=LOWORD(lParam);
y=HIWORD(lParam);
bnowDraw=TRUE;
return 0;
case WM_MOUSEMOVE:
if (bnowDraw==TRUE) {
hdc=GetDC(hWnd);
MoveToEx(hdc,x,y,NULL);
x=LOWORD(lParam);
y=HIWORD(lParam);
LineTo(hdc,x,y);
ReleaseDC(hWnd,hdc);
}
return 0;
case WM_LBUTTONUP:
bnowDraw=FALSE;
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用低级鼠标钩子可以做到,请查看MSDN中SetWindowsHookEx函数
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询