现在需要在PictureBox中画一条直线,并且为这条直线添加单击,双击等消息。
-
我想用控件代替,可是控件无法实现任意角度的旋转。
------解决方案--------------------
放个panel 高度设为1 加上边框 也是条线
------解决方案--------------------
- VB.NET code
Public Class Form1 Dim path, path1 As New System.Drawing.Drawing2D.GraphicsPath Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p0 As New Point(50, 50) Dim p1 As New Point(200, 200) Dim p2 As New Point(200, 201) Dim p3 As New Point(50, 51) path1.AddLines(New Point() {p0, p1, p2, p3, p0}) path.AddLine(p0, p1) End Sub '根据鼠标的位置取一个5X5的正方形 Function getRoundRec(ByVal p As Point) As Rectangle Return New Rectangle(p.X, p.Y, 5, 5) End Function Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick If New Region(path1).IsVisible(getRoundRec(e.Location)) Then MsgBox("the click event ") End If End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.DrawPath(Pens.Black, path) End Sub
------解决方案--------------------
画一条线,
根据鼠标位置查看图片框中的颜色,如果是线则鼠标指到了线然后编写事件。
------解决方案--------------------
思路:
1、画一条线,并记录这条线的两个点(点N,点M)
2、当点击控件时,记录点击的位置(点K)
3、判断点K的位置是不是在点N和点M之间(用点的坐标判断即可),
如果点K不在点N和点M之间,则说明点击的位置不在这条线上。
如果点K在点N和点M之间,再做下面操作。
4、计算点N到点M的水平距离(长度I)和垂直距离(长度J)
5、计算点N到点N的水平距离(长度P)和垂直距离(长度Q)
6、判断长度P和长度Q的比例是不是与长度I和长度J的比例一致,
如果是则说明点击的位置在这条线上。
LZ试试可不可以。