사용자 도구

사이트 도구

English

jcontrols_cf35:custom_controls

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
jcontrols_cf35:custom_controls [2016/03/15 16:56]
Comfile Technology [Canvas Property]
jcontrols_cf35:custom_controls [2016/04/14 09:46] (현재)
줄 1: 줄 1:
 +====== 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 [[https://​msdn.microsoft.com/​en-us/​library/​b2s063f7%28v=vs.90%29.aspx|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. ​
 +
 +{{ .:​customcontroltemplate.png }}
 +
 +A new custom control will be created with the following default source code.
 +
 +<code csharp>
 +public partial class jCustomControl1 : jControl
 +{
 +    public jCustomControl1()
 +    {
 +        InitializeComponent();​
 +    }
 +    ​
 +    protected override void Draw(Rectangle clipRectangle)
 +    {
 +        //TODO: Add custom drawing code here
 +        base.Draw(clipRectangle);​
 +    }
 +}
 +</​code>​
 +
 +Unlike the .Net Framework controls which put custom drawing code in the [[https://​msdn.microsoft.com/​en-us/​library/​system.windows.forms.control.onpaint%28v=vs.90%29.aspx|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 [[https://​en.wikipedia.org/​w/​index.php?​title=Back_buffer|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.
 +
 +{{.:​customcontrolyellowcircle.png}}
 +
 +<code csharp>
 +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);
 +    }
 +}
 +</​code>​
 +
 +===== InvalidateDraw Property =====
 +The InvalidateDraw method is analogous with the [[https://​msdn.microsoft.com/​en-us/​library/​598t492a%28v=vs.90%29.aspx|.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 [[.:​custom_controls#​canvas_property|Canvas property]] allowing the stroke thickness to be customized, the following code could be used.
 +
 +|{{.:​customcontrolyellowcirclecustomthickness.png|}}|{{.:​customcontrolyellowcircle3properties.png|}}|
 +
 +<code csharp>
 +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);
 +    }
 +}
 +</​code>​
 +
 +
 +
 +===== 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 (바깥 편집)