加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何允许用户在窗体上移动控件

发布时间:2020-12-15 17:41:15 所属栏目:百科 来源:网络整理
导读:我有一个winform,我想让用户移动一个控件. 该控件(现在)是一条垂直线:带边框且宽度为1的标签. 上下文不是很重要,但无论如何我都会给你.我有一些带有图形的背景,我希望用户能够在图形上方滑动指南.图形由NPlots库制作.它看起来像这样: http://www.ibme.de/p
我有一个winform,我想让用户移动一个控件.

该控件(现在)是一条垂直线:带边框且宽度为1的标签.

上下文不是很重要,但无论如何我都会给你.我有一些带有图形的背景,我希望用户能够在图形上方滑动指南.图形由NPlots库制作.它看起来像这样:
http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png

如果我可以找出用户如何点击并拖动屏幕周围的标签/线控制,我可以解决我的指南问题.请帮忙.

解决方法

这个代码有点复杂,但基本上你需要捕获表单上的MouseDown,MouseMove和MouseUp事件.像这样的东西:
public void Form1_MouseDown(object sender,MouseEventArgs e)
{
    if(e.Button != MouseButton.Left)
        return;

    // Might want to pad these values a bit if the line is only 1px,// might be hard for the user to hit directly
    if(e.Y == myControl.Top)
    {
        if(e.X >= myControl.Left && e.X <= myControl.Left + myControl.Width)
        {
            _capturingMoves = true;
            return;
        }
    }

    _capturingMoves = false;
}

public void Form1_MouseMove(object sender,MouseEventArgs e) 
{
    if(!_capturingMoves)
        return;

    // Calculate the delta's and move the line here
}

public void Form1_MouseUp(object sender,MouseEventArgs e) 
{
    if(_capturingMoves)
    {
        _capturingMoves = false;
        // Do any final placement
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读