我正在尝试制作一个带有滑块拇指自定义图形的轨迹栏。我从以下代码开始:
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但解决方案是从头开始完全重新实现控制。我只是想稍微修改一下现有的控件。
如果您想在轨迹栏的顶部进行绘制,您可以手动捕获 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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)