如何让用户以移动控制窗体上 [英] How to allow user to move a control on the form

查看:107
本文介绍了如何让用户以移动控制窗体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForm上,我想允许用户在移动控制。

I have a winform on which i want to allow the user to move a control.

的控制是(目前)的垂直线:标签与边框和宽度为1的

The control is (for now) a vertical line : label with border and a width of 1.

的背景是不是很重要,但我就把它给你反正。我有一些图形的背景下,我想用户能够滑动上面的图形参考。图形是由与NPlots库。它看起来是这样的: <一href="http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png">http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png

The context is not very important but i'll give it to you anyways. I have a background with some graphics and i'd like the user to be able to slide a guideline above the graphics. The graphics are made with the NPlots library. It looks something like this: http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png

如果我能找出用户如何单击并拖动屏幕周围的标签/线控制,我可以解决我的方针的问题。请大家帮帮忙。

If i can find out how the user can click and drag the label/line control around the screen, i can solve my guideline problem. Please help.

推荐答案

在code本可以得到一个有点复杂,但本质上则需要捕捉到你的窗体上的MouseDown,的MouseMove,和MouseUp事件。事情是这样的:

The code for this can get a bit complex, but essentially you will need to capture the MouseDown, MouseMove, and MouseUp events on your form. Something like this:

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
    }
}

这篇关于如何让用户以移动控制窗体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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