FormOptions.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:14k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Drawing.Drawing2D;
  7. namespace XmlNotepad
  8. {
  9. /// <summary>
  10. /// Summary description for FormOptions.
  11. /// </summary>
  12. public class FormOptions : System.Windows.Forms.Form
  13. {
  14.         private Settings settings;
  15.         UserSettings userSettings;
  16.         
  17.         private System.Windows.Forms.Button buttonOK;
  18.         private System.Windows.Forms.Button buttonCancel;
  19.         private System.Windows.Forms.FontDialog fontDialog1;
  20.         private System.Windows.Forms.ColorDialog colorDialog1;
  21.         private PropertyGrid propertyGrid1;
  22.         private Button buttonReset;
  23. /// <summary>
  24. /// Required designer variable.
  25. /// </summary>
  26. private System.ComponentModel.Container components = null;
  27. public FormOptions()
  28. {
  29. //
  30. // Required for Windows Form Designer support
  31. //
  32. InitializeComponent();
  33. }
  34.         protected override void OnLoad(EventArgs e) {
  35.             base.OnLoad(e);
  36.         
  37.             HelpProvider hp = this.Site.GetService(typeof(HelpProvider)) as HelpProvider;
  38.             if (hp != null) {
  39.                 hp.SetHelpKeyword(this, "Options");
  40.                 hp.SetHelpNavigator(this, HelpNavigator.KeywordIndex);
  41.             }
  42.             // now let the user resize it.
  43.             this.AutoSize = false; 
  44.         }
  45.         protected override bool ProcessDialogKey(Keys keyData) {
  46.             if (keyData == Keys.Escape) {
  47.                 this.Close();
  48.                 return true;
  49.             } else {
  50.                 return base.ProcessDialogKey(keyData);
  51.             }
  52.         }
  53. /// <summary>
  54. /// Clean up any resources being used.
  55. /// </summary>
  56. protected override void Dispose( bool disposing )
  57. {
  58. if( disposing )
  59. {
  60. if(components != null)
  61. {
  62. components.Dispose();
  63. }
  64. }
  65. base.Dispose( disposing );
  66. }
  67. #region Windows Form Designer generated code
  68. /// <summary>
  69. /// Required method for Designer support - do not modify
  70. /// the contents of this method with the code editor.
  71. /// </summary>
  72. private void InitializeComponent()
  73. {
  74.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormOptions));
  75.             this.buttonOK = new System.Windows.Forms.Button();
  76.             this.buttonCancel = new System.Windows.Forms.Button();
  77.             this.fontDialog1 = new System.Windows.Forms.FontDialog();
  78.             this.colorDialog1 = new System.Windows.Forms.ColorDialog();
  79.             this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
  80.             this.buttonReset = new System.Windows.Forms.Button();
  81.             this.SuspendLayout();
  82.             // 
  83.             // buttonOK
  84.             // 
  85.             resources.ApplyResources(this.buttonOK, "buttonOK");
  86.             this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
  87.             this.buttonOK.Name = "buttonOK";
  88.             this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
  89.             // 
  90.             // buttonCancel
  91.             // 
  92.             resources.ApplyResources(this.buttonCancel, "buttonCancel");
  93.             this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  94.             this.buttonCancel.Name = "buttonCancel";
  95.             // 
  96.             // propertyGrid1
  97.             // 
  98.             resources.ApplyResources(this.propertyGrid1, "propertyGrid1");
  99.             this.propertyGrid1.Name = "propertyGrid1";
  100.             // 
  101.             // buttonReset
  102.             // 
  103.             resources.ApplyResources(this.buttonReset, "buttonReset");
  104.             this.buttonReset.Name = "buttonReset";
  105.             this.buttonReset.UseVisualStyleBackColor = true;
  106.             this.buttonReset.Click += new System.EventHandler(this.buttonReset_Click);
  107.             // 
  108.             // FormOptions
  109.             // 
  110.             this.AcceptButton = this.buttonOK;
  111.             resources.ApplyResources(this, "$this");
  112.             this.BackColor = System.Drawing.Color.White;
  113.             this.CancelButton = this.buttonCancel;
  114.             this.Controls.Add(this.buttonReset);
  115.             this.Controls.Add(this.propertyGrid1);
  116.             this.Controls.Add(this.buttonCancel);
  117.             this.Controls.Add(this.buttonOK);
  118.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
  119.             this.HelpButton = true;
  120.             this.Name = "FormOptions";
  121.             this.ShowIcon = false;
  122.             this.ShowInTaskbar = false;
  123.             this.ResumeLayout(false);
  124.         }
  125. #endregion        
  126.         public override ISite Site {
  127.             get {
  128.                 return base.Site;
  129.             }
  130.             set {
  131.                 base.Site = value;
  132.                 if (value != null) {
  133.                     this.settings = value.GetService(typeof(Settings)) as Settings;
  134.                     this.userSettings = value.GetService(typeof(UserSettings)) as UserSettings;
  135.                     this.propertyGrid1.SelectedObject = this.userSettings;
  136.                 }
  137.             }
  138.         }
  139.         private void buttonOK_Click(object sender, System.EventArgs e) {
  140.             this.userSettings.Apply();
  141.             this.Close();
  142.         }
  143.         private void buttonReset_Click(object sender, EventArgs e) {
  144.             if (DialogResult.OK == MessageBox.Show(this, SR.ResetOptionsPrompt, SR.ResetOptionsPromptCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)) {
  145.                 this.userSettings.Reset();
  146.                 this.propertyGrid1.SelectedObject = null;
  147.                 this.propertyGrid1.SelectedObject = userSettings;
  148.             }
  149.         }
  150. }
  151.     // This class keeps s a local snapshot of the settings until the user clicks the Ok button,
  152.     // then the Apply() method is called to propagate the new settings to the underlying Settings object.
  153.     // It also provides localizable strings for the property grid.
  154.     public class UserSettings {
  155.         Settings settings;
  156.         Font font;
  157.         string fontName;
  158.         Color elementColor;
  159.         Color commentColor;
  160.         Color attributeColor;
  161.         Color piColor;
  162.         Color textColor;
  163.         Color cdataColor;
  164.         Color backgroundColor;
  165.         string updateLocation;
  166.         bool enableUpdate;
  167.         bool autoFormatOnSave;
  168.         int indentLevel;
  169.         IndentChar indentChar;
  170.         string newLineChars;
  171.         public UserSettings(Settings s) {            
  172.             this.settings = s;
  173.             this.font = (Font)this.settings["Font"];
  174.             this.fontName = font.Name + " " + font.SizeInPoints + " " + font.Style.ToString();
  175.             Hashtable colors = (Hashtable)this.settings["Colors"];
  176.             elementColor = (Color)colors["Element"];
  177.             commentColor = (Color)colors["Comment"];
  178.             attributeColor = (Color)colors["Attribute"];
  179.             piColor = (Color)colors["PI"];
  180.             textColor = (Color)colors["Text"];
  181.             cdataColor = (Color)colors["CDATA"];
  182.             backgroundColor = (Color)colors["Background"];
  183.             updateLocation = (string)this.settings["UpdateLocation"];
  184.             enableUpdate = (bool)this.settings["UpdateEnabled"];
  185.             autoFormatOnSave = (bool)this.settings["AutoFormatOnSave"];
  186.             indentLevel = (int)this.settings["IndentLevel"];
  187.             indentChar = (IndentChar)this.settings["IndentChar"];
  188.             newLineChars = Escape((string)this.settings["NewLineChars"]);
  189.         }
  190.         string Escape(string nl) {
  191.             return nl.Replace("r", "\r").Replace("n", "\n");
  192.         }
  193.         string Unescape(string nl) {
  194.             return nl.Replace("\r", "r").Replace("\n", "n");
  195.         }
  196.         public void Apply() {
  197.             this.settings["Font"] = this.font;
  198.             Hashtable colors = (Hashtable)this.settings["Colors"];
  199.             colors["Element"] = this.elementColor;
  200.             colors["Comment"] = this.commentColor;
  201.             colors["CDATA"] = this.cdataColor;
  202.             colors["Attribute"] = this.attributeColor;
  203.             colors["PI"] = this.piColor;
  204.             colors["Text"] = this.textColor;
  205.             colors["Background"] = this.backgroundColor;
  206.             
  207.             this.settings["UpdateEnabled"] = this.enableUpdate;
  208.             this.settings["UpdateLocation"] = this.updateLocation;
  209.             this.settings["AutoFormatOnSave"] = autoFormatOnSave;
  210.             this.settings["IndentLevel"] = indentLevel;
  211.             this.settings["IndentChar"] = indentChar;
  212.             this.settings["NewLineChars"] = Unescape(newLineChars);
  213.             this.settings.OnChanged("Colors");
  214.         }
  215.         public void Reset() {
  216.             this.font = new Font("Courier New", 10, FontStyle.Regular);
  217.             elementColor = Color.FromArgb(0, 64, 128); 
  218.             commentColor = Color.Green;
  219.             attributeColor = Color.Maroon;
  220.             piColor = Color.Purple;
  221.             textColor = Color.Black;
  222.             cdataColor = Color.Gray;
  223.             backgroundColor = Color.White;
  224.             updateLocation = "http://download.microsoft.com/download/6/e/e/6eef2361-33d4-48a2-b52e-5827c7f2ad68/Updates.xml";
  225.             enableUpdate = true;
  226.             autoFormatOnSave = true;
  227.             indentLevel = 2;
  228.             indentChar = IndentChar.Space;
  229.             newLineChars = Escape("rn");
  230.         }
  231.         [SRCategoryAttribute("ColorCategory")]
  232.         [LocDisplayName("ElementColor")]
  233.         [SRDescriptionAttribute("ElementColorDescription")]        
  234.         public Color ElementColor {
  235.             get {
  236.                 return this.elementColor;
  237.             }
  238.             set {
  239.                 this.elementColor = value;
  240.             }
  241.         }
  242.         [SRCategoryAttribute("ColorCategory")]
  243.         [LocDisplayName("AttributeColor")]
  244.         [SRDescriptionAttribute("AttributeColorDescription")]
  245.         public Color AttributeColor {
  246.             get {
  247.                 return this.attributeColor;
  248.             }
  249.             set {
  250.                 this.attributeColor = value;
  251.             }
  252.         }
  253.         [SRCategoryAttribute("ColorCategory")]
  254.         [LocDisplayName("CommentColor")]
  255.         [SRDescriptionAttribute("CommentColorDescription")]
  256.         public Color CommentColor {
  257.             get {
  258.                 return this.commentColor;
  259.             }
  260.             set {
  261.                 this.commentColor = value;
  262.             }
  263.         }
  264.         [SRCategoryAttribute("ColorCategory")]
  265.         [LocDisplayName("PiColor")]
  266.         [SRDescriptionAttribute("PiColorDescription")]
  267.         public Color PiColor {
  268.             get {
  269.                 return this.piColor;
  270.             }
  271.             set {
  272.                 this.piColor = value;
  273.             }
  274.         }
  275.         [SRCategoryAttribute("ColorCategory")]
  276.         [LocDisplayName("TextColor")]
  277.         [SRDescriptionAttribute("TextColorDescription")]
  278.         public Color TextColor {
  279.             get {
  280.                 return this.textColor;
  281.             }
  282.             set {
  283.                 this.textColor = value;
  284.             }
  285.         }
  286.         [SRCategoryAttribute("ColorCategory")]
  287.         [LocDisplayName("CDataColor")]
  288.         [SRDescriptionAttribute("CDataColorDescription")]
  289.         public Color CDataColor {
  290.             get {
  291.                 return this.cdataColor;
  292.             }
  293.             set {
  294.                 this.cdataColor = value;
  295.             }
  296.         }
  297.         [SRCategoryAttribute("ColorCategory")]
  298.         [LocDisplayName("BackgroundColor")]
  299.         [SRDescriptionAttribute("BackgroundColorDescription")]
  300.         public Color BackgroundColor {
  301.             get {
  302.                 return this.backgroundColor;
  303.             }
  304.             set {
  305.                 this.backgroundColor = value;
  306.             }
  307.         }
  308.         [SRCategoryAttribute("FontCategory")]
  309.         [LocDisplayName("FontCategory")]
  310.         [SRDescriptionAttribute("FontDescription")]
  311.         public Font Font {
  312.             get {
  313.                 return this.font;
  314.             }
  315.             set {
  316.                 this.font = value;
  317.             }
  318.         }
  319.         [SRCategoryAttribute("UpdateCategory")]
  320.         [LocDisplayName("EnableUpdate")]
  321.         [SRDescriptionAttribute("EnableUpdateDescription")]
  322.         public bool EnableUpdate {
  323.             get {
  324.                 return this.enableUpdate;
  325.             }
  326.             set {
  327.                 this.enableUpdate = value;
  328.             }
  329.         }
  330.         [SRCategoryAttribute("UpdateCategory")]
  331.         [LocDisplayName("UpdateLocation")]
  332.         [SRDescriptionAttribute("UpdateLocationDescription")]
  333.         public string UpdateLocation {
  334.             get {
  335.                 return this.updateLocation;
  336.             }
  337.             set {
  338.                 this.updateLocation = value;
  339.             }
  340.         }
  341.         [SRCategoryAttribute("FormatCategory")]
  342.         [LocDisplayName("AutoFormatOnSave")]
  343.         [SRDescriptionAttribute("AutoFormatOnSaveDescription")]
  344.         public bool AutoFormatOnSave {
  345.             get {
  346.                 return this.autoFormatOnSave;
  347.             }
  348.             set {
  349.                 this.autoFormatOnSave = value;
  350.             }
  351.         }
  352.         [SRCategoryAttribute("FormatCategory")]
  353.         [LocDisplayName("IndentLevel")]
  354.         [SRDescriptionAttribute("IndentLevelDescription")]
  355.         public int IndentLevel {
  356.             get {
  357.                 return this.indentLevel;
  358.             }
  359.             set {
  360.                 this.indentLevel = value;
  361.             }
  362.         }
  363.         [SRCategoryAttribute("FormatCategory")]
  364.         [LocDisplayName("IndentChar")]
  365.         [SRDescriptionAttribute("IndentCharDescription")]
  366.         public IndentChar IndentChar {
  367.             get {
  368.                 return this.indentChar;
  369.             }
  370.             set {
  371.                 this.indentChar = value;
  372.             }
  373.         }
  374.         [SRCategoryAttribute("FormatCategory")]
  375.         [LocDisplayName("NewLineChars")]
  376.         [SRDescriptionAttribute("NewLineCharsDescription")]
  377.         public string NewLineChars {
  378.             get {
  379.                 return this.newLineChars;
  380.             }
  381.             set {
  382.                 this.newLineChars = value;
  383.             }
  384.         }
  385.     }
  386. }