private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private void ItemGrid_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = ItemGrid.DoDragDrop(ItemGrid.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
}
}
}
private void ItemGrid_MouseDown(object sender, MouseEventArgs e)
{
rowIndexFromMouseDown = ItemGrid.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
dragBoxFromMouseDown = Rectangle.Empty;
}
private void ItemGrid_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = ItemGrid.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = ItemGrid.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
Item item = ItemList[rowIndexFromMouseDown];
ItemList.RemoveAt(rowIndexFromMouseDown);
ItemList.Insert(rowIndexOfItemUnderMouseToDrop, item);
}
ItemGrid.Rows[rowIndexOfItemUnderMouseToDrop].Selected = true; // 드래그해서 놓은 위치에 선택
}
private void ItemGrid_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
'C# Programming' 카테고리의 다른 글
C# datagridview 마우스로 Row 이동 (0) | 2019.09.25 |
---|---|
C# 마우스 드래그로 폼 이동하기 (0) | 2019.09.22 |
C# 프로그레스바 색 변경하기 (0) | 2019.07.19 |
C# combobox 마우스휠 막기 (0) | 2019.07.08 |
C# 텍스트 읽어주는 SpeechSynthesizer Class (0) | 2019.06.17 |