appearancedesigneractionlist.cs

来自「中文名:Windows Forms 程序设计 英文名:Windows Form」· CS 代码 · 共 103 行

CS
103
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AlarmClockControlLibrary {
  public class AppearanceDesignerActionList : DesignerActionList {

    public AppearanceDesignerActionList(IComponent component)
      : base(component) {
      // Automatically display smart tag panel when
      // design-time component is dropped a form
      this.AutoShow = true;
    }

    public override DesignerActionItemCollection GetSortedActionItems() {

      if( this.Component == null ) return null;
      
      // Create list to store designer action items
      DesignerActionItemCollection actionItems = new DesignerActionItemCollection();

      // Fill list of designer action items
      actionItems.Add(new DesignerActionHeaderItem("Appearance"));

      actionItems.Add(
        new DesignerActionTextItem(
          "Properties that affect how the AlarmClockControl looks.",
          "Appearance"));
          
      // Add Face designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "Face",
          "Face",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "Face", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "Face", "Description")));

      // Add DigitalTimeFormat designer action property item
      // if Face is not Analog
      if( this.AlarmClockControl.Face != ClockFace.Analog ) {
        actionItems.Add(
          new DesignerActionPropertyItem(
            "DigitalTimeFormat",
            "Digital Time Format",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "DigitalTimeFormat", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "DigitalTimeFormat", "Description")));
      }

      // Return list of designer action items
      return actionItems;
    }

    // Face proxy property
    [Editor(typeof(FaceEditor), typeof(UITypeEditor))]
    public ClockFace Face {
      get { return this.AlarmClockControl.Face; }
      set {
        this.SetProperty("Face", value);

        // Refresh smart tag panel
        DesignerActionUIService das = this.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
        if( das != null ) das.Refresh(this.Component);
      }
    }

    // DigitalTimeFormat proxy property
    [Editor(typeof(DigitalTimeFormatEditor), typeof(UITypeEditor))]
    public string DigitalTimeFormat {
      get { return this.AlarmClockControl.DigitalTimeFormat; }
      set { this.SetProperty("DigitalTimeFormat", value); }
    }

    // Helper method to safely set a component抯 property
    private void SetProperty(string propertyName, object value) {
      // Get property
      PropertyDescriptor property = TypeDescriptor.GetProperties(this.AlarmClockControl)[propertyName];
      // Set property value
      property.SetValue(this.AlarmClockControl, value);
    }

    // Helper property to acquire a AlarmClockControl reference
    private AlarmClockControl AlarmClockControl {
      get { return (AlarmClockControl)this.Component; }
    }

    // Helper method that returns the category string from a CategoryAttribute 
    // assigned to a property exposed by the specified objecs
    private string GetAttributeString<T>(object source, string sourceProperty, string attributeProperty) {
      PropertyInfo sourcePropertyInfo = source.GetType().GetProperty(sourceProperty);
      T attribute = (T)sourcePropertyInfo.GetCustomAttributes(typeof(T), false)[0];
      if( attribute == null ) return null;
      Type attributeType = attribute.GetType();
      PropertyInfo attributePropertyInfo = attributeType.GetProperty(attributeProperty);
      return (string)attributePropertyInfo.GetValue(attribute, null);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?