重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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 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
绘图是系统内部操作的,不需要懂原理
方法就在那里,只有会用和不会用,你的代码告诉它绘制,它就会绘制。它(方法)究竟如何去绘制的并不是重点,反正它会绘制。
drawline(绘线)方法很简单,第一个参数是pen,它确定线条的颜色、宽度和样式。第二、第三个参数都是point类型,确定两个点的位置,绘制直线。
如果可以的话请把分给我
以下是cad2007版的,引用autocad 2007 type library 和autocad/objectdbx common 17如果是04或者版本更低的只要引用autocad 2007 type library,代码的话大同小异,思路是一样的
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim acadapp As Autodesk.AutoCAD.Interop.AcadApplication
acadapp = GetObject(vbNullString, "autoCAD.application")
Dim acaddoc As Autodesk.AutoCAD.Interop.AcadDocument
acaddoc = acadapp.ActiveDocument
Dim Ms As Autodesk.AutoCAD.Interop.Common.AcadModelSpace
Ms = acaddoc.ModelSpace
Dim acadObjectI As Autodesk.AutoCAD.Interop.Common.AcadObject
Dim Linei As Autodesk.AutoCAD.Interop.Common.AcadLine
Dim Circlei As Autodesk.AutoCAD.Interop.Common.AcadCircle
Dim Polylinei As Autodesk.AutoCAD.Interop.Common.AcadPolyline
Dim pt As Autodesk.AutoCAD.Interop.Common.AcadPoint
For Each acadObjectI In Ms
Debug.Print(acadObjectI.ObjectName)
Select Case acadObjectI.ObjectName
Case "AcDbLine"
Linei = acadObjectI
Debug.Print("X =" Linei.StartPoint(0).ToString)
Debug.Print("Y =" Linei.StartPoint(1).ToString)
Case ""
Case ""
End Select
Next
End Sub
1. 创建一个Graphics对象实例。
绘制图形必须创建Graphics对象。如果是在窗体上绘图,要使用下列代码创建Graphics对象;
Dim MyGraphics As Graphics = Me.CreateGraphics
如果是在PictrueBox里绘图,要使用下列代码创建Graphics对象;
Dim MyGraphics As Graphics = PictureBox1.CreateGraphics
2. 定义一个Brush对象,用来填充图形(如果你需要填充的话)。
如果填充封闭曲线或者多边形,必须创建Brush对象(或者Brush类的继承类对象),用来确定填充的颜色。例如下面的代码,创建了一个填充红色的画刷对象。在最后的括号里,用Color结构指定的枚举值,确定画刷的颜色。限于篇幅有关Color结构这里不展开,可能在后续博文里介绍。
Dim RedBrush As New SolidBrush(Color.Red)
可以把所有画的线都保存在一个列表中,画的时候全部画出即可。如下:
Public Class Form1
Class Line '直线类
Public Point1, Point2 As Point '成员,直线的两个端点
Sub New(p1 As Point, p2 As Point) '构造方法
Point1 = p1
Point2 = p2
End Sub
Public Sub Draw(g As Graphics) '绘制方法
g.DrawLine(Pens.Black, Point1, Point2)
End Sub
End Class
Private Lines As New List(Of Line) '列表用于保存所有画下的直线
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackColor = Color.White
DoubleBuffered = True '开启双缓冲可有效避免闪烁
End Sub
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
Lines.Add(New Line(e.Location, e.Location)) '在直线列表中添加直线
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button Windows.Forms.MouseButtons.Left Then Return '左键未按下
'鼠标拖动时改变列表最后一条直线(也即当前直线的第二个端点)
Lines(Lines.Count - 1).Point2 = e.Location
Refresh() '刷新窗体
End Sub
'在Form的Paint事件中绘制所有直线,每次Form1重绘时都会触发Paint事件
'PS: 也可以通过重写OnPaint方法来达到类似的效果
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias '开启抗锯齿
For Each l In Lines '遍历所有直线
l.Draw(e.Graphics) '调用绘制方法,传入的参数可以理解为画布
Next
End Sub
End Class
运行效果:
不用PictureBoxTest.Image属性,直接把图形绘制到PictureBoxTest上面就可以了。
Dim button As Integer = 0
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Using g As Graphics = Graphics.FromHwnd(PictureBoxTest.Handle)
Dim penRed As Pen = New Pen(Color.Red, 1) '定义红色画笔
Dim penblue As Pen = New Pen(Color.Blue, 1) '定义蓝色画笔
If button = 0 Then
g.DrawLine(penRed, 0, 0, 100, 100)
button = 1
ElseIf button = 1 Then
g.DrawLine(penblue, 100, 100, 200, 200)
button = 0
End If
End Using
End Sub