2012-05-03 10 views
7

標準のWinForms 2.0を持つPropertyGrid control私は、コントロールの境界線の色を変更するか、境界線を完全に削除する方法を探しています。PropertyGridコントロールのボーダーカラーを変更する(またはボーダーを削除する)方法は?

enter image description here

私は残念ながら唯一の細胞間の内側の境界線を変更するLineColor propertyの意識です。

さらに、ILSpyを使用して、PropertyGridコントロールのソースコードを見ても、私には何の意味もないことがわかりました。

私の質問は:

PropertyGridコントロールの外側の境界線を削除するか、外側の境界線の色を変更する方法は?

アップデート2012-05-04 - ソリューション(別名 "ハック"):

enter image description here

アイデアはにある:

Jamie's answerに基づいて、私は作業溶液(which you can download from here)を組み立てプロパティグリッドをパネル内に配置し、パネルにコントロールをクリップさせます。

このアプローチでは、Paddingが「1」(または境界線が欲しいもの)である別のパネルにクリッピングパネルを配置し、このパネルにボーダーカラーとして機能するBackColorを与えました私の例)。

プロパティグリッドのアンカーを「左、右、上、下」に設定し、クリッピングパネルのDockを「フル」に設定します。

enter image description here

これは私の要件に適しています。私は、これを私が私が救うことができると思った2つのパネルのリソースを消費するので、これを一種のハックと見なします。

答えて

2

これは、別の代替です。これは汚いトリックですが、うまくいくはずです:

あなたのウィンドウやダイアログにPanelコントロールを入れて、サイズ100H x 300Vと言いましょう。位置-1、-1、サイズ102,302のパネル内にプロパティグリッドを配置します。

+0

Hehe、私はすでにそれを過去に試みたが、失敗した。私はもう一度それを試してみます。私が何か間違ったことがあった場合に備えて。 –

+0

@Jamieあなたの提案に感謝しました。私はあなたの答えで私の質問を更新します。 –

+1

自動フォームスケーリングのためにウィンドウのサイズが変更されたときに、この値はバイトになることがあるので注意してください。 PropertyGridがどこにあるべきか常に確認するために、パネルのResizeイベントハンドラにコードを記述してください。 –

1

あなたはそのために相互運用のビットが必要になります。私の最初の答えは、この特定の制御に適していないと思われるよう

[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

感謝、@Jamieは、私はこれを試してみましたが、残念ながらスタイルはすでに、変更されませんので、それを削除し、スタイルを省略しているようです。あなたのソリューションを試して、それを働かせましたか? –

+1

こんにちはUwe、いいえ、私は試していない、それはネイティブウィンドウコントロールの境界線を削除するための標準的な方法です。それは、プロパティグリッドは、この標準に従っていないようだ、私はあなたがこのようにそれを行うことはできないと思う。 –

0

このコードが動作します。ここで

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

このコードはなぜ機能しますか? –

+0

はい、そのコードは、PropertyGridのViewBoderとHelpBoderの表示と非表示を切り替えることができます。 – TrungNV

2

PropertyGridのプロセスを必要とする2つのコントロールを持っている私のプロジェクトからのコードです。
+ doccommentはドキュメントヘルプです。
+ gridViewプロパティ値を表示します。

これらのコントロールは、ControlDarkの色で境界矩形を描画します。

鮮明な表示を行うには、HelpBackColorとLineColorで矩形を再描画する必要があります。

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

コンテキストについても説明してください –

関連する問題