如何在WinForms中制作ownerdraw Trackbar

发布于 2024-08-07 04:40:03 字数 723 浏览 8 评论 0原文

我正在尝试制作一个带有滑块拇指自定义图形的轨迹栏。我从以下代码开始:

namespace testapp
{
    partial class MyTrackBar : System.Windows.Forms.TrackBar
    {
        public MyTrackBar()
        {
            InitializeComponent();
        }

        protected override void  OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
        //   base.OnPaint(e);
            e.Graphics.FillRectangle(System.Drawing.Brushes.DarkSalmon, ClientRectangle);
        }
    }
}

但它从不调用 OnPaint。还有其他人遇到过这个吗?我之前使用过这种技术来创建一个ownerdraw 按钮,但由于某种原因它不能与TrackBar 一起使用。

附言。是的,我已经看到问题#625728,但解决方案是完全重新-从头开始实施控制。我只是想稍微修改一下现有的控件。

I'm trying to make a trackbar with a custom graphic for the slider thumb. I have started out with the following code:

namespace testapp
{
    partial class MyTrackBar : System.Windows.Forms.TrackBar
    {
        public MyTrackBar()
        {
            InitializeComponent();
        }

        protected override void  OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
        //   base.OnPaint(e);
            e.Graphics.FillRectangle(System.Drawing.Brushes.DarkSalmon, ClientRectangle);
        }
    }
}

But it never calls OnPaint. Anyone else come across this? I have used this technique before to create an ownerdraw button but for some reason it doesn't work with TrackBar.

PS. Yes, I have seen question #625728 but the solution there was to completely re-implement the control from scratch. I just want to modify the existing control a little.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-08-14 04:40:03

如果你想在轨迹栏的顶部绘制,你可以手动捕获 WM_PAINT 消息,这意味着你不必自己重写所有绘制代码​​,只需绘制它,如下所示:

using System.Drawing;
using System.Windows.Forms;

namespace TrackBarTest
{
    public class CustomPaintTrackBar : TrackBar
    {
        public event PaintEventHandler PaintOver;

        public CustomPaintTrackBar()
            : base()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // WM_PAINT
            if (m.Msg == 0x0F) 
            {
                using(Graphics lgGraphics = Graphics.FromHwndInternal(m.HWnd))
                    OnPaintOver(new PaintEventArgs(lgGraphics, this.ClientRectangle));
            }
        }

        protected virtual void OnPaintOver(PaintEventArgs e)
        {
            if (PaintOver != null) 
                PaintOver(this, e);

            // Paint over code here
        }
    }
}

If you want to paint over the top of the trackbar you can capture the WM_PAINT message manually, this means you dont have to re-write all the painting code yourself and can simply paint it, like this:

using System.Drawing;
using System.Windows.Forms;

namespace TrackBarTest
{
    public class CustomPaintTrackBar : TrackBar
    {
        public event PaintEventHandler PaintOver;

        public CustomPaintTrackBar()
            : base()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // WM_PAINT
            if (m.Msg == 0x0F) 
            {
                using(Graphics lgGraphics = Graphics.FromHwndInternal(m.HWnd))
                    OnPaintOver(new PaintEventArgs(lgGraphics, this.ClientRectangle));
            }
        }

        protected virtual void OnPaintOver(PaintEventArgs e)
        {
            if (PaintOver != null) 
                PaintOver(this, e);

            // Paint over code here
        }
    }
}
风吹雨成花 2024-08-14 04:40:03

我通过在构造函数中设置 UserPaint 样式解决了这个问题,如下所示:

public MyTrackBar()
{
    InitializeComponent();
    SetStyle(ControlStyles.UserPaint, true);
}

OnPaint 现在被调用。

I've solved it by setting the UserPaint style in the constructor like so:

public MyTrackBar()
{
    InitializeComponent();
    SetStyle(ControlStyles.UserPaint, true);
}

OnPaint now gets called.

非要怀念 2024-08-14 04:40:03

在这个答案中,PaintOver 永远不会被调用,因为它从未被分配,它的值为 null。

In this answer PaintOver is never called, because it is never assigned, its value is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文