1. 소스
/// <summary>
/// WinForm에서 사용자가 정의한 타이틀바에 의해서 폼을 이동 한다.
/// </summary>
/// <param name="form">폼</param>
/// <param name="triggerControls">사용자정의 타이틀바에 속해 있는 컨트롤목록</param>
/// <param name="movingCursorOrNull">마우스커서</param>
void SetMovingForm(this Form form, Control[] triggerControls, Cursor movingCursorOrNull = null)
{
bool mouseDown = false;
Point lastMousePoint = new Point();
Cursor movingCursor;
if (movingCursorOrNull == null)
movingCursor = Cursors.NoMove2D;
else
movingCursor = movingCursorOrNull;
Dictionary<Control, Cursor> oldCursors = new Dictionary<Control, Cursor>();
foreach (var item in triggerControls)
{
Control con = item;
try
{
con.MouseDown += (s, e) =>
{
var thisControl = s as Control;
mouseDown = true;
if (!oldCursors.ContainsKey(item))
oldCursors.Add(item, thisControl.Cursor);
else
oldCursors[item] = thisControl.Cursor;
thisControl.Cursor = movingCursor;
lastMousePoint = Control.MousePosition;
};
con.MouseMove += (s, e) =>
{
if (!mouseDown)
return;
Point currentPoint = Control.MousePosition;
int x = currentPoint.X - lastMousePoint.X;
int y = currentPoint.Y - lastMousePoint.Y;
form.Location = new Point(form.Location.X + x, form.Location.Y + y);
lastMousePoint = currentPoint;
};
con.MouseUp += (s, e) =>
{
var thisControl = s as Control;
mouseDown = false;
if (oldCursors != null && oldCursors.ContainsKey(thisControl))
thisControl.Cursor = oldCursors[thisControl];
};
}
catch (Exception ex)
{
KLog.Write(ex.ToString());
}
}
}
2. 사용예제
SetMovingForm(this, new Control[] { this, this.LBL_TITLE, Label1 }, Cursors.Hand);
'유용한 정보' 카테고리의 다른 글
[C#] DataGridView에서 행이 비어있는지 체크 (0) | 2025.04.16 |
---|---|
[C#] TextBox 컨트롤에 수직스크롤바가 있는지의 여부 판단하기 (0) | 2025.04.16 |
[C#] 문자입력컨트롤에 워터마크 지원 (0) | 2025.04.16 |
[C#] 파일 오픈 및 읽기/쓰기 (0) | 2025.04.15 |
[C#] RDLC 리포트 (0) | 2025.04.08 |