2017-09-23 75 views
0

我想在c#winforms應用程序中禁用ToolStripSplitButton的按鈕部分。據我所知,這是不可能的,我想避免一個複雜的解決方案(重寫整個toolstripsplitbutton),所以我試圖至少禁用視覺,即。當按鈕部分禁用時繪製灰色圖標。如何禁用部分ToolStripSplitButton

首先我瀏覽referencesource發現的ToolStripRenderer和ToolStripProfessionalRenderer使用一些「內部」的屬性和方法在OnRenderItemImage(ToolStripItemImageRenderEventArgs E),所以我不能模仿(複製和修改-A位)OnRenderItemImage的行爲。

接下來我嘗試了下面的代碼。 基本上它的工作原理是,當標籤爲boolean false時,toolStripSplitButton1灰顯。 但是這個解決方案以某種方式殺死了我所有的System.Windows.Forms.Timer!試試這個代碼,當toolStripSplitButton1.Tag == false時,獨立於工具條的timer1不再勾選。而toolStripSplitButton1工具提示不會顯示出來(猜測是因爲它也使用Timer)。 (button1和button1_Click僅用於切換toolStripSplitButton1.Tag)

我的第一個問題是爲什麼OnRenderItemImage會殺死所有System.Windows.Forms.Timer?

第二個問題是如何實現原來的目標,灰色按鈕圖標至少在視覺上與按鈕本身無關?

using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      toolStrip1.Renderer = new MyToolStripProfessionalRenderer(); 

      toolStripSplitButton1.Tag = false; // this is for disabling button part 
      toolStripSplitButton1.ToolTipText = "toolStripSplitButton1 ToolTip"; 

      System.Windows.Forms.Timer timer1 = new Timer(); 
      timer1.Interval = 1000; 
      timer1.Tick += T_Tick; 
      timer1.Start(); 
     } 

     int ticks = 0; 
     private void T_Tick(object sender, EventArgs e) 
     { 
      System.Diagnostics.Debug.WriteLine(ticks++); 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
      toolStripSplitButton1.Tag = !((bool)toolStripSplitButton1.Tag); 
     } 
    } 

    class MyToolStripProfessionalRenderer : ToolStripProfessionalRenderer 
    { 

     protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e) 
     { 
      try 
      { 
       if (e.Item.Enabled && 
        e.Item.Tag?.GetType() == typeof(bool) && 
        !(bool)e.Item.Tag) 
       { 
        e.Item.Enabled = false; 
        base.OnRenderItemImage(e); 
        e.Item.Enabled = true; 
       } 
       else 
        base.OnRenderItemImage(e); 
      } 
      catch (Exception ex) 
      { 
       // this never reached, there's no exceptions 
       System.Diagnostics.Debug.WriteLine(ex); 
      } 
     } 

    } 

} 

Designer.cs:

namespace WindowsFormsApplication 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); 
      this.toolStrip1 = new System.Windows.Forms.ToolStrip(); 
      this.toolStripSplitButton1 = new System.Windows.Forms.ToolStripSplitButton(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.toolStrip1.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // toolStrip1 
      // 
      this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.toolStripSplitButton1}); 
      this.toolStrip1.Location = new System.Drawing.Point(0, 0); 
      this.toolStrip1.Name = "toolStrip1"; 
      this.toolStrip1.Size = new System.Drawing.Size(352, 25); 
      this.toolStrip1.TabIndex = 0; 
      this.toolStrip1.Text = "toolStrip1"; 
      // 
      // toolStripSplitButton1 
      // 
      this.toolStripSplitButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; 
      this.toolStripSplitButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton1.Image"))); 
      this.toolStripSplitButton1.ImageTransparentColor = System.Drawing.Color.Magenta; 
      this.toolStripSplitButton1.Name = "toolStripSplitButton1"; 
      this.toolStripSplitButton1.Size = new System.Drawing.Size(32, 22); 
      this.toolStripSplitButton1.Text = "toolStripSplitButton1"; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(13, 64); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 1; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(352, 265); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.toolStrip1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.toolStrip1.ResumeLayout(false); 
      this.toolStrip1.PerformLayout(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.ToolStrip toolStrip1; 
     private System.Windows.Forms.ToolStripSplitButton toolStripSplitButton1; 
     private System.Windows.Forms.Button button1; 
    } 
} 
+0

你的目標是?什麼是'SplitButton'的按鈕部分的用法被禁用,但用戶可以從它的下拉列表中選擇一個項目? –

+0

這是一個組合保存按鈕。按下按鈕部分不會「正常」保存。當數據沒有變化時應該禁用它。下拉部分包含「另存爲」和「僅保存修改」(這在我的應用程序的上下文中是有意義的),並且它們應該始終可以訪問。 (我知道,我的菜單結構的組建可以組織不同,以避免部分禁用按鈕的需要,但我正在尋找一種方法來做到這一點。)一般來說,我不明白爲什麼.NET使用'內部'的東西在一個可重寫的方法... – Feri

+0

你必須小心你在塗料事件中做什麼。做任何導致paint事件再次被觸發的事情都會讓你的UI線程燒掉100%的核心。似乎工作得很好,除了冷卻風扇的咆哮淹沒思考的能力,但是,定時器停止滴答。你不能像這樣混淆Enabled屬性,改變它會再次觸發paint事件。 –

回答

0

我不認爲使用定時器,用於跟蹤變化是一個好主意。我建議創建一個changeOccurred事件並在事件內部進行操作。

相關問題