Arcengine + C # -- ComboBox Color Control

Source: Internet
Author: User

This article describes how to write a single color and gradient color selection drop-down box (ComboBox) control. In some functions, you only need to call the colordialog Color Selection Control to customize the color. However, if you want to be closer to the ArcGIS operation style and use a gradient color, colordialog may not meet your needs.

If you are familiar with ArcGIS operations, you may remember to use the color drop-down box to select the color when you set the layers and elements. A drop-down box for the gradient color is displayed, as shown in. In some secondary development projects, you may encounter similar color selection requirements. I hope the introduction here will help you.

 

Select a gradient color for the TIN Model Based on the height

 

1. monochrome color drop-down box

The idea is to add the item that records the color value to ComboBox, and then redraw the rectangle area of the corresponding item of ComboBox based on the color value to achieve direct display.

The procedure is as follows:

Add a new usercontrol control under the project, drag a ComboBox control to usercontrol, set the docker attribute of ComboBox to fill, and then adjust the size of usercontrol, make it exactly in the ComboBox. The final key step is to add the code. The following code is attached. For specific explanations, see the code annotation. The effect is as follows:

Single Color Effect

Using system. Windows. forms;

Using system. drawing;

Using system;

Namespace windowsformsapplication1

{

/// <Summary>

/// Control class for Single Color Selection

/// </Summary>

Public partial class purecolorcombobox: usercontrol

{

Private color _ selectedcolor;

 

/// <Summary>

/// Selected color, encapsulation Field

/// </Summary>

Public color selectedcolor

{

Get {return _ selectedcolor ;}

Set {_ selectedcolor = value ;}

}

 

/// <Summary>

/// Events triggered when the ComboBox color is changed

/// </Summary>

Public event eventhandler selectcolorchanged;

 

// Custom initial color value. You can specify some specific colors if necessary.

// Private Static string [] colorlist =

//{

// "Aliceblue", "antiquewhite", "Aqua", "aquamarine", "azure", "beige ",

// "Bisque", "black", "blanchedalmond", "blue", "bluevilet", "brown ",

// "Burlywood", "cadetblue", "chartreuse", "Chocolate", "Coral ",

// "Cornflowerblue", "cornsilk", "Crimson", "cyan", "darkblue", "darkcyan ",

// "Darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta ",

// "Darkolivegreen", "darkorange", "darkorchid"

//};

 

/// <Summary>

/// Constructor

/// </Summary>

Public purecolorcombobox ()

{

Initializecomponent ();

Addcomponent ();

}

 

/// <Summary>

/// Load various color items (items)

/// </Summary>

Private void addcomponent ()

{

This. combobox1.drawmode = drawmode. ownerdrawfixed;

This. combobox1.dropdownstyle = comboboxstyle. dropdownlist;

// Set the ComboBox height

This. combobox1.itemheight = 18;

 

This. combobox1.beginupdate ();

This. combobox1.items. Clear ();

 

// Use this code if you want to use the custom color Initial Value

// Foreach (string onecolor in colorlist)

//{

// This. combobox1.items. Add (onecolor );

//}

 

// Load all colors of the system. If the custom color initial value is used, cancel the following code.

Array colors = system. enum. getvalues (typeof (knowncolor ));

For (INT I = colors. getlength (0)-1; I> = 0; I --)

{

This. combobox1.items. Add (colors. getvalue (I). tostring ());

}

 

This. combobox1.endupdate ();

}

 

// Draw a color rectangle in the drawitem event of ComboBox. This event is triggered every time an item is added.

// This function is automatically generated by double-clicking drawitem under the event bar of the ComboBox attribute label.

// Of course, you can also add

// This. combobox1.drawitem + = new system. Windows. Forms. drawitemeventhandler (this. combobox#drawitem );

Private void comboboxincludrawitem (Object sender, drawitemeventargs E)

{

If (E. index <0)

Return;

Rectangle rect = E. bounds; // obtain the rectangular box of the item.

 

// Obtain the color value of the corresponding item record

String colorname = combobox1.items [E. Index]. tostring ();

// Create a new single color brush. The color is the value of the corresponding item record.

Solidbrush brush = new solidbrush (color. fromname (colorname ));

 

_ Selectedcolor = brush. color;

// Beautiful, reducing the selected item area by 1 pixel

Rect. Inflate (-1,-1 );

// Fill color

E. Graphics. fillrectangle (brush, rect );

// Draw a colored border with black

E. Graphics. drawrectangle (pens. Black, rect );

}

 

/// <Summary>

/// Enable the color transfer event in the ComboBox option change trigger event

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "E"> </param>

Private void combobox#selectedindexchanged (Object sender, eventargs E)

{

// If the selectcolorchanged event is registered using the form of the control, the selectcolorchanged event is activated.

// Replace the selectedindexchanged event of conbobox with the selectcolorchanged event

If (selectcolorchanged! = NULL)

{

Selectcolorchanged (this, e );

}

}

}

}

Finally, drag the color control into the required form like other controls. In the selectcolorchanged event of the control, obtain the selected color value through the selectedcolor attribute of the control:

// Events triggered when the selected color value changes in the drop-down box

Private void colorcombobox#selectcolorchanged (Object sender, eventargs E)

{

// Obtain the currently selected value through the selectedcolor public field of ComboBox

MessageBox. Show ("the selected color is:" + this. colorcombobox1.selectedcolor. tostring ());

}

2. gradient color drop-down box

The gradient color drop-down box has the same idea as the monochrome drop-down box, but the color scheme is slightly different from the initial color setting. Here, only the code is pasted and the comments are omitted, for other information, see the upload example. The effect is as follows:

Gradient color drop-down box

Using system. Windows. forms;

Using system. drawing;

Using system;

Using system. Drawing. drawing2d;

Namespace windowsformsapplication1

{

Public partial class gradientcolorcombobox: usercontrol

{

Private color _ fromcolor;

Private color _ tocolor;

 

Public color fromcolor

{

Get {return _ fromcolor ;}

Set {_ fromcolor = value ;}

}

 

Public color tocolor

{

Get {return _ tocolor ;}

Set {_ tocolor = value ;}

}

 

Public event eventhandler selectcolorchanged;

 

// Predefined gradient

Private Static string [] colorlist =

{

"Aliceblue | antiquewhite", "Aqua | aquamarine", "Azure | beige ",

"Bisque | black", "blanchedalmond | blue", "blueviolet | Brown ",

"Burlywood | cadetblue", "chartreuse | chocolate ",

"Cornflowerblue | cornsilk", "Crimson | cyan", "darkblue | darkcyan ",

"Darkgoldenrod | darkgray", "darkgreen | darkkhaki ",

"Darkmagenta | darkolivegreen", "darkorange | darkorchid"

};

 

Public gradientcolorcombobox ()

{

Initializecomponent ();

Personalizecomponent ();

}

 

Private void personalizecomponent ()

{

This. combobox1.drawmode = drawmode. ownerdrawfixed;

This. combobox1.dropdownstyle = comboboxstyle. dropdownlist;

This. combobox1.itemheight = 18;

This. combobox1.beginupdate ();

This. combobox1.items. Clear ();

 

Foreach (string onecolor in colorlist)

{

This. combobox1.items. Add (onecolor );

}

This. combobox1.endupdate ();

}

 

Private void comboboxincludrawitem (Object sender, drawitemeventargs E)

{

If (E. index <0)

Return;

 

Rectangle rect = E. bounds;

// Read the start and end color values

String fcolorname = combobox1.items [E. Index]. tostring (). Split ('|') [0];

String tcolorname = combobox1.items [E. Index]. tostring (). Split ('|') [1];

_ Fromcolor = color. fromname (fcolorname );

_ Tocolor = color. fromname (tcolorname );

// Select a linear gradient brush

Lineargradientbrush brush = new lineargradientbrush (rect, _ fromcolor, _ tocolor, 0, false );

 

Rect. Inflate (-1,-1 );

// Fill color

E. Graphics. fillrectangle (brush, rect );

// Draw a border

E. Graphics. drawrectangle (pens. Black, rect );

}

 

Private void combobox#selectedindexchanged (Object sender, eventargs E)

{

If (selectcolorchanged! = NULL)

{

Selectcolorchanged (this, e );

}

}

}

}

Similarly, you need to drag the gradient control into the form. In the selectcolorchanged event of the control, you can use the fromcolor and tocolor attributes of the control to obtain the gradient value:

Private void gradientcolorcombobox#selectcolorchanged (Object sender, eventargs E)

{

MessageBox. Show ("the selected color is:" + this. gradientcolorcombobox1.fromcolor. tostring () + this. gradientcolorcombobox1.tocolor. tostring ());

}

Related Article

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.