Introduction
커스텀 컨트롤을 만드는 .NET 프레임워크의 짧고 간단한 데모입니다.
여기서 커스텀 컨트롤을 만들어서 윈도우 어플리케이션에서 컨트롤을 테스트 할것입니다.
컨트롤의 몇가지의 커스텀 프로퍼티를 구현할것입니다, 그래서 c#에서 구현을 어떻게 하는지 배울수 있습니다.
Building the Control
Visual Studio를 열고 새프로젝트를 시작합니다.
프로젝트는 Windows Control Library 템플릿의 기반이어야만 합니다.
ctlCuteButton 으로 프로젝트는 정하고 OK 클릭합니다.
프로젝트가 열리고 난후, 프로젝트에서 UserControl을 삭제합니다.
여기서 필요한게 'User Control'이 아니기 때문에 지워야 합니다.
'Project'메뉴에서 Project->Add User Control...로 가서 Custom Control 템플릿을 선택합니다.
이 예에서 우리가 필요한것이 'Custom Control' 입니다.
그것을 cuteButton으로 해야합니다.
OK 클릭합니다.
새로운 커스텀 컨트롤을 프로젝트에 추가하였습니다.
첫번째로 해야 할일은 cuteButton의 베이스 클래스를 바꿔야 합니다:
아래 줄을 :
public class cuteButton : System.Windows.Forms.Control
이와 같이:
public class cuteButton : System.Windows.Forms.Button
이제 컨트롤은 System.Windows.Forms.Button 클래스 기반입니다.
컨트롤을 위한 커스텀 프로퍼티들은 만들것입니다,
cuteButton 클래스에 다음 코드를 삽입합니다.
private Color m_color1 = Color.LightGreen; //first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)
public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}
public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}
public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}
public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}
Invalidate() 메소드는 뷰나 모든 컨트롤의 내부에서 디자인을 리프레쉬 하곤합니다.
그리고 마지막으로 컨트롤을 컨파일 하기전에 Paint 이벤트를 오버라이드 해야한다.
다음처럼 :
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,
c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
Ctrl + Shift + B 를 눌러 컨트롤을 컴파일 할수 있습니다.
여기 완전한 cuteButton.cs 파일의 내용이 있습니다
코드 전체:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ctlCuteButton
{
///
/// Summary description for cuteButton.
///
public class cuteButton : System.Windows.Forms.Button
{
private Color m_color1 = Color.LightGreen; // first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)
public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}
public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}
public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}
public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}
public cuteButton()
{
}
protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb
(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb
(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
}
}
}
Testing the Control
VS.NET을 새로 오픈합니다.
Windows Application 템플릿 프로젝트를 만듭니다.
새로운 Windows Form 프로젝트에서 toolbox에 컴파일한 커스텀 컨트롤을 추가할수 있습니다.
toolbox에서 마우스 오른쪽을 클릭해서 Customize Toolbox를 선택하고 .NET Framework Components 탭에서 Browse를 클릭하고 컨트롤 라이브러리 DLL위치를 선택합니다.
Toolbox에 cuteButton 컴포넌트가 나타날것입니다.
프로퍼티를 바꿀수 있습니다.(cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2)
댓글을 달아 주세요