Original Issuevision Learning Notes (ii)-----Add custom properties and events to a control

Source: Internet
Author: User
Notes | controls | Original Let's take a look at the issuevision. A user control panecaption a property window in a visual designer.






Look again at the Properties window when you use it in another user control Staffpane:






You'll find it comes out many more properties, which are properties that were not in the original inherited control, such as Inactivetextcolor,inactivetextcolor, and so on. How are they implemented? Let's take a look at the code for this user control PaneCaption.cs.

Namespace Issuevision
{
Custom Control This draws the caption for each pane. Contains an active
The state and draws the caption different to each state. Caption is drawn
With a gradient fill and antialias font.
public class Panecaption:usercontrol
{
Private Class Consts
{
......

You can see that it is inherited by System.Windows.Forms.UserControl, and figure one shows its default properties. Let's go ahead and PaneCaption.cs the code to see how the property or event is displayed in the visual designer.

[DefaultValueAttribute (typeof (Color), "3, 55, 145")]
[DescriptionAttribute ("Low color of the inactive gradient.")]
[CategoryAttribute ("appearance")]
Public Color Inactivegradientlowcolor
{
Get
{
return m_colorinactivelow;
}

Set
{
if (value = = Color.Empty)
{
Value = Color.FromArgb (3, 55, 145);
}
M_colorinactivelow = value;
Creategradientbrushes (); Custom method for creating a linear gradient brush
Invalidate (); Control.invalidate method to invalidate a specific area of a control and send a drawing message to the control
}
}

[CategoryAttribute ("appearance")]
[DescriptionAttribute ("High color of the inactive gradient.")]
[DefaultValueAttribute (typeof (Color), "90, 135, 215")]
Public Color Inactivegradienthighcolor
{
Get
{
return m_colorinactivehigh;
}

Set
{
if (value = = Color.Empty)
{
Value = Color.FromArgb (90, 135, 215);
}
M_colorinactivehigh = value;
Creategradientbrushes ();
Invalidate ();
}
}

[DescriptionAttribute ("Text displayed in the caption.")]
[DefaultValueAttribute ("")]
[CategoryAttribute ("appearance")]
public string Caption
{
Get
{
return m_text;
}

Set
{
M_text = value;
Invalidate ();
}
}

The results are shown in the following illustration:








We can see that the Views,staff list background uses this custom panecaption to produce a gradient effect (implemented by the Inactivegradienthighcolor and Inactivegradientlowcolor properties). Text views and staff list are implemented by attribute caption.




--------------------------------------------------------------------------------

Code Analysis:

The most important is the CategoryAttribute class, which specifies which category the property or event will appear in the visual designer, in the following table:

Category
Description

Action on the properties of the available actions.
Appearance properties that affect the appearance of an entity.
Behavior attributes that affect the behavior of an entity.
Data about the properties of the database.
The format affects the properties of the formatting.
Layout about layout properties.
Default categories have properties that are not categories.













For more information, see the. Net Framework 1.1 SDK
We see that these three attributes CategoryAttribute property values are CategoryAttribute ("appearance"), and as you can see from figure II, these properties are displayed under appearance.

The DefaultValueAttribute property, as the name suggests, is the default value for this custom property.
The DescriptionAttribute property is described for this custom attribute.


The key part has been set up, and the rest is how to implement the effect of the property, I explain in code:

protected override void OnPaint (PaintEventArgs e)
{
DrawCaption (E.graphics);
Base. OnPaint (e);
}

Draw the caption
private void DrawCaption (Graphics g)
{
Background
G.fillrectangle (this. Backbrush, this. DisplayRectangle);

if (M_antialias)
G.textrenderinghint = System.Drawing.Text.TextRenderingHint.AntiAlias;

Need a rectangle when want to use ellipsis
RectangleF bounds = new RectangleF (Consts.posoffset,
0,
This. Displayrectangle.width-consts.posoffset,
This. Displayrectangle.height);

g.DrawString (M_text, this. Font, this. Textbrush, Bounds, M_format);
}

Use graphics.drawstring to draw caption (that is, text views and staff list), Graphics.FillRectangle draw a gradient background. Note this Graphics.FillRectangle The first parameter of the method is the brush, which is the brush created by the Creategradientbrushes () method in the custom property code above. The code is as follows:

private void Creategradientbrushes ()
{
can only create brushes when have a width and height
if (Width > 0 && Height > 0)
{
if (m_brushactive!= null)
{
M_brushactive.dispose ();
}
Where the M_coloractivehigh value is the value of the custom attribute Inactivegradienthighcolor, M_coloractivelow is the value inactivegradientlowcolor the custom attribute.
m_brushactive = new LinearGradientBrush (DisplayRectangle, M_coloractivehigh, M_coloractivelow, lineargradientmode.vertical);

if (m_brushinactive!= null)
{
M_brushinactive.dispose ();
}

m_brushinactive = new LinearGradientBrush (DisplayRectangle, M_colorinactivehigh, M_colorinactivelow, lineargradientmode.vertical);
}
}

Gradient brush for the background
Private LinearGradientBrush Backbrush
{
Get
{
if (m_active && m_allowactive)
return m_brushactive;
Else
return m_brushinactive;
}
}

Add: According to GDI + requirements, all graphics are drawn by the OnPaint () method, and you can redraw the desired shape with only overloading this method. (This method is the same as the Paintcomponet () method in Java)

This time to write so much, I hope we have a lot of exchanges, this is only a person's understanding, but also to communicate with everyone will be improved.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.