Issuevision learning notes (2) -- add custom attributes and events to controls

Source: Internet
Author: User
Let's take a look at the attribute window of a user control panecaption in issuevision In the visualization designer.

Let's look at the attribute window when using staffpane, another user control:

You will find that many attributes are added. These attributes are attributes that were originally inherited from the control, such as inactivetextcolor and inactivetextcolor. How are they implemented? Let's take a look at the user control'sCodePanecaption. CS.

Namespace issuevision
{
// Custom control that draws the caption for each pane. contains an active
// State and draws the caption different for each State. caption is drawn
// With a gradient fill and antialias font.
Public class panecaption: usercontrol
{
Private class consts
{
......

We can see that it is composedSystem. Windows. Forms. usercontrolThe default attributes are shown in Figure 1. The following is the panecaption. CS code to see how attributes or events are displayed in the visualization designer.

[Defavaluvalueattribute (typeof (color), "3, 55,145")]
[Descriptionattribute ("low color of the inactive gradient.")]
[Categoryattribute ("appearance")]
Public colorInactivegradientlowcolor
{
Get
{
Return m_colorinactivelow;
}

Set
{
If (value = color. Empty)
{
Value = color. fromargb (3, 55,145 );
}
M_colorinactivelow = value;
Creategradientbrushes (); // custom method for creating linear gradient brushes
Invalidate (); // control. invalidate method to invalidate a specific area of the Control and send a draw message to the control
}
}

[Categoryattribute ("appearance")]
[Descriptionattribute ("high color of the inactive gradient.")]
[Defaultvalueattribute (typeof (color), "90,135,215")]
Public colorInactivegradienthighcolor
{
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 StringCaption
{
Get
{
Return m_text;
}

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

The result is shown in:

We can see that views and stff list backgrounds are all customized.PanecaptionProduce gradient effect (InactivegradienthighcolorAndInactivegradientlowcolorAttribute implementation), text views and staff list are implemented by the attribute caption.

Code Analysis:

Most importantlyCategoryattributeClass, which specifies which category of the attribute or event will be displayed in the visualization designer. The specific category is as follows:

 Category

Description

Action Available operation attributes.
Appearance Attributes that affect the appearance of an object.
Behavior Attributes that affect entity behaviors.
Data Data attributes.
Format Affects the format attributes.
Layout The layout attributes.
Default Attributes without category belong to the default category.

For more information, see. NET Framework 1.1 SDK

We can see the three attributes of the example.CategoryattributeAttribute values are all categoryattribute ("appearance"). As shown in figure 2, these attributes are displayed under "appearance.

DefaultvalueattributeProperty is the default value of this custom property.
DescriptionattributeThe description of the custom attribute.

The key part has been set, and the rest is how to implement the attribute effect. I will describe the Code:

Protected override voidOnpaint(Painteventargs E)
{
Drawcaption (E. Graphics );
Base. onpaint (E );
}

// draw the caption
private void drawcaption (Graphics g)
{< br> // 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.DrawstringDraw the caption (Text views and staff list), graphics.FillrectangleDraw the gradient background. Note this graphics.FillrectangleThe first parameter of the method is the brush, which is in the custom attribute code above.Creategradientbrushes ()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 ();
}
// The m_col1_tivehigh value indicates the custom attribute. Inactivegradienthighcolor M_col1_tivelow is the custom attribute. Inactivegradientlowcolor .
M_brushactive = new lineargradientbrush (displayrectangle, m_col1_tivehigh, m_col1_tivelow, 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 lineargradientbrushBackbrush
{
Get
{
If (m_active & m_allowactive)
Return m_brushactive;
Else
Return m_brushinactive;
}
}

Note: according to the requirements of GDI +, all images are drawn throughOnpaint ()Method painting, you can re-draw the required image by reloading this method. (This method is like in JavaPaintcomponet ()Same method)

I have written so much this time. I hope you can communicate more with me. This is only a matter of my understanding.

Copyright yellowwee 2004. All right reserved.

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.