我有一个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 } }