拥有标准的WinForms 2.0 PropertyGrid
control我正在寻找一种方法来更改控件的边框颜色或完全删除边框。
我知道LineColor
property,但遗憾的是只更改了单元格之间的内部边界。
此外,我使用ILSpy来查看PropertyGrid
控件的源代码,但仍然没有发现对我有意义。
我的问题是:
如何删除PropertyGrid
控件的外边框或更改外边框的颜色?
更新2012-05-04 - 解决方案(又名“黑客”):
基于Jamie's answer我汇总了一个有效的解决方案(which you can download from here):
我们的想法是将属性网格放在面板中,让面板剪切控件。
通过这种方法,我确实将剪切面板放置到另一个面板中,Padding
为“1”(或任何您想要的边框),并为此面板提供BackColor
服务作为边框颜色(在我的示例中为绿色)。
将属性网格的Anchor设置为“Left,Right,Top,Bottom”,将剪切面板的Dock
设置为“Full”。
这适合我的要求。我会认为这是一种黑客行为,因为它消耗了两个面板的资源,我希望我可以节省。
答案 0 :(得分:2)
这是另一种选择,因为我的第一个答案似乎不适合这个特定的控制。这是一个肮脏的伎俩,但应该有效:
将Panel控件放在窗口或对话框中,大小为100H x 300V。将propertygrid放在面板中,位置为-1,-1,大小为102,302。
答案 1 :(得分:2)
以下是我项目的代码
PropertyGrid有两个需要处理的控件。
+ 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);
}
}
}
答案 2 :(得分: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);
答案 3 :(得分: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();
}
}
答案 4 :(得分:0)