PropertiesDialog.cs
上传用户:sxsgcs
上传日期:2013-10-21
资源大小:110k
文件大小:3k
源码类别:

CAD

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Globalization;
  9. namespace DrawTools
  10. {
  11.     partial class PropertiesDialog : Form
  12.     {
  13.         public PropertiesDialog()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private GraphicsProperties properties;
  18.         private const string undefined = "??";
  19.         private const int maxWidth = 5;
  20.         public GraphicsProperties Properties
  21.         {
  22.             get
  23.             {
  24.                 return properties;
  25.             }
  26.             set
  27.             {
  28.                 properties = value;
  29.             }
  30.         }
  31.         private void PropertiesDialog_Load(object sender, EventArgs e)
  32.         {
  33.             InitControls();
  34.             SetColor();
  35.             SetPenWidth();
  36.         }
  37.         private void InitControls()
  38.         {
  39.             for (int i = 1; i <= maxWidth; i++)
  40.             {
  41.                 cmbPenWidth.Items.Add(i.ToString(CultureInfo.InvariantCulture));
  42.             }
  43.         }
  44.         private void SetColor()
  45.         {
  46.             if (properties.Color.HasValue)
  47.                 lblColor.BackColor = properties.Color.Value;
  48.             else
  49.                 lblColor.Text = undefined;
  50.         }
  51.         private void SetPenWidth()
  52.         {
  53.             if (properties.PenWidth.HasValue)
  54.             {
  55.                 int penWidth = properties.PenWidth.Value;
  56.                 if (penWidth < 1)
  57.                     penWidth = 1;
  58.                 if (penWidth > maxWidth)
  59.                     penWidth = maxWidth;
  60.                 label2.Text = penWidth.ToString(CultureInfo.InvariantCulture);
  61.                 cmbPenWidth.SelectedIndex = penWidth - 1;
  62.             }
  63.             else
  64.             {
  65.                 label2.Text = undefined;
  66.             }
  67.         }
  68.         private void ReadValues()
  69.         {
  70.             if (cmbPenWidth.Text != undefined)
  71.             {
  72.                 properties.PenWidth = cmbPenWidth.SelectedIndex + 1;
  73.             }
  74.             if (lblColor.Text.Length == 0)
  75.             {
  76.                 properties.Color = lblColor.BackColor;
  77.             }
  78.         }
  79.         private void cmbPenWidth_SelectedIndexChanged(object sender, EventArgs e)
  80.         {
  81.             int width = cmbPenWidth.SelectedIndex + 1;
  82.             lblPenWidth.Text = width.ToString(CultureInfo.InvariantCulture);
  83.         }
  84.         private void btnSelectColor_Click(object sender, EventArgs e)
  85.         {
  86.             ColorDialog dlg = new ColorDialog();
  87.             dlg.Color = lblColor.BackColor;
  88.             if (dlg.ShowDialog(this) == DialogResult.OK)
  89.             {
  90.                 lblColor.BackColor = dlg.Color;
  91.                 lblColor.Text = "";
  92.             }
  93.         }
  94.         private void btnOK_Click(object sender, EventArgs e)
  95.         {
  96.             ReadValues();
  97.             this.DialogResult = DialogResult.OK;
  98.         }
  99.     }
  100. }