■ PrintWindow API 함수를 사용해 윈도우 비트맵을 구하는 방법을 보여준다.
▶ ShowWindowCommand.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 |
namespace TestProject { /// <summary> /// 윈도우 명령 표시 /// </summary> public enum ShowWindowCommand : int { /// <summary> /// SW_HIDE /// </summary> SW_HIDE = 0, /// <summary> /// SW_SHOWNORMAL /// </summary> SW_SHOWNORMAL = 1, /// <summary> /// SW_SHOWMINIMIZED /// </summary> SW_SHOWMINIMIZED = 2, /// <summary> /// SW_MAXIMIZE /// </summary> SW_MAXIMIZE = 3, /// <summary> /// SW_SHOWMAXIMIZED /// </summary> SW_SHOWMAXIMIZED = 3, /// <summary> /// SW_SHOWNOACTIVATE /// </summary> SW_SHOWNOACTIVATE = 4, /// <summary> /// SW_SHOW /// </summary> SW_SHOW = 5, /// <summary> /// SW_MINIMIZE /// </summary> SW_MINIMIZE = 6, /// <summary> /// SW_SHOWMINNOACTIVE /// </summary> SW_SHOWMINNOACTIVE = 7, /// <summary> /// SW_SHOWNA /// </summary> SW_SHOWNA = 8, /// <summary> /// SW_RESTORE /// </summary> SW_RESTORE = 9 } } |
▶ POINT.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 |
using System.Drawing; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 포인트 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct POINT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// X /// </summary> public int X; /// <summary> /// Y /// </summary> public int Y; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - POINT(x, y) /// <summary> /// 생성자 /// </summary> /// <param name="x">X 좌표</param> /// <param name="y">Y 좌표</param> public POINT(int x, int y) { X = x; Y = y; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Point 타입 암시적 변환하기 - Point(point) /// <summary> /// Point 타입 암시적 변환하기 /// </summary> /// <param name="point">포인트</param> public static implicit operator Point(POINT point) { return new Point(point.X, point.Y); } #endregion #region POINT 타입 암시적 변환하기 - POINT(point) /// <summary> /// POINT 타입 암시적 변환하기 /// </summary> /// <param name="point">포인트</param> public static implicit operator POINT(Point point) { return new POINT(point.X, point.Y); } #endregion } } |
▶ RECT.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 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 사각형 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct RECT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 왼쪽 /// </summary> public int Left; /// <summary> /// 위쪽 /// </summary> public int Top; /// <summary> /// 오른쪽 /// </summary> public int Right; /// <summary> /// 아래쪽 /// </summary> public int Bottom; #endregion } } |
▶ WINDOWPLACEMENT.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 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 윈도우 배치 /// </summary> [Serializable] [StructLayout(LayoutKind.Sequential)] public struct WINDOWPLACEMENT { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 길이 /// </summary> public int Length; /// <summary> /// 길이 /// </summary> public int Flag; /// <summary> /// 윈도우 명령 표시 여부 /// </summary> public ShowWindowCommand ShowWindowCommand; /// <summary> /// 최소 위치 /// </summary> public POINT MinimumPosition; /// <summary> /// 최대 위치 /// </summary> public POINT MaximumPosition; /// <summary> /// 일반 사각형 /// </summary> public RECT NormalRectangle; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 디폴트 - Default /// <summary> /// 디폴트 /// </summary> public static WINDOWPLACEMENT Default { get { WINDOWPLACEMENT windowPlacement = new WINDOWPLACEMENT(); windowPlacement.Length = Marshal.SizeOf(windowPlacement); return windowPlacement; } } #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 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 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 전경 윈도우 구하기 - GetForegroundWindow() /// <summary> /// 전경 윈도우 구하기 /// </summary> /// <returns>윈도우 핸들</returns> [DllImport("user32")] private static extern IntPtr GetForegroundWindow(); #endregion #region 윈도우 배치 구하기 - GetWindowPlacement(windowHandle, windowPlacement) /// <summary> /// 윈도우 배치 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="windowPlacement">윈도우 배치</param> /// <returns>처리 결과</returns> [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetWindowPlacement(IntPtr windowHandle, out WINDOWPLACEMENT windowPlacement); #endregion #region 윈도우 인쇄하기 - PrintWindow(windowHandle, deviceContexthandle, flag) /// <summary> /// 윈도우 인쇄하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="deviceContexthandle">디바이스 컨텍스트 핸들</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool PrintWindow(IntPtr windowHandle, IntPtr deviceContexthandle, uint flag); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.executeButton.Click += executeButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 실행 버튼 클릭시 처리하기 - executeButton_Click(sender, e) /// <summary> /// 실행 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void executeButton_Click(object sender, EventArgs e) { Thread.Sleep(5000); IntPtr windowHandle = GetForegroundWindow(); this.pictureBox.Image = GetWindowBitmap(windowHandle); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 윈도우 비트맵 구하기 - GetBitmap(windowHandle) /// <summary> /// 윈도우 비트맵 구하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>윈도우 비트맵</returns> private Bitmap GetWindowBitmap(IntPtr windowHandle) { WINDOWPLACEMENT windowPlacement; GetWindowPlacement(windowHandle, out windowPlacement); int windowWidth = windowPlacement.NormalRectangle.Right - windowPlacement.NormalRectangle.Left + 1; int windowHeight = windowPlacement.NormalRectangle.Bottom - windowPlacement.NormalRectangle.Top + 1; Bitmap bitmap = new Bitmap(windowWidth, windowHeight); using(Graphics bitmapGraphics = Graphics.FromImage(bitmap)) { IntPtr deviceContextHandle = bitmapGraphics.GetHdc(); PrintWindow(windowHandle, deviceContextHandle, 0); bitmapGraphics.ReleaseHdc(deviceContextHandle); return bitmap; } } #endregion } } |