사용자 도구

사이트 도구

English

jcontrols_cf35:custom_controls

Custom Controls

It is unlikely that jControls CF35 will have a control for every developers need. So, like all controls in the .Net Compact Framework, controls in the jControls CF35 library can be extended through inheritance.

We are not prepared, at this time, to document the jControls CF35 API in detail, but we hope the following information will give developers enough understanding to get started, and encourage them explore on their own. All public and protected properties and will be documented with XML documentation comments, so developers are encouraged to use Visual Studio's Intellisense to explore the API.

Creating a Custom Control

To create a custom control, use the Project–>Add New Item… menu, and choose the jCustomControl template.

A new custom control will be created with the following default source code.

public partial class jCustomControl1 : jControl
{
    public jCustomControl1()
    {
        InitializeComponent();
    }
 
    protected override void Draw(Rectangle clipRectangle)
    {
        //TODO: Add custom drawing code here
        base.Draw(clipRectangle);
    }
}

Unlike the .Net Framework controls which put custom drawing code in the OnPaint method, custom drawing for jControls should be added to the Draw and DrawForeground methods. All drawing is done to the control's Canvas, the control's back buffer. The jControls' OnPaint method simply flushes the Canvas to the screen, and typically does not need to be overriden.

Canvas Property

Canvas is a protected property that serves as the control's back buffer. For example, to draw a yellow circle inscribed in the area of the control, one might use the following code.

protected override void Draw(Rectangle clipRectangle)
{
    // restrict drawing to the invalid area, clipRectangle
    Canvas.ClipRectangle = clipRectangle;
 
    // set the thickness of the stroke to use for drawing
    float strokeThickness = 1.0f;
    Canvas.StrokeThickness = strokeThickness;
 
    // Draw the ellipse in yellow
    using (var brush = new jControls.SolidBrush(Color.Yellow))
    {
        PointF center = new PointF(Width / 2.0f, Height / 2.0f);
 
        // account for stroke thickenss when determining the dimensions
        // of the ellipse to draw
        SizeF diameter = new SizeF(Width - strokeThickness, Height - strokeThickness);
 
        Canvas.Brush = brush;
        Canvas.StrokeEllipse(center, diameter);
    }
}

InvalidateDraw Property

The InvalidateDraw method is analogous with the .Net Compact Framework controls' Invalidate method, and will initiate the control's drawing routines. It is typically used in response to a change to one of the control's properties. For example, if to extend the example given for the Canvas property allowing the stroke thickness to be customized, the following code could be used.

private float _strokeThickness = 1.0f;
public float StrokeThickness
{
    get { return _strokeThickness; }
    set
    {
        // only redraw if the value is changing.
        if (_strokeThickness != value)
        {
            _strokeThickness = value;
 
            // invalidate the entire control and initiate drawing
            InvalidateDraw();
        }
    }
}
 
protected override void Draw(Rectangle clipRectangle)
{
    // restrict drawing to the invalid area, clipRectangle
    Canvas.ClipRectangle = clipRectangle;
 
    // set the thickness of the stroke to use for drawing
    Canvas.StrokeThickness = _strokeThickness;
 
    // Draw the ellipse in yellow
    using (var brush = new jControls.SolidBrush(Color.Yellow))
    {
        PointF center = new PointF(Width / 2.0f, Height / 2.0f);
 
        // account for stroke thickenss when determining the dimensions
        // of the ellipse to draw
        SizeF diameter = new SizeF(Width - _strokeThickness, Height - _strokeThickness);
 
        Canvas.Brush = brush;
        Canvas.StrokeEllipse(center, diameter);
    }
}

Draw Method

The Draw method typically draws the overall appearance of the control. It is called before the DrawForeground method. The clipRectangle parameter specifies the area of the control that needs to be redrawn. It is typically assigned to the Canvas.ClipRectangle to restrict drawing to just that area, which significantly improves the performance of the control.

DrawForeground Method

The DrawForeground method typically draws the control's text. It is called after the Draw method. The clipRectangle parameter specifies the area of the control that needs to be redrawn. It is typically assigned to the Canvas.ClipRectangle to restrict drawing to just that area, which significantly improves the performance of the control.

jcontrols_cf35/custom_controls.txt · 마지막으로 수정됨: 2016/04/14 09:46 (바깥 편집)