2011-12-16 37 views
6

Cài đặt của tôi: Tôi có ứng dụng C# (.NET 3.5) trong Visual Studio 2008. Không có cơ hội chuyển sang WPF hoặc bất kỳ thứ gì :).Bật nút được nhấp vào lúc thiết kế trong Visual Studio?

Ứng dụng của tôi chứa một điều khiển tùy chỉnh (một lớp nút có nguồn gốc từ Windows.Forms.Button) hoạt động như một sự thay thế cho Windows.Forms.TabControl. Tôi có thể kết hợp các nút này với nhau và mỗi nút có thể được kết hợp với một điều khiển mà nó đang xử lý (thường là một số loại Windows.Forms.Panel). Nó trông giống như sau:

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

Về cơ bản nó chỉ là về nhấp chuột vào một nút, cho thấy kiểm soát ràng buộc của nó và có các điều khiển ràng buộc để các nút liên quan biến mất - giống như TabControl, nhưng phương pháp này có thể dễ dàng thể thiết kế và tôi có thể đặt các nút cách xa bảng nội dung của chúng.

Vấn đề: này hoạt động khá tốt trong thời gian chạy, nhưng việc sử dụng vào thời điểm thiết kế được cho là kỳ quặc: Với chuột, hãy tìm một that's kiểm soát thuộc vào nhóm và chạy một loạt các <Send To Back> s cho đến khi điều khiển mong muốn được hiển thị.

Câu hỏi: Có cách nào để nói với nhà thiết kế VS để đánh giá nhấp chuột vào các nút ở thời gian thiết kế như nó với TabControl để tôi có thể chuyển đổi các tab chỉ bằng cách nhấp vào chúng như tôi sẽ ở thời gian chạy?

Tôi đã tìm kiếm khá lâu rồi. Có một số bài viết ở đây tại SO nhưng chúng dường như chỉ bao gồm thêm các thuộc tính bổ sung cho trình thiết kế thuộc tính.

+1

đây là kinh thánh của bạn: http://msdn.microsoft.com/en-us/library/c5z9s1h4.aspx –

+1

@DavidePiras cảm ơn bạn đã chỉ cho tôi này! Tôi đoán nó sẽ mất một số nỗ lực để hoàn thành nhưng điều này có vẻ là một điểm khởi đầu tốt ... – mfeineis

+0

Tôi tìm thấy một giải pháp xem chỉnh sửa ... – mfeineis

Trả lời

3

Edith nói: Bằng cách yêu cầu, một câu trả lời cho câu hỏi của riêng tôi ...

Đây là giải pháp đó là phù hợp với ứng dụng của tôi. Nó về cơ bản là một ví dụ từ msdn với một số xoắn để có được các nhà thiết kế tùy chỉnh để sử dụng một cuộc gọi lại trên nhấp chuột. Hy vọng nó sẽ giúp bất cứ ai :-).

[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 này làm việc cho tôi. VisualStyleElement.TreeView.Glyph được niêm phong, và tôi không thể ghi đè lên, tôi cũng muốn thực hiện một nút bấm, không phải là một tabbutton – deadManN

Các vấn đề liên quan