有没有办法拒绝用户在PropertyGrid控件中使用Splitter。我浏览了PropertyGrid Control的所有属性,但我没有找到禁用它。是我遗失的任何东西。无论如何,以编程方式进行。 我正在使用C#Winforms VS2010。
由于
答案 0 :(得分:3)
查看PropertyGrid
控件的源代码:MSDN PropertyGrid
要防止水平分隔符行为,请覆盖OnMouseDown和OnMouseMove方法。要防止垂直分隔符行为,一种方法是使用IMessageFilter并在鼠标位置靠近分隔符位置时使用鼠标事件。
public class MyForm3 : Form, IMessageFilter {
PropertyGrid pg = new MyPropertyGrid { Dock = DockStyle.Fill };
Control gridView = null;
MethodInfo miSplittlerInside = null;
public MyForm3() {
Controls.Add(pg);
pg.SelectedObject = new Button { Text = "Bob" };
var f = typeof(PropertyGrid).GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic);
gridView = (Control) f.GetValue(pg);
miSplittlerInside = gridView.GetType().GetMethod("SplitterInside", BindingFlags.Instance | BindingFlags.NonPublic);
Application.AddMessageFilter(this);
}
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONDBLCLK = 0x203;
public bool PreFilterMessage(ref Message m) {
if (m.HWnd == gridView.Handle) {
if (m.Msg == WM_MOUSEMOVE || m.Msg == WM_LBUTTONDOWN || m.Msg == WM_LBUTTONDBLCLK) {
Point pt = new Point(m.LParam.ToInt32());
bool inside = (bool) miSplittlerInside.Invoke(gridView, new Object[] { pt.X, pt.Y });
if (inside) {
return true;
}
}
}
return false;
}
class MyPropertyGrid : PropertyGrid {
protected override void OnMouseMove(MouseEventArgs me) {
//base.OnMouseMove(me);
// do nothing, prevent user from moving the split bar
}
protected override void OnMouseDown(MouseEventArgs me) {
//base.OnMouseDown(me);
}
}
}
注意:不调用基本方法会产生副作用,即不会触发这些事件的任何侦听器。
澄清:代码阻止了紫色分割器。你的问题是指橙色还是紫色?