⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alarmclockcontroldesigneractionlist.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
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 AlarmClockControlDesignerActionList : DesignerActionList {

    public AlarmClockControlDesignerActionList(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
      actionItems.Add(
        new DesignerActionPropertyItem(
          "DigitalTimeFormat",
          "Digital Time Format",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "DigitalTimeFormat", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "DigitalTimeFormat", "Description")));

      actionItems.Add(new DesignerActionHeaderItem("Behavior"));

      actionItems.Add(
        new DesignerActionTextItem(
        "Properties that affect how the AlarmClockControl behaves.",
        "Behavior"));

      // Add BackupAlarm designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "BackupAlarm",
          "Backup Alarm",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "BackupAlarm", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "BackupAlarm", "Description")));

      // Add PrimaryAlarm designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "PrimaryAlarm",
          "Primary Alarm",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "PrimaryAlarm", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "PrimaryAlarm", "Description")));

      actionItems.Add(new DesignerActionHeaderItem("Design"));

      actionItems.Add(
        new DesignerActionTextItem(
        "Properties that affect the AlarmClockControl at design-time.",
        "Design"));

      // ShowBorder designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "ShowBorder",
          "Show Border",
          this.GetAttributeString<CategoryAttribute>(this.Designer, "ShowBorder", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.Designer, "ShowBorder", "Description")));

      actionItems.Add(new DesignerActionHeaderItem("Commands"));

      actionItems.Add(
        new DesignerActionTextItem(
        "Design-Time AlarmClockControl commands.",
        "Commands"));

      //// Dock/Undock designer action method item
      //actionItems.Add(
      //  new DesignerActionMethodItem(
      //    this,
      //    "ToggleDockStyle",
      //    this.GetDockStyleText(),
      //    "Commands",
      //    "Dock or undock this control in it's parent container."));

      // EditClockHands designer action method item
      actionItems.Add(
        new DesignerActionMethodItem(
          this,
          "EditClockHands",
          "Edit Clock Hands...",
          "Commands",
          "Configure the AlarmClockControl's hour, minute and second hands.",
          true));

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

    // Face proxy property
    //[CategoryAttribute("Appearance")]
    //[DescriptionAttribute("Determines the clock face type to display.")]
    //[DisplayNameAttribute("Face")]
    [Editor(typeof(FaceEditor), typeof(UITypeEditor))]
    public ClockFace Face {
      get { return this.AlarmClockControl.Face; }
      set { SetProperty("Face", value); }
    }

    // DigitalTimeFormat proxy property
    //[Category("Appearance")]
    //[Description("The digital time format, constructed from .NET format specifiers.")]
    //[DisplayNameAttribute("Digital Time Format")]
    [Editor(typeof(DigitalTimeFormatEditor), typeof(UITypeEditor))]
    public string DigitalTimeFormat {
      get { return this.AlarmClockControl.DigitalTimeFormat; }
      set { SetProperty("DigitalTimeFormat", value); }
    }

    // BackupAlarm proxy property
    //[Category("Behavior")]
    //[Description("Primary alarm for very late risers.")]
    //[DisplayNameAttribute("Backup Alarm")]
    public DateTime BackupAlarm {
      get { return this.AlarmClockControl.BackupAlarm; }
      set { SetProperty("BackupAlarm", value); }
    }

    // PrimaryAlarm proxy property
    //[Category("Behavior")]
    //[Description("Primary alarm for very very late risers.")]
    //[DisplayNameAttribute("Primary Alarm")]
    public DateTime PrimaryAlarm {
      get { return this.AlarmClockControl.PrimaryAlarm; }
      set { SetProperty("PrimaryAlarm", value); }
    }

    // ShowBorder proxy property
    //[CategoryAttribute("Design")]
    //[DescriptionAttribute("Show/Hide a border at design-time.")]
    //[DisplayNameAttribute("Show Border")]
    public bool ShowBorder {
      get { return this.Designer.ShowBorder; }
      set { this.Designer.ShowBorder = value; }
    }

    // HourHand proxy property
    //[Category("Appearance")]
    //[Description("Sets the color and size of the Hour Hand.")]
    //[DisplayNameAttribute("Hour Hand")]
    public Hand HourHand {
      get { return this.AlarmClockControl.HourHand; }
      set { SetProperty("HourHand", value); }
    }

    // MinuteHand proxy property
    //[Category("Appearance")]
    //[Description("Sets the color and size of the Minute Hand.")]
    //[DisplayNameAttribute("Minute Hand")]
    public Hand MinuteHand {
      get { return this.AlarmClockControl.MinuteHand; }
      set { SetProperty("MinuteHand", value); }
    }

    // SecondHand proxy property
    //[Category("Appearance")]
    //[Description("Sets the color and size of the Second Hand.")]
    //[DisplayNameAttribute("Second Hand")]
    public Hand SecondHand {
      get { return this.AlarmClockControl.SecondHand; }
      set { SetProperty("SecondHand", value); }
    }

    // EditClockHands method
    //[CategoryAttribute("Commands")]
    //[DescriptionAttribute("Configure the AlarmClockControl's hour, minute and second hands.")]
    //[DisplayNameAttribute("Edit Clock Hands...")]
    public void EditClockHands() {
      // Create form
      HandsEditorForm form = new HandsEditorForm();
      // Set current hand values
      form.HourHand = this.AlarmClockControl.HourHand;
      form.MinuteHand = this.AlarmClockControl.MinuteHand;
      form.SecondHand = this.AlarmClockControl.SecondHand;
      // Update new hand values of OK button was pressed
      if( form.ShowDialog() == DialogResult.OK ) {
        IDesignerHost designerHost = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
        if( designerHost != null ) {
          DesignerTransaction t = designerHost.CreateTransaction();
          try {
            this.SetProperty("HourHand", form.HourHand);
            this.SetProperty("MinuteHand", form.MinuteHand);
            this.SetProperty("SecondHand", form.SecondHand);
            t.Commit();
          }
          catch { t.Cancel(); }
        }
      }
    }

    // Toggle AlarmClockControl's Dock property
    //[CategoryAttribute("Commands")]
    //[DescriptionAttribute("Dock or undock this control in it's parent container.")]
    //[DisplayNameAttribute("Dock/Undock in parent container")]
    public void ToggleDockStyle() {
      if( this.AlarmClockControl.Dock != DockStyle.Fill ) {
        this.SetProperty("Dock", DockStyle.Fill);
      }
      else {
        this.SetProperty("Dock", DockStyle.None);
      }
    }

    // Helper method that returns an appropriate
    // display name for the Dock/Undock property,
    // based on the AlarmClockControl's current Dock 
    // property value
    private string GetDockStyleText() {
      if( this.AlarmClockControl.Dock == DockStyle.Fill ) {
        return "Undock in parent container";
      }
      else {
        return "Dock in parent container";
      }
    }

    // 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 method to acquire a AlarmClockControlDesigner reference
    private AlarmClockControlDesigner Designer {
      get {
        IDesignerHost designerHost = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
        if( designerHost == null ) return null;
        return (AlarmClockControlDesigner)designerHost.GetDesigner(this.AlarmClockControl);
      }
    }

    // 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -