protected override void OnClick()
{
//
// When the utility is selected, check that we have a currently selected
// feature layer with a ClassBreaksRenderer already set. First we get the contents view.
//
IContentsView ContentsView = null;
ContentsView = ArcMap.Document.CurrentContentsView;
//
// If we have a DisplayView active
//
object VarSelectedItem = null;
IGeoFeatureLayer GeoFeatureLayer = null;
IClassBreaksRenderer ClassBreaksRenderer = null;
IEnumColors pColors = null;
int lngCount = 0;
IHsvColor HsvColor = null;
IClone ClonedSymbol = null;
ISymbol NewSymbol = null;
IActiveView ActiveView = null; //AlgorithimcColorRamp contains HSV colors.
if (ContentsView is TOCDisplayView)
{
if (ContentsView.SelectedItem is DBNull)
{
//
// If we don't have anything selected.
//
MessageBox.Show("SelectedItem is Null C#." + "Select a layer in the Table of Contents.", "No Layer Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//
// Get the selected Item.
//
VarSelectedItem = ContentsView.SelectedItem;
//
// Selected Item should implement the IGeoFeatureLayer interface - therefore we
// have selected a feature layer with a Renderer property (Note: Other interfaces
// also have a Renderer property, which may behave differently.
//
if (VarSelectedItem is IGeoFeatureLayer)
{
GeoFeatureLayer = (IGeoFeatureLayer)VarSelectedItem;
//
// Set the cached property to true, so we can refresh this layer
// without refreshing all the layers, when we have changed the symbols.
//
GeoFeatureLayer.Cached = true;
//
// Check we have an existing ClassBreaksRenderer.
//
if (GeoFeatureLayer.Renderer is IClassBreaksRenderer)
{
ClassBreaksRenderer = (IClassBreaksRenderer)GeoFeatureLayer.Renderer;
//
// If successful so far we can go ahead and open the Form. This allows the
// user to change the properties of the new RandomColorRamp.
//
frmAlgoColorRamp.m_lngClasses = ClassBreaksRenderer.BreakCount;
frmAlgoColorRamp.ShowDialog();
//
// Return the selected colors enumeration.
pColors = frmAlgoColorRamp.m_enumNewColors;
if (pColors == null)
{
//
// User has cancelled the form, or not set a ramp.
//
//MsgBox("Colors object is empty. Exit Sub")
return;
}
//
// Set the new random colors onto the Symbol array of the ClassBreaksRenderer.
//
pColors.Reset(); // Because you never know if the enumeration has been
// iterated before being passed back.
int tempFor1 = ClassBreaksRenderer.BreakCount;
for (lngCount = 0; lngCount < tempFor1; lngCount++)
{
//
// For each Value in the ClassBreaksRenderer, we clone the existing
// Fill symbol (so that all the properties are faithful preserved,
// and set its color from our new AlgorithmicColorRamp.
//
IClone symClone;
symClone = (IClone)ClassBreaksRenderer.get_Symbol(lngCount);
ClonedSymbol = CloneMe(ref (symClone));
//
// Now the ClonedSymbol variable holds a copy of the existing
// Symbol, we can change the assigned Color. We set the new
// symbol onto the Symbol array of the Renderer. '
//
HsvColor = (IHsvColor)pColors.Next();
NewSymbol = SetColorOfUnknownSymbol(ClonedSymbol, HsvColor);
if (NewSymbol != null)
ClassBreaksRenderer.set_Symbol(lngCount, NewSymbol);
}
//
// Refresh the table of contents and the changed layer.
//
ActiveView = (IActiveView)ArcMap.Document.FocusMap;
ActiveView.ContentsChanged();
ArcMap.Document.UpdateContents();
ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, GeoFeatureLayer, null);
}
}
}
}