📄 ccircle.vb
字号:
'圆类
Imports System.Math
Imports System.Drawing.Drawing2D
Public Class CCircle
Inherits CGElement
Private m_Center As PointF
Private m_PCircle As PointF
'圆心属性
Public Property Center() As PointF
Get
Return m_Center
End Get
Set(ByVal Value As PointF)
m_Center = Value
End Set
End Property
'圆上一点属性
Public Property PCircle() As PointF
Get
Return m_PCircle
End Get
Set(ByVal Value As PointF)
m_PCircle = Value
End Set
End Property
'半径属性,只读
Public ReadOnly Property Radius() As Single
Get
Dim r As Single
r = DistPtoP(m_Center, m_PCircle)
Return r
End Get
End Property
'无参构造函数
Public Sub New()
Init()
End Sub
'构造函数,用已知的两个点构造圆
Public Sub New(ByVal pCenter As PointF, ByVal pCircle As PointF)
Init()
m_Center = pCenter
m_PCircle = pCircle
End Sub
'构造函数,用已知的圆构造圆
Public Sub New(ByVal aCircle As CCircle)
With aCircle
m_Center = .Center
m_PCircle = .PCircle
End With
End Sub
'初始化圆
Private Shadows Sub Init()
MyBase.Init()
With m_Center
.X = 0
.Y = 0
End With
m_PCircle = m_Center
End Sub
'绘圆
Public Overrides Sub Draw(ByVal g As Graphics, ByVal aDrawMode As geDrawMode)
'将控制点的坐标转换为页面坐标
Dim ec As PointF = WorldtoPage(m_Center)
Dim ep As PointF = WorldtoPage(m_PCircle)
'获取绘图环境的句柄
Dim hdc As IntPtr
hdc = g.GetHdc()
'设置画笔参数
Dim penPara As Integer() = DrawSettings(hdc, aDrawMode)
'创建画笔
Dim aPen As Long = Win32API.CreatePen(penPara(0), penPara(1), penPara(2))
'把画笔选入绘图环境,并返回原来的画笔
Dim oldP As Long = Win32API.SelectObject(hdc, aPen)
'把空刷子选入绘图环境
Win32API.SelectObject(hdc, Win32API.GetStockObject(5))
'绘圆
Win32API.Ellipse(hdc, ec.X - Radius, ec.Y + Radius, _
ec.X + Radius, ec.Y - Radius)
'把原来的画笔选入绘图环境
Win32API.SelectObject(hdc, oldP)
'删除新创建的画笔
Win32API.DeleteObject(aPen)
'释放绘图环境句柄
g.ReleaseHdc(hdc)
End Sub
'计算圆的包围矩形
Public Overrides Function GetBox() As CBox
Dim aBox As New CBox()
With aBox
.minX = m_Center.X - Radius
.minY = m_Center.Y - Radius
.maxX = m_Center.X + Radius
.maxY = m_Center.Y + Radius
End With
Return aBox
End Function
'拾取圆
Public Overrides Function Pick(ByVal aPos As PointF) As Boolean
Dim dist As Single
'如果拾取点不在包围矩形中,则该圆不被拾取
If (Not InBox(GetBox, aPos)) Then
Return False
Else
'如果拾取点在包围矩形中,且到圆心的距离在一定的范围内
'则圆被拾取,否则不被拾取
dist = DistPtoP(aPos, m_Center)
If ((dist > Radius - PickRadius) And _
(dist < Radius + PickRadius)) Then
Return True
Else
Return False
End If
End If
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -