■ 모서리가 둥근 윈도우 폼을 만드는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 둥근 사각형 영역 생성하기 - CreateRoundRectRgn(left, top, right, bottom, radiusX, radiusY) /// <summary> /// 둥근 사각형 영역 생성하기 /// </summary> /// <param name="left">왼쪽 좌표</param> /// <param name="top">위쪽 좌표</param> /// <param name="right">오른쪽 좌표</param> /// <param name="bottom">아래쪽 좌표</param> /// <param name="radiusX">반지름 X</param> /// <param name="radiusY">반지름 Y</param> /// <returns>처리 결과</returns> [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn(int left, int top, int right, int bottom, int radiusX, int radiusY); #endregion #region 객체 삭제하기 - DeleteObject(objectHandle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="objectHandle">객체 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] private static extern bool DeleteObject(IntPtr objectHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 드래그 여부 /// </summary> private bool isDragging; /// <summary> /// 시작 위치 /// </summary> private Point startPoint; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); FormBorderStyle = FormBorderStyle.None; this.Paint += Form_Paint; this.MouseDown += Form_MouseDown; this.MouseMove += Form_MouseMove; this.MouseUp += Form_MouseUp; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 페인트시 처리하기 - Form_Paint(sender, e) /// <summary> /// 폼 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Paint(object sender, PaintEventArgs e) { IntPtr regionHandle = CreateRoundRectRgn(0, 0, this.Width, this.Height, 50, 50); Region = Region.FromHrgn(regionHandle); DeleteObject(regionHandle); } #endregion #region 폼 마우스 다운시 처리하기 - Form_MouseDown(sender, e) /// <summary> /// 폼 마우스 다운시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseDown(object sender, MouseEventArgs e) { this.startPoint.X = e.X; this.startPoint.Y = e.Y; this.isDragging = true; } #endregion #region 폼 마우스 이동시 처리하기 - Form_MouseMove(sender, e) /// <summary> /// 폼 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseMove(object sender, MouseEventArgs e) { if(this.isDragging) { Point screenPoint = PointToScreen(e.Location); Location = new Point ( screenPoint.X - this.startPoint.X, screenPoint.Y - this.startPoint.Y ); } } #endregion #region 폼 마우스 업시 처리하기 - Form_MouseUp(sender, e) /// <summary> /// 폼 마우스 업시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_MouseUp(object sender, MouseEventArgs e) { this.isDragging = false; } #endregion #region Close 버튼 클릭시 처리하기 - closeButton_Click(sender, e) /// <summary> /// Close 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeButton_Click(object sender, EventArgs e) { Close(); } #endregion } } |