获取MDI客户端水平滚动值

时间:2016-08-30 16:13:02

标签: c# winforms

图像:

enter image description here

如何在将MDI子窗体拖出边框时获取MDI客户端水平滚动值和其他参数?

或者我怎样才能获得MDI子窗体绝对坐标包括MDI客户端的滚动部分?

1 个答案:

答案 0 :(得分:0)

仅找到此解决方案:

    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetScrollPos(IntPtr hWnd, int nBar);
    public int hpos = 0;
    void MdiClient_Scroll(object sender, ScrollEventArgs e)
    {
        hpos += e.NewValue; 
        Console.WriteLine(hpos);
    }
    private MdiClientWrapper wrapper;
    protected override void OnHandleCreated(EventArgs e)
    {
        // Find the MdiClient and sub-class it so we can get the Scroll event
        base.OnHandleCreated(e);
        if (wrapper != null) wrapper.Scroll -= MdiClient_Scroll;
        var client = this.Controls.OfType<MdiClient>().First();
        wrapper = new MdiClientWrapper();
        wrapper.AssignHandle(client.Handle);
        wrapper.Scroll += MdiClient_Scroll;
    }

    private class MdiClientWrapper : NativeWindow
    {
        public event ScrollEventHandler Scroll;
        private int oldPos;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x114)
            {   // Trap WM_HSCROLL
                var type = (ScrollEventType)(m.WParam.ToInt32() & 0xffff);
                var pos = GetScrollPos(this.Handle, SBS_HORZ);//m.WParam.ToInt32() >> 16;
                Scroll(this, new ScrollEventArgs(type, oldPos, pos));
                oldPos = pos;
            }
            base.WndProc(ref m);
        }
    }

现在水平滚动位置始终位于hpos变量中。