나는 어제이 작업을하고있었습니다. 이것이 최선의 방법인지는 모르겠지만 작동합니다. 드래그를 시작하기 위해 control + mousedown 이벤트를 사용합니다.
자식 컨트롤이 만들어지면 아래에 마우스 이벤트 처리기를 추가합니다.
private void btnNotes_Click(object sender, EventArgs e)
{
if (_editor == null)
{
_editor = new VimControl();
_editor.Location = new Point(400, 200);
_editor.Size = _editor.MinimumSize;
_editor.vimTextBox.Text = "Hello World!";
_editor.vimTextBox.MouseDown += HandleMouseDown;
_editor.vimTextBox.MouseMove += HandleMouseMove;
_editor.vimTextBox.MouseUp += HandleMouseUp;
this.SuspendLayout();
this.Controls.Add(_editor);
_editor.BringToFront();
this.ResumeLayout(true);
}
_editor.Show();
}
#region Drag Child
private Point? _mouseDown = null;
private Point? _mouseLast = null;
private Control frame = null;
/// <summary>
/// Control click to begin drag child.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleMouseDown(object sender, MouseEventArgs e)
{
var child = sender as Control;
Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name);
Log.Message("{0} MouseDown at ({1}, {2})", e.Button, e.X, e.Y);
if (e.Button == MouseButtons.Left && (Control.ModifierKeys & Keys.Control) == Keys.Control)
{
_mouseDown = e.Location;
_mouseLast = e.Location;
frame = FormHelper.FrameControl(_editor.Size, _editor.Location);
this.Controls.Add(frame);
frame.BringToFront();
}
}
private void HandleMouseMove(object sender, MouseEventArgs e)
{
var child = sender as Control;
Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name);
if (child == null) return;
if (_mouseDown.HasValue)
{
Point delta = MyMath.Delta(_mouseLast.Value, e.Location);
frame.Left = frame.Left + delta.X;
frame.Top = frame.Top + delta.Y;
_mouseLast = e.Location;
}
}
private void HandleMouseUp(object sender, MouseEventArgs e)
{
var child = sender as Control;
Log.Message("My {0} MouseUp at ({1}, {2})", e.Button, e.X, e.Y);
if (e.Button == MouseButtons.Left && _mouseDown.HasValue)
{
_mouseDown = null;
_mouseLast = e.Location;
this.SuspendLayout();
{
child.Location = frame.Location;
this.Controls.Remove(frame);
frame.Dispose();
frame = null;
}
this.ResumeLayout(true);
child.Show();
}
}
#endregion
프레임 컨트롤은 드래그하는 동안 아이 에 대한의 의미 단지 빈 사용자 컨트롤입니다. 그것은 끌기를 매우 매끄럽게 만듭니다.
public static UserControl FrameControl(Size size, Point location)
{
UserControl frame = new UserControl();
frame.Location = location;
frame.Size = size;
frame.BorderStyle = BorderStyle.Fixed3D;
frame.BackColor = Color.Transparent;
return frame;
}
델타 도우미 기능은 무엇입니까
public static Point Delta(Point p1, Point p2)
{
return new Point(p2.X - p1.X, p2.Y - p1.Y);
}
_ "C#을 Visual Basic에서"_? – MickyD
나는 그것을 잘못 썼다. 좌절하고주의를 기울이지 않았다. C#을 사용하는 Visual Studio. – Rob