C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)

时间:2021-10-04 02:16:31

//在Form中增加几个Button,分别命名为cmdArrow,cmdLabel,cmdTextBox,cmdComboBox,cmdGroupBox public partial class Form1 : Form { private MouseHook _MouseHook; //我们将所有的已经与具体控件关联了的UISizeKnob缓存在这个HashTable中 private Hashtable _HashUISizeKnob; //负责控件移动的类 private Hashtable _HashUIMoveKnob; public Form1() { InitializeComponent(); this._MouseHook = new MouseHook(this); this._HashUISizeKnob = new Hashtable(); this._HashUIMoveKnob = new Hashtable(); //为了简洁明了,我们在ControlAdded中来设置具体控件和UISizeKnob的关联 this.ControlAdded += new ControlEventHandler(Form1_ControlAdded); } void Form1_ControlAdded(object sender, ControlEventArgs e) { if (!(e.Control is UISizeDot)) { this._HashUISizeKnob.Add(e.Control, new UISizeKnob(e.Control)); this._HashUIMoveKnob.Add(e.Control, new UIMoveKnob(e.Control)); //点击控件的时候,显示控件的选择 e.Control.Click += new EventHandler(Control_Click); } } void Control_Click(object sender, EventArgs e) { //寿险清除已经选择的控件 foreach (UISizeKnob knob in this._HashUISizeKnob.Values) { knob.ShowUISizeDots(false); } //System.ComponentModel.Design.ISelectionService //System.Drawing.Design.IToolboxService try { ((UISizeKnob)this._HashUISizeKnob[sender]).ShowUISizeDots(true); //我这里仅仅做TextBox的属性演示,如果是其它的控件的话,那么你需要设计不同的ControlProperty(比如TextBoxProperty,ComboBoxProperty) if (sender is TextBox) { this.propertyGrid1.SelectedObject = new TextBoxProperty((TextBox)sender); } else { this.propertyGrid1.SelectedObject = null; } } catch { } } private void cmdArrow_Click(object sender, EventArgs e) { SettingService.Instance.SelectedToolBoxControl = null; } private void cmdLabel_Click(object sender, EventArgs e) { SettingService.Instance.SelectedToolBoxControl = new Label(); } private void cmdTextBox_Click(object sender, EventArgs e) { SettingService.Instance.SelectedToolBoxControl = new TextBox(); } private void cmdComboBox_Click(object sender, EventArgs e) { SettingService.Instance.SelectedToolBoxControl = new ComboBox(); } private void cmdGroupBox_Click(object sender, EventArgs e) { SettingService.Instance.SelectedToolBoxControl = new GroupBox(); } }