如何在WinForms中制作ownerdraw Trackbar
我正在尝试制作一个带有滑块拇指自定义图形的轨迹栏。我从以下代码开始:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想在轨迹栏的顶部绘制,你可以手动捕获 WM_PAINT 消息,这意味着你不必自己重写所有绘制代码,只需绘制它,如下所示:
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:
我通过在构造函数中设置 UserPaint 样式解决了这个问题,如下所示:
OnPaint 现在被调用。
I've solved it by setting the UserPaint style in the constructor like so:
OnPaint now gets called.
在这个答案中,PaintOver 永远不会被调用,因为它从未被分配,它的值为 null。
In this answer PaintOver is never called, because it is never assigned, its value is null.