调用EnableScrollBar时,滚动条闪烁

时间:2010-08-19 18:00:23

标签: c# .net winforms scroll pinvoke

我正在使用p / invoke从EnableScrollBarMSDN reference)拨打user32.dll。我注意到,当启用滚动条时,它似乎绘制好像没有应用主题,然后在应用主题的情况下重新绘制。到目前为止,我只测试过Windows 7。在那儿 有什么方法可以阻止这种情况发生吗?

编辑:这里有一些代码可以显示会发生什么(转储到带有滚动条的表单中):

private class Native
{
    [DllImport("user32.dll")]
    public static extern bool EnableScrollBar(IntPtr hWnd, uint wSBflags, uint wArrows);

    public static class SBArrows
    {
        public const uint ESB_ENABLE_BOTH = 0;
        public const uint ESB_DISABLE_BOTH = 3;
        public const uint ESB_DISABLE_LEFT = 1;
        public const uint ESB_DISABLE_RIGHT = 2;
        public const uint ESB_DISABLE_UP = 1;
        public const uint ESB_DISABLE_DOWN = 2;
        public const uint ESB_DISABLE_LTUP = 1;
        public const uint ESB_DISABLE_RTDN = 2;
    }

    public static class SBFlags
    {
        public const uint SB_HORZ = 0;
        public const uint SB_VERT = 1;
        public const uint SB_CTL = 2;
        public const uint SB_BOTH = 3;
    }
}


private bool Switch = false;

protected override void OnMouseDown(MouseEventArgs e)
{
    Native.EnableScrollBar(this.Handle, Native.SBFlags.SB_HORZ, this.Switch ? Native.SBArrows.ESB_DISABLE_BOTH : Native.SBArrows.ESB_ENABLE_BOTH);
    this.Switch = !this.Switch;
}


最终解决方案

Native.SendMessage(this.Handle, Native.WindowMessages.WM_SETREDRAW, new IntPtr(0), IntPtr.Zero);
Native.EnableScrollBar(this.Handle, Native.SBFlags.SB_HORZ, Native.SBArrows.ESB_ENABLE_BOTH);
Native.SendMessage(this.Handle, Native.WindowMessages.WM_SETREDRAW, new IntPtr(1), IntPtr.Zero);

1 个答案:

答案 0 :(得分:2)

我不太喜欢这个解决方案。但它确实有效:

    protected override void OnMouseDown(MouseEventArgs e) {
        Native.LockWindowUpdate(this.Handle);
        Native.EnableScrollBar(this.Handle, Native.SBFlags.SB_HORZ, this.Switch ? Native.SBArrows.ESB_DISABLE_BOTH : Native.SBArrows.ESB_ENABLE_BOTH);
        //this.Invalidate();
        Native.LockWindowUpdate(IntPtr.Zero);
        this.Switch = !this.Switch;
    }