byt*_*e77 5 .net c# splitcontainer winforms
如何在SplitContainer上禁用焦点提示?我问,因为我宁愿自己使用OnPaint绘制它们,以使它看起来更平滑.
我试过这个:
protected override bool ShowFocusCues
{
get
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制:
public class cSplitContainer : SplitContainer
{
private bool IsDragging;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (!IsSplitterFixed) IsDragging = true;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (IsDragging)
{
IsDragging = false;
IsSplitterFixed = false;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (IsDragging)
{
IsSplitterFixed = true;
if (e.Button == MouseButtons.Left)
{
if (Orientation == Orientation.Vertical)
{
if (e.X > 0 && e.X < Width) SplitterDistance = e.X;
}
else
{
if (e.Y > 0 && e.Y < Height) SplitterDistance = e.Y;
}
}
else
{
IsDragging = false;
IsSplitterFixed = false;
}
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
if (IsDragging)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, 0, 0, 0)), Orientation == Orientation.Horizontal ? new Rectangle(0, SplitterDistance, Width, SplitterWidth) : new Rectangle(SplitterDistance, 0, SplitterWidth, Height));
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我还尝试了之前提到的其他一些方法,但我仍然得到了焦点线索.
我不认为你所看到的是FocusCue,而是一个用于移动滑块的浮动窗口.
如果键盘访问不重要,您可以尝试使其无法选择:
public class MySplit : SplitContainer {
public MySplit() {
this.SetStyle(ControlStyles.Selectable, false);
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.Clear(Color.Red);
}
}
Run Code Online (Sandbox Code Playgroud)
这可以防止SplitContainer获得焦点,但您的鼠标仍然可以与它进行交互.
SplitContainer的代码如下:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
if (Focused) {
DrawFocus(e.Graphics,SplitterRectangle);
}
}
Run Code Online (Sandbox Code Playgroud)
DrawFocus不是虚拟的。所以你不能覆盖它。
专注是虚拟的。base.OnPaint(...)
也许您可以在调用覆盖时将其设置为 false OnPaint
。
所以你可以添加以下代码(我没有测试它是否有效):
private bool _painting;
public override bool Focused
{
get { return _painting ? false : base.Focused; }
}
protected override void OnPaint(PaintEventArgs e)
{
_painting = true;
try
{
base.OnPaint(e);
}
finally
{
_painting = false;
}
}
Run Code Online (Sandbox Code Playgroud)
这更像是一种黑客行为,而不是一个干净的解决方案。