单击中间按钮以滚动

时间:2012-11-06 14:24:18

标签: c# winforms mouse

当滚动条可见时,如何在WinForms容器控件中实现此功能?

此处突出显示(Google Chrome浏览器):

enter image description here

编辑:此光标是屏幕截图中唯一可见的光标。我希望我的意思很清楚。

编辑: 在我的控制下尝试了这个。不起作用。

    const int WM_MBUTTONDOWN = 0x207;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_MBUTTONDOWN)
            DefWndProc(ref m);
        else
            base.WndProc(ref m);
    }

2 个答案:

答案 0 :(得分:2)

这是我到目前为止所拥有的。如果我释放中间按钮,它退出“阅读器模式”,并且我没有在控件中实现滚动(我使用了文本框),但它可能会给你一些东西。

    [DllImport("comctl32.dll", SetLastError=true,  EntryPoint="#383")]
    static extern void DoReaderMode(ref READERMODEINFO prmi);

    public delegate bool TranslateDispatchCallbackDelegate(ref MSG lpmsg);
    public delegate bool ReaderScrollCallbackDelegate(ref READERMODEINFO prmi, int dx, int dy);

    [StructLayout(LayoutKind.Sequential)]
    public struct READERMODEINFO
    {
        public int cbSize;
        public IntPtr hwnd;
        public int fFlags;
        public IntPtr prc;
        public ReaderScrollCallbackDelegate pfnScroll;
        public TranslateDispatchCallbackDelegate fFlags2;
        public IntPtr lParam;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public UInt32 message;
        public IntPtr wParam;
        public IntPtr lParam;
        public UInt32 time;
        public POINT pt;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct RECT
    {
        public int left, top, right, bottom;
    }

    private bool TranslateDispatchCallback(ref MSG lpMsg)
    {
        return false;
    }
    private bool ReaderScrollCallback(ref READERMODEINFO prmi, int dx, int dy)
    {
        // TODO: Scroll around within your control here

        return false;
    }

    private void EnterReaderMode()
    {
        READERMODEINFO readerInfo = new READERMODEINFO
        {
            hwnd = this.textBox1.Handle,
            fFlags = 0x00,
            prc = IntPtr.Zero,
            lParam = IntPtr.Zero,
            fFlags2 = new TranslateDispatchCallbackDelegate(this.TranslateDispatchCallback),
            pfnScroll = new ReaderScrollCallbackDelegate(this.ReaderScrollCallback)
        };
        readerInfo.cbSize = Marshal.SizeOf(readerInfo);

        DoReaderMode(ref readerInfo);
    }

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Middle)
        {
            EnterReaderMode();
        }
    }

答案 1 :(得分:0)

当您按下鼠标滚轮按钮时,RichTextBox控件默认执行此操作。

编辑:对不起,我误解了,并且认为你问的是在文本框中而不是容器控件中进行此操作