水平或垂直移动控件,而不是两者的组合 [英] Move control either horizontally or vertically, not combination of both
问题描述
我在一些试图解决问题的代码行周围加班加点.
I was here burning the midnight oil around some lines of code trying to solve a problem.
下面的代码会将控件设置为能够移动到其父控件内的任何位置.
The code bellow will setup the control to be able to move anywhere within its parent control.
但是此代码允许向任何方向移动,我想将其移动限制在 X 或 Y 轴,这意味着用户可以水平或垂直移动它,而不是两者的组合.
However this code allows to move in any direction, I would like to restrict its movement to either the X or Y axis, meaning either the user can move it horizontally or vertically, never a combination of both.
Point lastPosition = Point.Empty;
control.MouseDown += (sender, evt) =>
{
lastPosition = evt.Location;
};
control.MouseMove += (sender, evt) =>
{
// This moves the control anywhere.
// I only want to move in one direction (left<->right or top <-> down) never diagonally
// Not sure how to first find which direction the user wants to move,
// nor how to restrict the movement to only one of the directions mentioned
int x = last.X + movingPiece.Left - mouseDownLocation.X;
int y = last.Y + movingPiece.Top - mouseDownLocation.Y;
movingPiece.Left = x;
movingPiece.Top = y;
};
谢谢
感谢 Hans Passant 和 TaW 对于他们的回答,我从中提取了我构建小型 2D 引擎的想法,该引擎能够在考虑碰撞的情况下划定面板容器内可以移动多个控件的区域根据需要检测、预防和控制由多个自定义因素强加的运动方向约束.我想接受这两个答案,但由于这是不可能的,所以我接受了对相关问题提供最深入见解的答案.
My thanks to Hans Passant and TaW for their answers, from which I extracted the ideas from which I built a small 2D engine, capable of delimiting areas on where a number of controls can be moved about inside a panel container, taking into account collision detection, prevention and control movement direction constraints imposed by a number of custom factors as required. I would like to accept both answers, but since this isn't possible, I accepted the answer who provided the most insight into the matter in question.
推荐答案
这是一个例子.我将一个脚本化的小 Panel
'piece' 添加到一个更大的 Panel
'board'.
Here is an example. I add a scripted small Panel
'piece' to a larger Panel
'board'.
我检查了最小增量,这样颤抖的手就不会开始运动..
I check for a minimum delta, so that a shaky hand won't start the movement..
一个标志跟踪运动,另一个标志跟踪方向,0"表示尚未"决定.
One flag tracks the movement, another one the direction, with '0' being 'not yet' decided.
bool pieceMoving = false;
byte pieceDirection = 0;
Point startPosition = Point.Empty;
private void AddPieceButton_Click(object sender, EventArgs e)
{
Panel newPiece = new Panel();
newPiece.Size = new Size(16, 16);
newPiece.BackColor = Color.Blue;
pan_board.Controls.Add(newPiece);
newPiece.MouseDown += (sender2, evt) =>
{ pieceMoving = true; pieceDirection = 0; startPosition = evt.Location; };
newPiece.MouseUp += (sender2, evt) =>
{ pieceMoving = false; pieceDirection = 0;};
newPiece.MouseMove += (sender2, evt) =>
{
int delta = 0;
if (!pieceMoving) return;
if (pieceDirection == 0)
{
int deltaX = Math.Abs(startPosition.X - evt.X);
int deltaY = Math.Abs(startPosition.Y - evt.Y);
delta = deltaX + deltaY;
if (deltaX == deltaY) return;
if (delta < 6) return; // some minimum movement value
if (deltaX > deltaY) pieceDirection = 1; else pieceDirection = 2;
}
// else if (delta == 0) { pieceDirection = 0; return; } // if you like!
Panel piece = (Panel) sender2;
if (pieceDirection == 1) piece.Left += evt.X; else piece.Top += evt.Y;
};
由于我已将代码放在 Button
单击中,因此我将发送方命名为sender2",并使用它来允许将相同的代码用于多个部分.
Since I have put the code in a Button
click, I named the sender 'sender2' and I use it to allow for the same code being used for many pieces.
这篇关于水平或垂直移动控件,而不是两者的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!