2013-10-22 24 views
3

我想創建Custom ToolStripItem,其中有checkbox和Icon。 我通過使用兩個picturebox和標籤創建自定義控件來做到這一點。 左picturebox如果對於圖標而右是作爲特殊checkbox將位置添加到收藏夾。帶有圖標和複選框的ToolStripControlHost。 Hightlight如何選擇項目?在圖標字段添加圖標?

我將該控件添加到ToolStripControlHost

  1. 問題是左右兩側的空白我不想將它們移除或將圖標移動到左側欄並刪除右側空間。我試圖設置PaddingMargin值爲0,但它不起作用。 我發現類似的問題,但沒有正確的答案。是否有可能這樣做?

enter image description here

  • 我想使上的MouseEnter全線亮點就像在正常ToolStripMenuItem元素。當我重寫OnMouseEnter和OnMouseLeave更改背景屬性時,只需更改託管控件的顏色,而不是Antirie ToolStripControlHost。 是否可以模擬與ToolStripMenuItem相同的行爲?
  • 現在它看起來像圖片「A」,我希望它看起來更像是畫面B,但與星形複選框:

    enter image description here

    基本上我想我CustomToolStrip項目相似儘可能到ToolStripMenuItem。隨着點擊獨立事件(如果你點擊文本)ChackChange(如果你點擊星)

    任何想法如何做到這一點?

    這是我ToolStripControlHost代碼的一部分:

    private void AddEvents() 
        { 
         this.ToolStripICItemControl.eMouseEnter += new EventHandler(On_MouseEnter); 
         this.ToolStripICItemControl.eMouseLeave += new EventHandler(On_MouseLeave); 
        } 
        private void AutoSizeControl(Control control, int textPadding) 
        { 
         // Create a Graphics object for the Control. 
         Graphics g = control.CreateGraphics(); 
    
         // Get the Size needed to accommodate the formatted Text. 
         Size preferredSize = g.MeasureString(
          control.Text, control.Font).ToSize(); 
    
         // Pad the text and resize the control. 
         this.Size = new Size(
          preferredSize.Width + 5 + 50 + (textPadding * 2), 
          this.Size.Height /*+ (textPadding * 2)*/); 
    
         // Clean up the Graphics object. 
         g.Dispose(); 
        } 
        protected override void OnPaint(PaintEventArgs e) 
        { 
         base.OnPaint(e); 
        } 
        protected override void OnSubscribeControlEvents(Control c) 
        { 
         // Call the base so the base events are connected. 
         base.OnSubscribeControlEvents(c); 
    
         // Cast the control to a ToolStripCheckBox control. 
         ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c; 
    
         // Add the event. 
         checkBoxToolStrip.LabelClick += new EventHandler(checkBoxToolStrip_LabelClick); 
         checkBoxToolStrip.CheckedChanged += new EventHandler(checkBoxToolStrip_CheckedChanged); 
        } 
        protected override void OnUnsubscribeControlEvents(Control c) 
        { 
         // Call the base method so the basic events are unsubscribed. 
         base.OnUnsubscribeControlEvents(c); 
    
         // Cast the control to a ToolStripCheckBox control. 
         ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c; 
    
         // Remove the event. 
         checkBoxToolStrip.LabelClick -= new EventHandler(checkBoxToolStrip_LabelClick); 
        } 
        protected override void OnMouseEnter(EventArgs e) 
        { 
         DefaultBackColor = this.BackColor; 
         base.OnMouseEnter(e); 
         this.BackColor = Color.Aquamarine; 
        } 
        protected override void OnMouseLeave(EventArgs e) 
        { 
         base.OnMouseLeave(e); 
         this.BackColor = DefaultBackColor; 
        } 
        void checkBoxToolStrip_LabelClick(object sender, EventArgs e) 
        { 
         if (Click != null) 
          Click(this, e); 
        } 
        void checkBoxToolStrip_CheckedChanged(object sender, EventArgs e) 
        { 
         if (CheckedChange != null) 
          CheckedChange(this, e); 
        } 
        void On_MouseLeave(object sender, EventArgs e) 
        { 
         OnMouseLeave(e); 
         //throw new NotImplementedException(); 
        } 
        void On_MouseEnter(object sender, EventArgs e) 
        { 
         this.Select(); 
         this.Focus(); 
         OnMouseEnter(e); 
        } 
    
    +0

    你的問題不是很清楚。如果你想要一些完整的代碼,你應該用一些屏幕截圖描述你想要的東西(就像一些草圖就夠了)。如果你需要修正,你應該發佈你的代碼,顯示對說明性屏幕截圖不滿意的內容。 –

    +0

    請參閱[ToolStripMenuItem無法顯示覆選標記和圖像(圖標)時,RenderMode是「系統」?](http://stackoverflow.com/a/710610/719186) – LarsTech

    +0

    @LarsTech - 感謝您的鏈接,但沒有幫助我因爲我不知道如何添加新的Render方法到'ToolStripControlHost' @KingKing - 感謝你的提示,我會盡量讓它更清楚。 – GryzLi

    回答

    1

    我MENAGE做什麼,我需要從ToolStripMenuItem駕駛MyControl並重寫OnPaint方法

    public class MyControl : ToolStripMenuItem 
    { 
        public MyControl(){} 
        public MyControl(string text, Image image):base (text,image){} 
        public MyControl(string text):base(text){} 
        public MyControl(Image image):base(image){} 
        public MyControl(string text,Image image,EventHandler onClick):base(text,image,onClick){} 
        public MyControl(string text, Image image,int id):base(text,image){ this.ID = id;} 
        public MyControl(string text,Image image,int id,EventHandler onClick):base(text,image,onClick){this.ID = id; } 
    
        protected override void OnPaint(PaintEventArgs e) 
        { 
         base.OnPaint(e); 
    
         if (base.Checked == false) 
         { 
          Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20);    
          e.Graphics.DrawImage(Properties.Resources.BlackStar, rect); 
         } 
         else 
         { 
          Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20); 
          e.Graphics.DrawImage(Properties.Resources.YellowStar, rect); 
         } 
        } 
        public int ID { get; set; } 
        public bool StarClicked { get; set; } 
    
        protected override void OnMouseDown(MouseEventArgs e) 
        {    
         StarClicked = e.X > (this.Width - 20); 
         if (StarClicked) 
         { 
          this.Checked = this.Checked == true ? false : true; 
         } 
         else 
          base.OnClick(e); 
         base.OnMouseDown(e); 
        } 
    

    這它現在看起來如何

    http://i.stack.imgur.com/ZwESK.png

    形式代碼:

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
         InitializeComponent(); 
         AddMenuElements(7); 
        } 
        public void AddMenuElements(int licznik) 
        { 
         ToolStripMenuItem wszystkie = new ToolStripMenuItem("wszystkie", SystemIcons.Question.ToBitmap()); 
         manu1ToolStripMenuItem.DropDownItems.Add(wszystkie); 
         menuStrip1.Renderer = new ToolStripSystemRenderer(); 
         for (int i = 0; i < licznik; i++) 
         { 
          ToolStripMenuItem element = new ToolStripMenuItem(String.Format("element {0}", i), GetIcon(i),element2_Click); 
          element.Visible = i % 2 == 0 ? false:true;     
          manu1ToolStripMenuItem.DropDownItems.Add(element); 
         } 
    
    
         for (int i = 0; i < licznik; i++) 
         { 
          MyControl element2 = new MyControl(String.Format("element {0}",i), GetIcon(i),element2_Click); 
          element2.ID = i; 
          element2.CheckedChanged += (s, e) => 
          {      
           MyControl control = s as MyControl; 
           manu1ToolStripMenuItem.DropDownItems[control.ID + 1].Visible = control.Checked;     
          }; 
          element2.Checked = i % 2 == 0 ? false : true; 
    
          wszystkie.DropDownItems.Add(element2); 
    
         } 
        } 
        void element2_Click(object sender, EventArgs e) 
        { 
         MyControl kontener = (sender as MyControl); 
    
         if (kontener.ForeColor == Color.Red) 
          kontener.ForeColor = Color.Black; 
         else 
          kontener.ForeColor = Color.Red; 
    
        } 
        private static Image GetIcon(int i) 
        { 
         Image ikona = null; 
         switch (i) 
         { 
          case 0: ikona = SystemIcons.Application.ToBitmap(); 
           break; 
          case 1: ikona = SystemIcons.Asterisk.ToBitmap(); 
           break; 
          case 2: ikona = SystemIcons.WinLogo.ToBitmap(); 
           break; 
          case 3: ikona = SystemIcons.Exclamation.ToBitmap(); 
           break; 
          case 4: ikona = SystemIcons.Hand.ToBitmap(); 
           break; 
          case 5: ikona = SystemIcons.Warning.ToBitmap(); 
           break; 
          default: ikona = SystemIcons.Shield.ToBitmap(); 
           break; 
         } 
         return ikona; 
        } 
    } 
    

    它基本上我想要做什麼我只需要從星點擊後消失停止。 感謝幫助@LarsTech它給了我一個提示如何做到這一點。

    相關問題