移动无边框的Winform持有鼠标右键,可能使用本地方法 [英] Move a borderless Winform holding right mouse button, possibly with native methods

查看:291
本文介绍了移动无边框的Winform持有鼠标右键,可能使用本地方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,我想握住它的客户区鼠标右键移动窗口的形式;形式是无国界的,因为我已经说。



我想移动它本身(如果可能的话,否则其他的答案都OK太)。我的意思是它的行为方式,当你按住标题栏鼠标左键(使用鼠标移动之类的东西,我得到了很多奇怪的行为,但也许这只是我)。



我看过周围很多东西和这个职位看起来有用



http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b9985b19 -cab5-4fba-9dc5-f323d0d37e2f /



我尝试了各种方法来使用并通过的 http://msdn.microsoft.com/en-us/library/ff468877%28v=VS.85 %29.aspx 找到其他有用的东西,并WM_NCRBUTTONDOWN就在我的脑海,但WndProc中没有检测到,也许是因为它是由表格处理?



任何建议表示赞赏,感谢



弗朗西斯


解决方案

 公共部分类DragForm:表
{
//从表格左上角,其中鼠标抓住
私人尺寸偏差? _mouseGrabOffset;

公共DragForm()
{
的InitializeComponent();
}

保护覆盖无效onmousedown事件(MouseEventArgs E)
{
如果(e.Button == System.Windows.Forms.MouseButtons.Right)
_mouseGrabOffset =新的大小(e.Location);

base.OnMouseDown(E);
}

保护覆盖无效OnMouseUp(MouseEventArgs E)
{
_mouseGrabOffset = NULL;

base.OnMouseUp(E);
}

保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
如果(_mouseGrabOffset.HasValue)
{
this.Location =光标.POSITION - _mouseGrabOffset.Value;
}

base.OnMouseMove(E);
}
}


I have a situation where I would like to move a windows form by holding right mouse button on it's client area; the form it's borderless as i've stated.

I would like to move it "natively" (if possible, otherwise other answers are ok too). I mean the way it behaves when you hold left mouse button on the titlebar (with mouse-move and things like that I get a lot of strange behaviours, but maybe it's just me).

I've read around a lot of things and this post looks helpful

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b9985b19-cab5-4fba-9dc5-f323d0d37e2f/

I tried various way to use that and watched through http://msdn.microsoft.com/en-us/library/ff468877%28v=VS.85%29.aspx to find other useful things and WM_NCRBUTTONDOWN came in my mind, however the wndproc doesn't detect it, maybe because it's handled by the form?

Any suggestion are appreciated, thanks

Francesco

解决方案

public partial class DragForm : Form
{
    // Offset from upper left of form where mouse grabbed
    private Size? _mouseGrabOffset;

    public DragForm()
    {
        InitializeComponent();
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if( e.Button == System.Windows.Forms.MouseButtons.Right )
            _mouseGrabOffset = new Size(e.Location);

        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        _mouseGrabOffset = null;

        base.OnMouseUp(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (_mouseGrabOffset.HasValue)
        {
            this.Location = Cursor.Position - _mouseGrabOffset.Value;
        }

        base.OnMouseMove(e);
    }
}

这篇关于移动无边框的Winform持有鼠标右键,可能使用本地方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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