2012-05-03 47 views
7

Có chuẩn WinForms 2.0 PropertyGrid control Tôi đang tìm cách thay đổi màu đường viền của điều khiển hoặc xóa toàn bộ đường viền.Làm cách nào để thay đổi màu đường viền (hoặc xóa đường viền) của điều khiển PropertyGrid?

enter image description here

Tôi nhận thức được LineColor property mà tiếc là chỉ thay đổi biên giới bên trong giữa các tế bào.

Ngoài ra, tôi đã sử dụng ILSpy để xem mã nguồn của điều khiển PropertyGrid và vẫn không tìm thấy gì có ý nghĩa đối với tôi.

Câu hỏi của tôi là:

Làm thế nào để loại bỏ các đường viền ngoài của một điều khiển PropertyGrid hoặc thay đổi màu của đường viền bên ngoài?

Cập nhật 2012-05-04 - Giải pháp (hay còn gọi là "hack"):

Dựa trên Jamie's answer tôi tập hợp một giải pháp làm việc (which you can download from here):

enter image description here

Ý tưởng là để đặt lưới thuộc tính bên trong một bảng điều khiển và để cho bảng điều khiển clip điều khiển.

Với phương pháp này, tôi đã làm ra bảng điều khiển cắt vào một bảng mà có một Padding "1" (hoặc bất cứ điều gì bạn muốn biên giới phải) và đưa bảng này một BackColor đóng vai trò như màu biên giới (màu xanh lá cây trong ví dụ của tôi).

Đặt neo của lưới thuộc tính thành "Trái, Phải, Trên cùng, Dưới cùng", đặt Dock của bảng cắt thành "Đầy đủ".

enter image description here

Điều này phù hợp với yêu cầu của tôi. Tôi sẽ thấy điều này như là một loại hack vì nó tiêu thụ tài nguyên của hai tấm mà tôi hy vọng tôi có thể tiết kiệm.

Trả lời

2

này là thay thế khác, vì nó có vẻ như câu trả lời đầu tiên của tôi là không thích hợp cho kiểm soát đặc biệt này. Đây là một thủ thuật bẩn nhưng nên hoạt động:

Đặt điều khiển Bảng điều khiển trong cửa sổ hoặc hộp thoại của bạn, giả sử với kích thước 100H x 300V. Đặt propertygrid bên trong bảng điều khiển với vị trí -1, -1 và kích thước 102,302.

+0

Hehe, tôi đã thử điều đó trong quá khứ, không thành công. Tôi sẽ thử lại lần nữa, chỉ trong trường hợp tôi đã làm điều gì sai. –

+0

@Jamie Nó hoạt động, cảm ơn đề xuất của bạn! Tôi sẽ cập nhật câu hỏi của tôi với câu trả lời của bạn. –

+1

Hãy cẩn thận, điều này có thể byte khi các cửa sổ được thay đổi kích cỡ vì quy mô hình thức tự động. Bạn thực sự muốn viết mã trong trình xử lý sự kiện Resize của bảng điều khiển để luôn đảm bảo rằng PropertyGrid là nơi cần thiết. –

1

bạn sẽ cần một chút interop cho rằng:

[DllImport("User32", CharSet=CharSet.Auto)] 
private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); 
[DllImport("User32", CharSet=CharSet.Auto)] 
private static extern int GetWindowLong(IntPtr hWnd, int Index); 

int GWL_STYLE = -16; 
int WS_BORDER = 0x00800000; 
IntPtr hWnd = yourPropertyGrid.Handle; 

int style = GetWindowLong(hWnd, GWL_STYLE); 
style = style & ~WS_BORDER; 
SetWindowLong(hWnd, GWL_STYLE, style); 
+0

Thank, @Jamie tôi đã cố gắng này, nhưng tiếc là phong cách đã dường như bỏ qua phong cách, do đó loại bỏ nó, không thay đổi. Bạn đã thử giải pháp của mình và đã làm việc chưa? –

+1

Hi Uwe, không, tôi đã không thử, nó chỉ là cách tiêu chuẩn để loại bỏ biên giới của một cửa sổ bản địa kiểm soát. có vẻ như các propertygrid không theo tiêu chuẩn này, vì vậy, tôi nghĩ rằng bạn sẽ không thể làm điều đó theo cách này. –

0

mã này hoạt động.

private void SetHelpBoderColor(bool showBorder) 
{ 
    if (showBorder) 
    { 
     //Set Default ViewBackColor 
     PropertyInfo viewBackColor = this.propertyGrid.GetType().GetProperty("ViewBorderColor"); 
     if (viewBackColor != null) 
      viewBackColor.SetValue(this.propertyGrid, SystemColors.ControlDark, null); 

     //Set Default HelpBorderColor 
     PropertyInfo helpBorderColor = this.propertyGrid.GetType().GetProperty("HelpBorderColor"); 
     if (helpBorderColor != null) 
      helpBorderColor.SetValue(this.propertyGrid, SystemColors.ControlDark, null); 

    } 
    else 
    { 
     //Set ViewBackColor 
     PropertyInfo viewBackColor = this.propertyGrid.GetType().GetProperty("ViewBorderColor"); 
     if (viewBackColor != null) 
      viewBackColor.SetValue(this.propertyGrid, SystemColors.Control, null); 

     //Set HelpBorderColor 
     PropertyInfo helpBorderColor = this.propertyGrid.GetType().GetProperty("HelpBorderColor"); 
     if (helpBorderColor != null) 
      helpBorderColor.SetValue(this.propertyGrid, SystemColors.Control, null); 
    } 

    if (DesignMode) 
    { 
     Parent.Refresh(); 
    } 
} 
+1

Tại sao mã này hoạt động? –

+0

Có, mã đó có thể hiển thị hoặc ẩn ViewBoder và HelpBoder trong PropertyGrid. – TrungNV

2

Đây là mã từ dự án của tôi

PropertyGrid có hai điều khiển mà cần quá trình.
+ doccomment là Trợ giúp tài liệu.
+ gridXem giá trị thuộc tính hiển thị đó.

Các điều khiển đó vẽ hình chữ nhật viền với màu ControlDark.

Chúng tôi cần vẽ hình chữ nhật vẽ với HelpBackColor và LineColor để làm cho chế độ xem rõ ràng.

namespace Bravo.Bravo7.UI 
    { 
     public class MyPropertyGrid : PropertyGrid 
     { 
      public class SnappableControl : NativeWindow 
      { 
       private Control _parent; 
       private MyPropertyGrid _ownerGrid; 

       public SnappableControl(Control parent, MyPropertyGrid ownerGrid) 
       { 
        _parent = parent; 
        _parent.HandleCreated += _parent_HandleCreated; 
        _parent.HandleDestroyed += _owner_HandleDestroyed; 

        _ownerGrid = ownerGrid; 
       } 

       protected override void WndProc(ref Message m) 
       { 
        base.WndProc(ref m); 

        switch (m.Msg) 
        { 
         case (int)NativeMethods.WM_NCPAINT: 
         case (int)NativeMethods.WM_PAINT: 

          using (var g = _parent.CreateGraphics()) 
          { 
           using (var pen = new Pen(_ownerGrid.HelpBackColor)) 
           { 
            var clientRectangle = _parent.ClientRectangle; 
            clientRectangle.Width--; 
            clientRectangle.Height--; 
            g.DrawRectangle(pen, clientRectangle); 
           } 
          } 

          break; 
        } 
       } 

       void _owner_HandleDestroyed(object sender, EventArgs e) 
       { 
        ReleaseHandle(); 
       } 

       void _parent_HandleCreated(object sender, EventArgs e) 
       { 
        AssignHandle(_parent.Handle); 
       } 
      } 

      public class PropertyGridView : NativeWindow 
      { 
       private Control _parent; 
       private MyPropertyGrid _ownerGrid; 

       public PropertyGridView(Control parent, MyPropertyGrid ownerGrid) 
       { 
        _parent = parent; 
        _parent.HandleCreated += _owner_HandleCreated; 
        _parent.HandleDestroyed += _owner_HandleDestroyed; 

        _ownerGrid = ownerGrid; 
       } 

       protected override void WndProc(ref Message m) 
       { 
        base.WndProc(ref m); 

        switch (m.Msg) 
        { 
         case (int)NativeMethods.WM_NCPAINT: 
         case (int)NativeMethods.WM_PAINT: 

          using (var g = _parent.CreateGraphics()) 
          { 
           using (var pen = new Pen(_ownerGrid.LineColor)) 
           { 
            g.DrawRectangle(pen, 0, 0, _parent.Width - 1, _parent.Height - 1); 
           } 
          } 

          break; 
        } 
       } 

       void _owner_HandleDestroyed(object sender, EventArgs e) 
       { 
        ReleaseHandle(); 
       } 

       void _owner_HandleCreated(object sender, EventArgs e) 
       { 
        AssignHandle(_parent.Handle); 
       } 
      } 

      public class MyToolStripRenderer : ToolStripSystemRenderer 
      { 
       protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) 
       { 
        //base.OnRenderToolStripBorder(e); 
       } 
      } 

      public MyPropertyGrid() 
      { 
       base.LineColor = SystemColors.Control; 
       base.ViewBackColor = Color.FromArgb(246, 246, 246); 

       base.DrawFlatToolbar = true; 
       base.ToolStripRenderer = new MyToolStripRenderer(); 

       var docDocument = typeof(PropertyGrid) 
        .GetField("doccomment", BindingFlags.NonPublic | BindingFlags.Instance) 
        .GetValue(this) as Control; 

       new SnappableControl(docDocument, this); 

       var gridView = typeof(PropertyGrid) 
        .GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance) 
        .GetValue(this) as Control; 

       new PropertyGridView(gridView, this); 
      } 

     } 

    } 

Screen Shot

+0

Giải thích ngữ cảnh –

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