■ Form 클래스에서 투명 폼을 만드는 방법을 보여준다.
▶ TransformForm.cs
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 투명 폼 /// </summary> public partial class TransformForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 레이어 윈도우 업데이트 하기 - UpdateLayeredWindow(windowHandle, targetDCHandle, targetPoint, targetSize, sourceDCHandle, sourcePoint, keyColor, blendFunction, flag); /// <summary> /// 레이어 윈도우 업데이트 하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="targetDCHandle">타겟 디바이스 컨텍스트 핸들</param> /// <param name="targetPoint">타겟 포인트</param> /// <param name="targetSize">타겟 크기</param> /// <param name="sourceDCHandle">소스 디바이스 컨텍스트 핸들</param> /// <param name="sourcePoint">소스 포인트</param> /// <param name="keyColor">키 색상</param> /// <param name="blendFunction">블렌드 함수</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UpdateLayeredWindow ( IntPtr windowHandle, IntPtr targetDCHandle, ref Point targetPoint, ref Size targetSize, IntPtr sourceDCHandle, ref Point sourcePoint, int keyColor, ref BlendFunction blendFunction, int flag ); #endregion #region 호환 디바이스 컨텍스트 생성하기 - CreateCompatibleDC(dcHandle) /// <summary> /// 호환 디바이스 컨텍스트 생성하기 /// </summary> /// <param name="dcHandle">디바이스 컨텍스트 핸들</param> /// <returns>호환 디바이스 컨텍스트</returns> [DllImport("gdi32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CreateCompatibleDC(IntPtr dcHandle); #endregion #region 디바이스 컨텍스트 구하기 - GetDC(windowHandle) /// <summary> /// 디바이스 컨텍스트 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>디바이스 컨텍스트 핸들</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetDC(IntPtr windowHandle); #endregion #region 디바이스 컨텍스트 해제하기 - ReleaseDC(windowHandle, dcHandle) /// <summary> /// 디바이스 컨텍스트 해제하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="dcHandle">디바이스 컨텍스트 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] private static extern int ReleaseDC(IntPtr windowHandle, IntPtr dcHandle); #endregion #region 디바이스 컨텍스트 삭제하기 - DeleteDC(dcHandle) /// <summary> /// 디바이스 컨텍스트 삭제하기 /// </summary> /// <param name="dcHandle">디바이스 컨텍스트 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool DeleteDC(IntPtr dcHandle); #endregion #region 객체 선택하기 - SelectObject(dcHandle, objectHandle) /// <summary> /// 객체 선택하기 /// </summary> /// <param name="dcHandle">디바이스 컨텍스트 핸들</param> /// <param name="objectHandle">객체 핸들</param> /// <returns>이전 객체 핸들</returns> [DllImport("gdi32", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SelectObject(IntPtr dcHandle, IntPtr objectHandle); #endregion #region 객체 삭제하기 - DeleteObject(objectHandle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="objectHandle">객체 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool DeleteObject(IntPtr objectHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WS_EX_LAYERED /// </summary> private const int WS_EX_LAYERED = 0x80000; /// <summary> /// HTCAPTION /// </summary> private const int HTCAPTION = 0x02; /// <summary> /// WM_NCHITTEST /// </summary> private const int WM_NCHITTEST = 0x84; /// <summary> /// ULW_ALPHA /// </summary> private const int ULW_ALPHA = 0x02; /// <summary> /// AC_SRC_OVER /// </summary> private const byte AC_SRC_OVER = 0x00; /// <summary> /// AC_SRC_ALPHA /// </summary> private const byte AC_SRC_ALPHA = 0x01; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 생성 매개 변수 - CreateParams /// <summary> /// 생성 매개 변수 /// </summary> protected override CreateParams CreateParams { get { CreateParams createParams = base.CreateParams; createParams.ExStyle |= WS_EX_LAYERED; return createParams; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TransformForm() /// <summary> /// 생성자 /// </summary> public TransformForm() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 설정하기 - SetBitmap(bitmap, opacity) /// <summary> /// 비트맵 설정하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <param name="opacity">투명도</param> public void SetBitmap(Bitmap bitmap, int opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("비트맵은 알파 채널을 갖는 32비트 픽셀 이미지가 아닙니다."); } IntPtr screenDCHandle = GetDC(IntPtr.Zero); IntPtr memoryDCHandle = CreateCompatibleDC(screenDCHandle); IntPtr bitmapHandle = IntPtr.Zero; IntPtr previousBitmapHandle = IntPtr.Zero; try { bitmapHandle = bitmap.GetHbitmap(Color.FromArgb(0)); previousBitmapHandle = SelectObject(memoryDCHandle, bitmapHandle); Size targetSize = new Size(bitmap.Width, bitmap.Height); Point sourceLocation = new Point(0, 0); Point targetLocation = new Point(this.Left, this.Top); BlendFunction blendFunction = new BlendFunction(); blendFunction.BlendOperation = AC_SRC_OVER; blendFunction.BlendFlag = 0; blendFunction.SourceConstantAlpha = (byte)opacity; blendFunction.AlphaFormat = AC_SRC_ALPHA; UpdateLayeredWindow ( Handle , screenDCHandle , ref targetLocation, ref targetSize , memoryDCHandle , ref sourceLocation, 0 , ref blendFunction , ULW_ALPHA ); } finally { ReleaseDC(IntPtr.Zero, screenDCHandle); if(bitmapHandle != IntPtr.Zero) { SelectObject(memoryDCHandle, previousBitmapHandle); DeleteObject(bitmapHandle); } DeleteDC(memoryDCHandle); } } #endregion #region 비트맵 설정하기 - SetBitmap(bitmap) /// <summary> /// 비트맵 설정하기 /// </summary> /// <param name="bitmap">비트맵</param> public void SetBitmap(Bitmap bitmap) { SetBitmap(bitmap, 255); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 윈도우 프로시저 처리하기 - WndProc(message) /// <summary> /// 윈도우 프로시저 처리하기 /// </summary> /// <param name="message">메시지</param> protected override void WndProc(ref Message message) { if(message.Msg == WM_NCHITTEST) { message.Result = (IntPtr)HTCAPTION; } else { base.WndProc(ref message); } } #endregion } } |
▶ MainForm.cs
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 투명 폼 /// </summary> private TransformForm transformForm = null; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.bitmap = Bitmap.FromFile("IMAGE\\ring.png") as Bitmap; this.showButton.Click += showButton_Click; this.opacityTrackBar.ValueChanged += opacityTrackBar_ValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 투명 폼 보여주기 버튼 클릭시 처리하기 - showButton_Click(sender, e) /// <summary> /// 투명 폼 보여주기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showButton_Click(object sender, EventArgs e) { if(!OSFeature.Feature.IsPresent(OSFeature.LayeredWindows)) { MessageBox.Show("현재 시스템에서 레이어 윈도우가 지원되지 않습니다."); return; } if(this.transformForm == null) { this.transformForm = new TransformForm(); } this.transformForm.Show(); this.transformForm.SetBitmap(this.bitmap, this.opacityTrackBar.Value); } #endregion #region 불투명도 트랙바 값 변경시 처리하기 - opacityTrackBar_ValueChanged(sender, e) /// <summary> /// 불투명도 트랙바 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void opacityTrackBar_ValueChanged(object sender, EventArgs e) { this.opacityValueLabel.Text = this.opacityTrackBar.Value.ToString(); if(this.transformForm != null) { this.transformForm.SetBitmap(this.bitmap, this.opacityTrackBar.Value); } } #endregion } } |