C#WinForms中的自定义绘画拆分器控件 [英] Custom paint Splitter control in C# WinForms

查看:202
本文介绍了C#WinForms中的自定义绘画拆分器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制拖动拆分器控件时出现的拆分线:

I am trying to paint the split line that appears when you drag a splitter control:

从这张图片中可以看到,默认的拆分器是棋盘格.

As you can see from this image, the default splitter is a checkerboard.

...这不起作用:

public partial class MockForm : Form
{
    public MockForm()
    {
        InitializeComponent();
        this.splitter1.Paint += splitter1_Paint;
    }

    private void splitter1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.Clear(Color.Red);
    }
}

这只是绘制控件的背景,而不是拖动拖动时的拆分器.

this just paints the background of the control but not the splitter when it's dragged.

有什么想法吗?

推荐答案

LarsTech发布的答案确实不错,但是处理程序的闪烁某种程度上令人讨厌.如果不显示Form中的控件,而是将Form显示为拆分器处理程序并将其显示在拆分器的Container上方,则闪烁将消失.

The answer posted by LarsTech is really good, But the handler flickers are somehow annoying. Instead of showing the control in Form, if you show a Form as splitter handler and show it above the Container of splitter, the flickers will be gone.

HighLight f = new HighLight() { BackColor = Color.Red };
private void splitter1_SplitterMoving(object sender, SplitterEventArgs e)
{
    this.splitter1.Parent.Refresh();
    f.Location = this.splitter1.Parent.PointToScreen(new Point(e.SplitX, e.SplitY));
    f.Size = this.splitter1.Size;
    if (!f.Visible)
        f.ShowInactiveTopmost();
}
private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)
{
    f.Hide();
}

以下是我用来突出显示的表格:

Here is the form which I used as highlight:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class HighLight : Form
{
    public HighLight()
    {
        Opacity = 0;
        FormBorderStyle = FormBorderStyle.None;
        ShowInTaskbar = false;
        StartPosition = FormStartPosition.Manual;
    }
    protected override void OnDeactivate(EventArgs e)
    {
        base.OnDeactivate(e);
        this.Hide();
    }
    private const int SW_SHOWNOACTIVATE = 4;
    private const int HWND_TOPMOST = -1;
    private const uint SWP_NOACTIVATE = 0x0010; 
    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    static extern bool SetWindowPos(int hWnd, int hWndInsertAfter,
         int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    public void ShowInactiveTopmost()
    {
        ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
        SetWindowPos(this.Handle.ToInt32(), HWND_TOPMOST,
        this.Left, this.Top, this.Width, this.Height,
        SWP_NOACTIVATE);
        this.Opacity = 1;
    }
}

要查看支持透明处理程序的自定义拆分器,请查看此相关文章.在另一篇文章中,我使用原始拆分器的源代码创建了一个新的拆分器控件,但是更改了突出显示的显示方式:

To see a custom splitter which supports transparent handler take a look at this related post. In the other post I created a new splitter control using source codes of original splitter, but changed rendering the highlight:

这篇关于C#WinForms中的自定义绘画拆分器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆