MouseMove事件为何这么[迟钝]?
一,为什么没有DrawPoint方法之所以提出这个问题,是因为我要用,由于找不到,只好用矩形代替了
如果要画函数曲线,怎么办呢?
二,在窗体的MouseMove事件中写如下代码:
if(e.Button==MouseButtons.Left)
CreateGraphics().DrawRectangle(Pens.Blue,e.X,e.Y,1,1);//不知怎么画点,只好画小矩形了
运行后会发现,如果鼠标移动稍快一点,就会出现一些离散的点(间距真是不小)
而我们用Windows自带的画图程序,至少从视觉上看起来是连续的
我想了一个办法来画连续的线:
int x;
int y;
在MouseDown事件中写:
x=e.X;
y=e.Y;
在MouseMove事件中写:
if(e.Button==MouseButtons.Left)
CreateGraphics().DrawLine(Pens.Blue,x,y,e.X,e.Y);
x=e.X;
y=e.Y;
虽然想到解决办法,还是想问一下,为何MouseMove反应这么迟钝?
(其实我还有办法证明其迟钝:在窗体上添加一个图片框和两个标签(这里用图片框的MouseMove事件,标签用于显示e.X和e.Y)
当鼠标快速(也不用太快,因为确实很迟钝)移出图片框时,本来e.X或e.Y中应该有一个是边界值,但事实并非如此,因为反应迟钝
--------------------编程问答-------------------- 去Www.codeproject.com里面,有源代码 --------------------编程问答-------------------- Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
'Namespace myPaint
Inherits System.Windows.Forms.Form ' Of course ;)
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
'*** below I declare some values for an Alpha and other user selected variables
'these will be used as I expand this program for a higher level use.
Dim myAlpha As Integer = 100 ' declare a Alpha variable
Dim myUserColor As New Color() 'this is a color the user selects
Dim myPenWidth As Single = 5 'set pen width variable
'**************************************************************
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(104, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(472, 32)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Below is a Graphics path freehand drawing space"
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.Ivory
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(112, 96)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(448, 272)
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.BackColor = System.Drawing.Color.BlanchedAlmond
Me.ClientSize = New System.Drawing.Size(696, 456)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, Me.Label1})
Me.Name = "Form1"
Me.Text = "Johns' Free Hand Doodle"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then ' draw a filled circle if left mouse is down
mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then ' draw a filled circle if left mouse is down
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
Catch
MsgBox("No way, Hose!")
End Try
End If
PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
' Here is where we do the actual painting
Try ' error trapping
myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
'change the value of myUserColor
myAlpha = 100 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
'*********************** NOTE ***********************************************
'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
' A 易做图r, but less flexible solution would be to replace the line with the following code:
'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
'************ End Note ***************************
Dim CurrentPen As Pen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :)
Catch
' MsgBox("Not happening!")
End Try
End Sub
'End Namespace
End Class 'Form1
这是VB代码,去这个网站找Freehand Drawing,没有C#源代码,原理一样 --------------------编程问答-------------------- MouseMove反应不是迟钝——是你在这个事件里面做的事情太多或者太慢,所以肯定会影响到它的精度 --------------------编程问答-------------------- 自己机器配置不行
补充:.NET技术 , C#