模拟窗口标题栏拖动的控件 [英] Control that simulates dragging of the window title bar

查看:75
本文介绍了模拟窗口标题栏拖动的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个自定义控件,我希望允许人们单击并拖动我的控件,就像拖动窗口标题栏一样。最好的方法是什么?

I've built a custom control, and I'd like to allow people to click and drag on my control just as if they were dragging on the window title bar. What is the best way to do this?

到目前为止,我一直未能成功地利用鼠标的向下,向上移动以及在需要关闭窗口时移动事件来解密

So far I've been unsuccessful at leveraging the mouse down, up, and move events to decipher when the window needs to be moved.

推荐答案

除了我的其他答案,您还可以在这样的控件中手动完成此操作:

In addition to my other answer, you can do this manually in a Control like this:

Point dragOffset;

protected override void OnMouseDown(MouseEventArgs e) {
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left) {
        dragOffset = this.PointToScreen(e.Location);
        var formLocation = FindForm().Location;
        dragOffset.X -= formLocation.X;
        dragOffset.Y -= formLocation.Y;
    }
}

protected override void OnMouseMove(MouseEventArgs e) {
    base.OnMouseMove(e);

    if (e.Button == MouseButtons.Left) {
        Point newLocation = this.PointToScreen(e.Location);

        newLocation.X -= dragOffset.X;
        newLocation.Y -= dragOffset.Y;

        FindForm().Location = newLocation;
    }
}

编辑:经过测试并已修复-现在可以正常使用。

EDIT: Tested and fixed - this now actually works.

这篇关于模拟窗口标题栏拖动的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆