来自WinForms C的SCROLLINFO PInvoke#

时间:2015-02-17 14:52:39

标签: c# .net winforms scrollbar pinvoke

我正在用滚动条编写一个控件,我希望它像RichTextBox一样表现(就其滚动条而言);即当栏被“强迫”时,我希望它们在必要时被禁用。我有这种工作(有点),但在构建项目之前,我在Windows窗体设计器中看不到结果。

代码:

namespace WindowsFormsApplication1
{
    public class ScrollControl : Control
    {
        public const int WS_HSCROLL = 0x00100000;
        public const int WS_VSCROLL = 0x00200000;

        private RichTextBoxScrollBars sb;

        public RichTextBoxScrollBars ScrollBars
        {
            get
            {
                return this.sb;
            }

            set
            {
                this.sb = value;
                this.UpdateScrollBars();
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams result = base.CreateParams;
                result.Style |= WS_HSCROLL;
                result.Style |= WS_VSCROLL;
                return result;
            }
        }

        private void UpdateScrollBars()
        {
            ScrollOrientation dirH = ScrollOrientation.HorizontalScroll;
            ScrollInfoMask maskH = ScrollInfoMask.SIF_ALL;

            ScrollOrientation dirV = ScrollOrientation.VerticalScroll;
            ScrollInfoMask maskV = ScrollInfoMask.SIF_ALL;

            if (this.ScrollBars == RichTextBoxScrollBars.ForcedVertical || this.ScrollBars == RichTextBoxScrollBars.ForcedBoth)
            {
                maskV |= ScrollInfoMask.SIF_DISABLENOSCROLL;
            }

            if(this.ScrollBars == RichTextBoxScrollBars.ForcedHorizontal || this.ScrollBars == RichTextBoxScrollBars.ForcedBoth)
            {
                maskH |= ScrollInfoMask.SIF_DISABLENOSCROLL;
            }

            SCROLLINFO sV = new SCROLLINFO();
            SCROLLINFO sH = new SCROLLINFO();

            sV.cbSize = (uint)Marshal.SizeOf<SCROLLINFO>();
            sH.cbSize = (uint)Marshal.SizeOf<SCROLLINFO>();

            sV.fMask = (uint)maskV;
            sH.fMask = (uint)maskH;

            SetScrollInfo(this.Handle, (int)dirV, ref sV, true);
            SetScrollInfo(this.Handle, (int)dirH, ref sH, true);
        }

        [DllImport("user32.dll")]
        private static extern int SetScrollInfo(IntPtr hwnd, int fnBar, [In] ref SCROLLINFO lpsi, bool fRedraw);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int nMin;
        public int nMax;
        public uint nPage;
        public int nPos;
        public int nTrackPos;
    }

    public enum ScrollInfoMask
    {
        SIF_RANGE = 0x0001,
        SIF_PAGE = 0x0002,
        SIF_POS = 0x0004,
        SIF_DISABLENOSCROLL = 0x0008,
        SIF_TRACKPOS = 0x0010,
        SIF_ALL = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS
    }
}

在这里,我已经构建了控件并将其加载到一个表单中(表单上的16x填充以清楚地显示控件的边界)

Image 1

在这里,我已将ScrollBars属性设置为ForcedVertical,但在构建解决方案之前,此不会显示

Image 2

在这里,我选择了ForcedBoth,这个仅在构建后显示

Image 3

问题:

如何让Designer立即更新,以便我可以在不需要不断构建的情况下查看我的更改?

注意:

请原谅可怜的代码质量(可变的结构,可怕的名字等) - 这只是一个示范!

我也不想使用ScrollableControl ......太糟糕了!我需要完全控制ScrollBars - Win32的意图!

.Invalidate() .Update().Refresh()不起作用!

2 个答案:

答案 0 :(得分:2)

我认为你必须重新创建窗口才能做到这一点。试试这种方式:

public RichTextBoxScrollBars ScrollBars {
  get {
    return this.sb;
  }
  set {
    this.sb = value;
    this.RecreateHandle();
  }
}

protected override void OnHandleCreated(EventArgs e) {
  base.OnHandleCreated(e);
  UpdateScrollBars();
}

答案 1 :(得分:0)

最有可能的是,您需要强制重新绘制控件,即在UpdateScrollBars方法的末尾添加Invalidate();

编辑:以下内容适用于我(在UpdateScrollBars末尾):

Invalidate();
Update();