1) 상하좌우 크기 조절 기능.
2) 최대화, 최소화, 종료 버튼
3) 타이틀 버튼 설정 (마우스 클릭, 더블 클릭)
TestForm (FormParent 상속 받아서 기능 사용.)
namespace FormStyleNoneResize
{
public partial class TestForm : FormParent
{
public TestForm()
{
InitializeComponent();
this.SetTitle(label1); //타이틀 (마우스 움직임 등 설정)
this.SetCloseButton(button1); //닫기 이벤트 추가
}
}
}
FormParent Class
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormStyleNoneResize
{
public partial class FormParent : Form
{
//https://docs.microsoft.com/ko-kr/windows/win32/inputdev/wm-nchittest
private const int WM_NCHITTEST = 0x84; //마우스 커서 위치가 크기 조절 가능한 부분에 있는 경우
private const int HTCLIENT = 1;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int GRIP = 5; //리사이즈 잡는 영역 크기
private Point prePosition;
private Point mouseDownPoint;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if ((int)m.Result == HTCLIENT)
{
Point position = this.PointToClient(Cursor.Position);
bool left = position.X <= GRIP;
bool right = this.Width - GRIP <= position.X;
bool top = position.Y <= GRIP;
bool bottom = this.Height - GRIP <= position.Y;
if (left && top) m.Result = (IntPtr)HTTOPLEFT;
else if (right && top) m.Result = (IntPtr)HTTOPRIGHT;
else if (left && bottom) m.Result = (IntPtr)HTBOTTOMLEFT;
else if (right && bottom) m.Result = (IntPtr)HTBOTTOMRIGHT;
else if (left) m.Result = (IntPtr)HTLEFT;
else if (right) m.Result = (IntPtr)HTRIGHT;
else if (top) m.Result = (IntPtr)HTTOP;
else if (bottom) m.Result = (IntPtr)HTBOTTOM;
}
return;
}
base.WndProc(ref m); //다른 처리를 위한 기존 콜백 다시 호출해주기
}
public FormParent()
{
InitializeComponent();
}
//마우스 이벤트가 발생할 타이틀바를 등록합니다.
protected void SetTitle(Control titleControl)
{
titleControl.MouseDoubleClick += new MouseEventHandler(titleControl_MouseDoubleClick);
titleControl.MouseDown += new MouseEventHandler(titleControl_MouseDown);
titleControl.MouseMove += new MouseEventHandler(titleControl_MouseMove);
}
//최소화 버튼을 등록합니다.
protected void SetMinimumButton(Button button)
{
button.Click += new EventHandler(this.button_minimum_Click);
}
//최대화 버튼을 등록합니다.
protected void SetMaximumButton(Button button)
{
button.Click += new EventHandler(this.button_maximum_Click);
}
protected void SetCloseButton(Button button)
{
button.Click += new EventHandler(this.button_close_Click);
}
private void titleControl_MouseDoubleClick(object sender, MouseEventArgs e) //상단 타이틀 더블 클릭
{
if (e.Button != MouseButtons.Left) //왼쪽 버튼만 사용
{
return;
}
if (WindowState != FormWindowState.Maximized)
{
SetMaximize();
}
else
{
SetNormal();
}
}
private void titleControl_MouseDown(object sender, MouseEventArgs e) //상단 타이틀 클릭 시 움직일 준비
{
if (e.Button != MouseButtons.Left) //왼쪽 버튼만 사용
{
return;
}
mouseDownPoint = new Point(e.X, e.Y);
}
private void titleControl_MouseMove(object sender, MouseEventArgs e) //상단 타이틀을 통한 이동
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) //왼쪽 버튼이 눌린 경우만 움직여줍니다.
{
this.Left += e.X - mouseDownPoint.X;
this.Top += e.Y - mouseDownPoint.Y;
}
}
private void button_minimum_Click(object sender, EventArgs e) //최소화 버튼
{
WindowState = FormWindowState.Minimized;
}
private void button_maximum_Click(object sender, EventArgs e) //최대화 버튼
{
if (WindowState != FormWindowState.Maximized)
{
SetMaximize();
}
else
{
SetNormal();
}
}
private void SetNormal() //기존 사이즈로 변경
{
this.Location = prePosition; //크기 변경 시 원 위치로 복귀
this.WindowState = FormWindowState.Normal;
}
private void SetMaximize() //최대화
{
Screen screen = Screen.FromControl(this); //현재 Form과 가장 가까운 Screen 찾기.
prePosition = this.Location; //노멀화 복귀 좌표값
this.Location = screen.Bounds.Location; //위치 지정
this.MaximizedBounds = new Rectangle(0, 0, screen.WorkingArea.Width, screen.WorkingArea.Height); //최대화 크기 변경
this.WindowState = FormWindowState.Maximized;
}
//종료 버튼
private void button_close_Click(object sender, EventArgs e)
{
Close();
}
}
}'C# > Windows Form' 카테고리의 다른 글
| [C# Windows Form] PrivateFontCollection 외부 폰트 파일 적용 (1) | 2025.11.28 |
|---|---|
| [C# Windows Form] Custom Button Class - 이미지 버튼 생성 (hover, press, normal, focus) (0) | 2025.11.27 |
| [C# Windows Form] 섬네일 만들기 (ListView, ListBox) (0) | 2025.11.17 |
| [C# Windows Form] ZipFile 다루기 - 압축 및 해제 (0) | 2025.10.06 |
| [C# Windows Form] 비동기 Custom Form 종료, 파일 열기, 프린트 등 창 닫기 (BlockingCollection - queue) (0) | 2025.09.08 |