如何在WinForms中制作ownerdraw轨迹栏 [英] How to make an ownerdraw Trackbar in WinForms

查看:72
本文介绍了如何在WinForms中制作ownerdraw轨迹栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有自定义图形的滑块创建跟踪栏.我开始使用以下代码:

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);
        }
    }
}

但是它从不调用OnPaint.还有其他人遇到吗?在创建ownerdraw按钮之前,我曾使用过此技术,但由于某种原因,它不适用于TrackBar.

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.是的,我看到了问题# 625728 ,但是解决方案是完全重新-从头实现控件.我只想对现有控件进行一些修改.

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.

推荐答案

我已经通过在构造函数中设置UserPaint样式来解决它,如下所示:

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

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

现在会调用OnPaint.

OnPaint now gets called.

这篇关于如何在WinForms中制作ownerdraw轨迹栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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