stefan27dk
С помощью этого кода Вы можете добавлять панели и перемещать их только в той панели, в которой они развернуты.
//Исковой давности код////::старт::::
// Mouse Button Left Down
if (e.Button == System.Windows.Forms.MouseButtons.Left) // The panel is attached to the mouse and you can move it
{
//Selected Panel Limited location of Bottom and Right
if (this.activeControl.Location.X == Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width) && this.activeControl.Location.Y == Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height))
{
int RightX = Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
int BottomY = Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
activeControl.Location = new Point(RightX, BottomY);
///!LocationRightPanel.Equals(this.activeControl) && this.activeControl.Bounds.IntersectsWith(LocationRightPanel.Bounds)
}
//Selected Panel Limited location of LEFT and TOP
else
{
int LeftX = Math.Min(Math.Max(activeControl.Left + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
int TopY = Math.Min(Math.Max(activeControl.Top + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
activeControl.Location = new Point(LeftX, TopY);
}
}
//////////-----ПОЛНЫЙ КОД::START:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
namespace TaskbarTest
{
public partial class Form1 : Form
{
private Control activeControl;
private Point previousPosition;
Panel TaskPanel;
public Form1()
{
InitializeComponent();
}
// ADD Task - Button
private void add_Task_Button_Click(object sender, EventArgs e)
{
AddTask();
}
// ADD task Method
private void AddTask()
{
TaskPanel = new Panel();//Added task panel
TaskPanel.Location = new System.Drawing.Point(30, 50);//Added panel Location
TaskPanel.MaximumSize = new Size(300, 100);
TaskPanel.Size = new Size(150, 400);
TaskPanel.BackColor = Color.DarkBlue;
TaskPanel.Margin = new Padding(200, 200, 200, 200);
TaskPanel.Padding = new Padding(200);
TaskPanel.Name = "TaskPanel";
// Event Handlers
TaskPanel.MouseDown += new MouseEventHandler(TaskPanel_MouseDown);///Added panel Declaring MouseDown
TaskPanel.MouseMove += new MouseEventHandler(TaskPanel_MouseMove);///Added panel Declaring MouseMove
TaskPanel.MouseUp += new MouseEventHandler(TaskPanel_MouseUp);///Added panel Declaring MouseUp
TaskPanel.LocationChanged += new System.EventHandler(TaskPanel_LocationChanged);
TaskPanel.KeyDown += MovingPanel_KeyDown;
TaskPanel.MouseClick += new MouseEventHandler(TaskPanel_MouseClick);
loader_panel.Controls.Add(TaskPanel);// Add the panel to the loader_panel "Workplace"
activeControl = TaskPanel;//Focused
}
// Mouse Down - "On holding the mouse button"
private void TaskPanel_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousPosition = e.Location;// Start position on panel move
Cursor = Cursors.Hand;
}
// Mouse Move
private void TaskPanel_MouseMove(object sender, MouseEventArgs e)
{
// If it is not sender does not move any panel "The sender is the panel that owns the current MouseEvent"
if (activeControl == null || activeControl != sender)
{
return;//Terminates execution of the method and returns control to the calling method
}
// Mouse Button Left Down
if (e.Button == System.Windows.Forms.MouseButtons.Left) // The panel is attached to the mouse and you can move it
{
//Selected Panel Limited location of Bottom and Right
if (this.activeControl.Location.X == Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width) && this.activeControl.Location.Y == Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height))
{
int RightX = Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
int BottomY = Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
activeControl.Location = new Point(RightX, BottomY);
///!LocationRightPanel.Equals(this.activeControl) && this.activeControl.Bounds.IntersectsWith(LocationRightPanel.Bounds)
}
//Selected Panel Limited location of LEFT and TOP
else
{
int LeftX = Math.Min(Math.Max(activeControl.Left + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
int TopY = Math.Min(Math.Max(activeControl.Top + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
activeControl.Location = new Point(LeftX, TopY);
}
}
}
private void TaskPanel_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;//Disable Control "You can now mouseclick on one dynamic panel to foucus on "Select"
Cursor = Cursors.Default;
}
private void TaskPanel_LocationChanged(object sender, EventArgs e)
{
}
private void MovingPanel_KeyDown(object sender, KeyEventArgs e)
{
}
private void TaskPanel_MouseClick(object sender, MouseEventArgs e)
{
}
}
}