C#:重写的OnPaint上进度不工作? [英] C#: Overriding OnPaint on ProgressBar not working?

查看:1187
本文介绍了C#:重写的OnPaint上进度不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想这应该是pretty容易地创建一个进度是在自身画了一些文字。不过,我不太清楚这里发生了什么......

Was thinking it should be pretty easy to create a ProgressBar that drew some text upon itself. However, I am not quite sure what is happening here...

我添加了以下两个替代:

I added the following two overrides:

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        base.OnPaintBackground(pevent);
        var flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
        TextRenderer.DrawText(pevent.Graphics, "Hello", Font, Bounds, Color.Black, flags);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        var flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
        TextRenderer.DrawText(e.Graphics, "Hello", Font, Bounds, Color.Black, flags);
    }

不过,我没有得到任何的文本,并且这些方法并不甚至似乎被调用。这到底是怎么回事呢?

However, I get no text, and the methods doesn't even seem to be called. What is going on here?


更新:多亏了两个答案,到目前为止,我已经得到了它实际调用的OnPaint 使用的SetStyle(ControlStyles.UserPaint,真),我得到它在<​​code>新的Rectangle(0,0,宽度,高度)发送绘制在正确的地方文本而不是边界

Update: Thanks to the two answers so far, I have gotten it to actually call the OnPaint by using SetStyle(ControlStyles.UserPaint, true), and I have gotten it to draw the text in the right place by sending in new Rectangle(0, 0, Width, Height) instead of Bounds.

我现在得到的文本,但进度走了......和点是那种会对顶部的文字进度。任何想法我怎么能解决这个问题?

I do get text now, but the ProgressBar is gone... and the point was kind of to have the text on top of the ProgressBar. Any idea how I can solve this?

推荐答案

您的问题是,你的边界作为传递您的矩形参数。 bounds,其中包含你的控制,这是你想要什么样的高度和宽度,但它也包含了顶部和相对父窗体控件的左属性,让你的你好正在上的控制由然而很多抵消你控制它的父窗体上抵消。

Your problem is that you're passing in Bounds as your Rectangle parameter. Bounds contains the Height and Width of your control, which is what you want, but it also contains the Top and Left properties of your control, relative to the parent form, so your "Hello" is being offset on the control by however much your control is offset on its parent form.

替换边界新的Rectangle(0,0,this.Width,this.Height)键,你应该看看你的你好。

Replace Bounds with new Rectangle(0, 0, this.Width, this.Height) and you should see your "Hello".

这篇关于C#:重写的OnPaint上进度不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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