控制,使形式的拖动 [英] Control that allows dragging of form

查看:124
本文介绍了控制,使形式的拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我给自己建一个控件,我还想让它所以当有人launchs我的应用程序,它们可以在我的控制拖动,就好像他们被拖动应用程序的标题栏。什么是最好的是要做到这一点

Alright, so i built myself a control, and id like to make it so when somebody launchs my app, they can drag on my control just as if they were dragging on the title bar of the application. What is the best was to do this?

我的实验至今:香港专业教育学院使用了与鼠标按下,向上移动事件,并试图decifer当鼠标已被移动和这样的,但我只是无法得到用鼠标

My experiments so far: Ive played with the mousedown,up,move events, and tried to decifer when the mouse has been moved and such, but i just cant get the form to move with the mouse

推荐答案

在除了我其他的答案移动的形式,你可以做这个手动在这样的控制方式:

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天全站免登陆