.net中C#怎样自定义不规则用户控件

shdxs 2009-10-30 09:54:16
.net中C#怎样自定义不规则用户控件,具体的例子,代码能全完整的话,就好了
...全文
125 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shdxs 2009-11-01
  • 打赏
  • 举报
回复
谁能将下面这段VB的代码改成c#的,必定重谢
Public Class UserControl1
Inherits System.Windows.Forms.UserControl

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

pointsCalculate(Me.Height, Me.Width, sideNum)
End Sub

'UserControl 重写 dispose 以清理组件列表。
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

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'UserControl1
'
Me.Name = "UserControl1"
Me.Size = New System.Drawing.Size(272, 224)

End Sub

#End Region
Private sideNum As Integer = 8 '定义初始化时的边数
Private myPoints() As PointF
Const Pi As Double = 3.1415926
<System.ComponentModel.Browsable(True)> Public Property side() As Integer
Get
Return sideNum
End Get
Set(ByVal Value As Integer)
If Value > 0 And sideNum <> Value Then
sideNum = Value
pointsCalculate(Me.Height, Me.Width, sideNum)

'每设定一次边数,需要重新计算顶点的坐标

Refresh()

'然后进行刷新

End If
End Set
End Property
Private Sub pointsCalculate(ByVal height As Integer, ByVal width As Integer, ByVal side As Integer)
Dim Angle As Double
Dim Slice As Double
Dim X1 As Integer, Y1 As Integer
Dim radius As Integer
Dim theta As Double
Dim n As Integer, i As Integer

'计算半径、中心和两个相邻顶点间的角度

Slice = 360 / sideNum
X1 = width \ 2
Y1 = height \ 2
If X1 > Y1 Then
radius = Y1 - 2
Else
radius = X1 - 2
End If

'计算绘制多边形每个顶点的坐标

ReDim myPoints(sideNum - 1)
Angle = 180.0
For i = 0 To sideNum - 1
theta = Angle * Pi / 180
myPoints(i).X = X1 + radius * (Math.Sin(theta))
myPoints(i).Y = Y1 + radius * (Math.Cos(theta))
Angle = Angle + Slice
Next i

'计算Region每个顶点的坐标
'Region的区域需要比多边形的面积大,否则多边形将是虚线的

Dim Points(sideNum - 1) As PointF
Angle = 180.0
radius += 1
For i = 0 To sideNum - 1
theta = Angle * Pi / 180
Points(i).X = X1 + radius * (Math.Sin(theta))
Points(i).Y = Y1 + radius * (Math.Cos(theta))
Angle = Angle + Slice
Next i
Dim myPath As New System.Drawing.Drawing2D.GraphicsPath
myPath.AddPolygon(myPoints)
Me.Region = New Region(myPath)
End Sub

'由于重新绘制控件时会自动调用Paint方法,所以需要添加符合本控件要求的处理方法,即重新绘制多边形

Private Sub PolyCtrl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim p As New Pen(Me.ForeColor)
e.Graphics.FillPolygon(New SolidBrush(Me.BackColor), myPoints)
e.Graphics.DrawPolygon(p, myPoints)
End Sub

'同时,在改变控件时也需要重新绘制多边形,所以需要重写Resize事件

Private Sub PolyCtrl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
pointsCalculate(Me.Height, Me.Width, sideNum)
Refresh()
End Sub

Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class
shdxs 2009-10-30
  • 打赏
  • 举报
回复
恩,在线在等,就是创建表示方向的形状的控件
lzsh0622 2009-10-30
  • 打赏
  • 举报
回复
bancxc 2009-10-30
  • 打赏
  • 举报
回复
自定义控件还是用户控件

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧