gpt4 book ai didi

c# - 如何允许用户移动窗体上的控件

转载 作者:太空狗 更新时间:2023-10-29 20:02:03 25 4
gpt4 key购买 nike

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

关于c# - 如何允许用户移动窗体上的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3781245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com