怎样编写自定义控件?有相关书籍吗?
自定义控件我一点也不会,哪位大大教教我吧? --------------------编程问答-------------------- 很多书里面都讲了一些 --------------------编程问答-------------------- UP --------------------编程问答-------------------- UP --------------------编程问答-------------------- 共同学习吧,我也是刚刚开始学,在书店也找了一下,没有找到专门讲的书,但很多书里都讲了一些,看看MSDN,一步一步来,我开发的东东都修改了N次代码了, --------------------编程问答-------------------- 书都不很全,MSDN上有简单的例子,你可以去看看。 --------------------编程问答-------------------- 专门的书可能没什么吧,你把MSDN用好也不错嘛,我写过一个using System;
using System.Collections;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace control
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class beziercontrol:System.Windows.Forms.UserControl
{
private Pen curvepen=new Pen(Color.Blue);
private Point pt1=new Point(0,0);
private Point pt2=new Point(10,30);
private Point pt3=new Point(50,20);
private Point pt4=new Point(100,40);
private System.ComponentModel.Container components;
public beziercontrol()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void Dispose(bool disposing)
{
base.Dispose (disposing);
components.Dispose();
}
private void InitializeComponet()
{
components=new System.ComponentModel.Container();
}
[CategoryAttribute("Apperance"),Description("start point of the curve.")]
public Point Pt1
{
get
{
return pt1;
}
set
{
pt1=value;
Invalidate();
}
}
[CategoryAttribute("Apperance"),Description("start point of the curve.")]
public Point Pt2
{
get
{
return pt2;
}
set
{
pt2=value;
Invalidate();
}
}
[CategoryAttribute("Apperance"),Description("start point of the curve.")]
public Point Pt3
{
get
{
return pt3;
}
set
{
pt3=value;
Invalidate();
}
}
[CategoryAttribute("Apperance"),Description("start point of the curve.")]
public Point Pt4
{
get
{
return pt4;
}
set
{
pt4=value;
Invalidate();
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
Rectangle r=new Rectangle(0,0,this.Size.Width,this.Size.Height);
LinearGradientBrush lb=new LinearGradientBrush(r,Color.Red,Color.Yellow,LinearGradientMode.BackwardDiagonal);
pevent.Graphics.FillRectangle(lb,r);
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.DrawBezier(curvepen,pt1,pt2,pt3,pt4);
pevent.Graphics.DrawEllipse(curvepen,pt1.X-1,pt1.Y-1,3,3);
pevent.Graphics.DrawEllipse(curvepen,pt2.X-1,pt2.Y-1,3,3);
pevent.Graphics.DrawEllipse(curvepen,pt3.X-1,pt3.Y-1,3,3);
pevent.Graphics.DrawEllipse(curvepen,pt4.X-1,pt4.Y-1,3,3);
}
}
} --------------------编程问答-------------------- 用过没写过 --------------------编程问答-------------------- UP --------------------编程问答-------------------- up --------------------编程问答-------------------- 看看这个吧。
http://book.csdn.net/bookfiles/533/#c1 --------------------编程问答-------------------- UP --------------------编程问答-------------------- 我写的一个控件,你凑和着看吧。
Imports System.ComponentModel--------------------编程问答-------------------- 你到网上可以找找,我记得见过一本书,名字好像就是 :组件开发...。【记不清了】 --------------------编程问答-------------------- Next --------------------编程问答-------------------- [ISBN0471793507] Wrox - Professional ASP.NET 2.0 Server Control and Component Development (Wrox Professional Guides).pdf
Imports VB = Microsoft.VisualBasic
Imports System.Text.RegularExpressions
Imports System.Text
Public Class EAN13
Inherits System.Windows.Forms.Control
Private Const Cstar As String = "101" 'EAN码的开始字符
Private Const Cmid As String = "01010" 'EAN码的中间字符
Private Const Cend As String = "101" 'EAN码的结束字符
Private m_Barcode As String '输入的EAN码字符
Public Sub New()
m_Barcode = "1234567890128"
End Sub
Private Function Rg(ByVal InputString As String) As Boolean
Dim Expression As New Regex("^\d{13}$")
If Not Expression.Match(InputString).Success Then
Return False
Else
Return True
End If
End Function
Public Property Barcode() As String
Get
Return m_Barcode
End Get
Set(ByVal value As String)
If Rg(value) = False Then
m_Barcode = ""
Else
m_Barcode = value
End If
Me.Invalidate()
End Set
End Property
Private Function Checkout() As Boolean
Dim i As Integer, Esum As Integer, Osum As Integer
For i = 12 To 1 Step -1
If Rg(m_Barcode) = True Then
If (i Mod 2) = 0 Then
Esum += CInt(Mid(m_Barcode, i, 1))
Else
Osum += CInt(Mid(m_Barcode, i, 1))
End If
Else
Return False
End If
Next
If (((Osum + 3 * Esum) + CInt(Strings.Right(m_Barcode, 1))) Mod 10) = 0 Then
Return True
End If
End Function
Private Function Convert() As String
Dim CharA As String() = New String(9) {"0001101", "0011001", "0010011", "0111101", "0100011", _
"0110001", "0101111", "0111011", "0110111", "0001011"}
Dim CharB As String() = New String(9) {"0100111", "0110011", "0011011", "0100001", "0011101", _
"0111001", "0000101", "0010001", "0001001", "0010111"}
Dim Guid As String() = New String(9) {"AAAAAA", "AABABB", "AABBAB", "AABBBA", "ABAABB", "ABBAAB", _
"ABBBAA", "ABABAB", "ABABBA", "ABBABA"}
Dim Rencode As String() = New String(9) {"1110010", "1100110", "1101100", "1000010", "1011100", _
"1001110", "1010000", "1000100", "1001000", "1110100"}
Dim BinaryCode As New StringBuilder(95), Lmethod As Integer, i As Integer
If Checkout() = True Then
Lmethod = CInt(Strings.Left(m_Barcode, 1))
BinaryCode.Append(Cstar)
For i = 2 To 7
If Mid(Guid(Lmethod), i - 1, 1) = "A" Then
BinaryCode.Append(CharA(CInt(Mid(m_Barcode, i, 1))))
Else
BinaryCode.Append(CharB(CInt(Mid(m_Barcode, i, 1))))
End If
Next
BinaryCode.Append(Cmid)
For i = 8 To 13
BinaryCode.Append(Rencode(CInt(Mid(m_Barcode, i, 1))))
Next
Return BinaryCode.Append(Cend).ToString()
End If
End Function
Private Sub DrawBaseLine(ByVal BaseLine As Graphics)
Dim Mypen As New Pen(Color.Black, 2)
Dim MyPen1 As New Pen(Color.White, 2)
Dim i As Integer, sh As Integer
For i = 1 To 95
If i < 5 OrElse i > 92 OrElse (i > 46 AndAlso i < 51) Then
sh = 10
Else
sh = 0
End If
If Mid(Convert, i, 1) = "1" Then
BaseLine.DrawLine(Mypen, 20 + 2 * (i - 1), 10, 20 + 2 * (i - 1), Me.Height - 25 + sh)
Else
BaseLine.DrawLine(MyPen1, 20 + 2 * (i - 1), 10, 20 + 2 * (i - 1), Me.Height - 25 + sh)
End If
Next
Dim Myfont As New Font("Verdana", 11, FontStyle.Regular)
Dim FontSize As Size = TextRenderer.MeasureText(Strings.Left(m_Barcode, 1), Myfont)
If Checkout() = False Then
BaseLine.DrawString("该条码不符合EAN13规范", Myfont, Brushes.Red, 20, 20)
Else
BaseLine.DrawString(Strings.Left(m_Barcode, 1), Myfont, Brushes.Black, 20 - FontSize.Width, Me.Height - 26)
For i = 2 To 7
BaseLine.DrawString(Strings.Mid(m_Barcode, i, 1), Myfont, Brushes.Black, CInt(FontSize.Width * i * 0.75), Me.Height - 26)
Next
For i = 8 To 13
BaseLine.DrawString(Strings.Mid(m_Barcode, i, 1), Myfont, Brushes.Black, CInt(FontSize.Width * i * 0.75 + 10), Me.Height - 26)
Next
End If
Mypen.Dispose()
MyPen1.Dispose()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim Myrect As New Rectangle(0, 0, Me.Width, Me.Height)
e.Graphics.FillRectangle(Brushes.White, Myrect)
DrawBaseLine(e.Graphics)
End Sub
End Class
这本书看完了,就差不多了
博客园里面的一些文章也可以参考,写的不错
http://www.cnblogs.com/Clingingboy/category/68883.html?Show=All --------------------编程问答-------------------- 看了,不错,
有待研究。 --------------------编程问答-------------------- MSDN里有资料,不过比较碎,而且需要你有耐心去看
控件开发还是有书的
最近出了一本:道不远人
搜一下就知道了,可以看看 --------------------编程问答-------------------- 小强控件网:www.xq168.cn --------------------编程问答-------------------- <webpart高级编程>
补充:.NET技术 , 组件/控件开发