2011-12-16 16 views
6

私の設定: 私はVisual Studio 2008でC#アプリケーション(.NET 3.5)を持っています。WPFなどに切り替える機会はありません:)。Visual Studioでデザイン時にボタンをクリックできるようにしますか?

私のアプリには、Windows.Forms.TabControlの代わりに動作するカスタムコントロール(Windows.Forms.Buttonから派生したボタンクラス)が含まれています。私はこれらのボタンをお互いに関連付けることができ、各ボタンはそれが扱っている1つのコントロール(通常はWindows.Forms.Panelの何らかの種類)に関連付けることができます。それはこのようなものになります。

public class TabButton : System.Windows.Forms.Button 
{ 
    // ... 
    protected override void OnClick(EventArgs e) 
    { 
     base.OnClick(e); 
     this.myAssociatedControl.Visible = true; 
     this.tellMyBuddiesToHideTheirControls(); 
    } 
    // ... 
} 

基本的にはそれだけで、ボタンをクリックするとその結合制御を示すと関連するボタンにバインドされたコントロールが消えることについてです - ちょうどTabControlのように、しかしアプローチが簡単に設計可能であると私ボタンをコンテンツパネルから遠くに置くことができます。

問題:これは、実行時にかなりうまく動作しますが、設計時に使用量が奇数間違いなくある :マウスを使って、グループに属する制御that'sを見つけるとまで<Send To Back> Sのシリーズを実行します所望の制御が見える。

質問: 私が希望のように、私はちょうどそれらをクリックすることでタブを切り替えることができるように、それはTabControlのでないように設計時にボタンのクリックを評価するために、VSのデザイナーを伝える方法がありますランタイム?

私は今かなり長い間探してきました。 SOにはいくつかの記事がありますが、プロパティデザイナーに追加の属性を追加することしかカバーしていないようです。

+1

これはあなたのバイブルです:http://msdn.microsoft.com/en-us/library/c5z9s1h4.aspx –

+1

@DavidePirasは私にこのことを指してくれてありがとう!私はそれが完了するためにいくつかの努力を払うだろうと思うが、これは良い出発点と思われる... – mfeineis

+0

私は解決策を見つけた... – mfeineis

答えて

3

エディスは言う:リクエストによって 、自分の質問への答えを...

これは私のアプリケーションに適しているソリューションです。これは基本的にmsdnの例であり、カスタムデザイナーにクリック時のコールバックを使用させるためにいくつかの紆余曲折があります。誰も助けてくれることを願っています:-)。

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
public class TabButtonDesigner : System.Windows.Forms.Design.ControlDesigner 
{ 
    ShowTabGlyph myGlyph = null; 
    Adorner myAdorner; 

    public TabButtonDesigner() 
    { 
    } 

    public override void Initialize(IComponent component) 
    { 
     base.Initialize(component); 

     // Add the custom set of glyphs using the BehaviorService. 
     // Glyphs live on adornders. 
     myAdorner = new Adorner(); 
     BehaviorService.Adorners.Add(myAdorner); 
     myGlyph = new ShowTabGlyph(BehaviorService, Control); 
     myGlyph.Callback =() => 
     { 
      ((MyCustomTabButton)this.Control).ShowMyTab(); 
     }; 
     myAdorner.Glyphs.Add(myGlyph); 
    } 

    class ShowTabGlyph : Glyph 
    { 
     Control control; 
     BehaviorService behaviorSvc; 

     public Action Callback 
     { 
      get; 
      set; 
     } 

     public ShowTabGlyph(BehaviorService behaviorSvc, Control control) : 
      base(new ShowTabBehavior()) 
     { 
      this.behaviorSvc = behaviorSvc; 
      this.control = control; 
     } 

     public override Rectangle Bounds 
     { 
      get 
      { 
       // Create a glyph that is 10x10 and sitting 
       // in the middle of the control. Glyph coordinates 
       // are in adorner window coordinates, so we must map 
       // using the behavior service. 
       Point edge = behaviorSvc.ControlToAdornerWindow(control); 
       Size size = control.Size; 
       Point center = new Point(edge.X + (size.Width/2), 
        edge.Y + (size.Height/2)); 

       Rectangle bounds = new Rectangle(
        center.X - 5, 
        center.Y - 5, 
        10, 
        10); 

       return bounds; 
      } 
     } 

     public override Cursor GetHitTest(Point p) 
     { 
      // GetHitTest is called to see if the point is 
      // within this glyph. This gives us a chance to decide 
      // what cursor to show. Returning null from here means 
      // the mouse pointer is not currently inside of the glyph. 
      // Returning a valid cursor here indicates the pointer is 
      // inside the glyph, and also enables our Behavior property 
      // as the active behavior. 
      if (Bounds.Contains(p)) 
      { 
       return Cursors.Hand; 
      } 

      return null; 
     } 

     public override void Paint(PaintEventArgs pe) 
     { 
      // Draw our glyph. It is simply a blue ellipse. 
      pe.Graphics.DrawEllipse(Pens.Blue, Bounds); 
     } 

     // By providing our own behavior we can do something interesting 
     // when the user clicks or manipulates our glyph. 
     class ShowTabBehavior : Behavior 
     { 
      public override bool OnMouseUp(Glyph g, MouseButtons button) 
      { 
       //MessageBox.Show("Hey, you clicked the mouse here"); 
       //this. 
       ShowTabGlyph myG = (ShowTabGlyph)g; 
       if (myG.Callback != null) 
       { 
        myG.Callback(); 
       } 
       return true; // indicating we processed this event. 
      } 
     } 
    } 
} 

[DesignerAttribute(typeof(TabButtonDesigner))] 
public class MyCustomTabButton : System.Windows.Forms.Button 
{ 
    // The attribute will assign the custom designer to the TabButton 
    // and after a rebuild the button contains a centered blue circle 
    // that acts at design time like the button in runtime does ... 

    // ... 
} 
+0

これは私のために働いたdoens't。 VisualStyleElement.TreeView.Glyphは封印されていますが、私は上書きできません。また、ボタンをクリックできないようにしたいのです。 – deadManN

関連する問題