티스토리 뷰
목차
C# 윈도우 크기, 위치 확인 (GetWindowPlacement, FindWindow)
전체 소스는 맨 아래에 있고, 주요 소스부터 한 부분씩 설명합니다.
먼저, 윈도우(Window) 라이브러리를 사용해야 하니 InteropServices를 추가합니다.
1 | using System.Runtime.InteropServices; | cs |
그리고 밑에서 사용할 함수를 위해 enum과 struct도 하나씩 선언합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | internal enum SHOW_WINDOW_COMMANDS : int { HIDE = 0, NORMAL = 1, MINIMIZED = 2, MAXIMIZED = 3, } internal struct WINDOWPLACEMENT { public int length; public int flags; public SHOW_WINDOW_COMMANDS showc_cmd; public Point min_position; public Point max_position; public Rectangle normal_position; } | cs |
주의할 점.
enum과 구조체는 변경하면 안 됩니다. 이름을 바꾸는 건 상관없는데 요소 자체를 지워버리면 연산이 제대로 안 됩니다.
이어서, 윈도우 크기와 위치를 알아낼 함수도 선언합니다.
1 2 3 4 5 | [DllImport("USER32.DLL", SetLastError = true)] public static extern IntPtr FindWindow(string class_name, string window_name); [DllImport("user32.dll")] internal static extern bool GetWindowPlacement(IntPtr handle, ref WINDOWPLACEMENT placement); | cs |
윈도우 크기, 위치를 알아내려면 해당 윈도우의 핸들이 필요합니다. 그래서 핸들을 알아내는 함수도 추가합니다.
1 2 3 4 | public IntPtr GetWinAscHandle() { return FindWindow(null, "제목 없음 - 메모장"); } | cs |
다음은 실제로 윈도우 위치와 크기를 알아내는 함수입니다.
1 2 3 4 5 6 7 8 9 10 | private void GetWindowPos(IntPtr hwnd, ref Point point, ref Size size) { WINDOWPLACEMENT placement = new WINDOWPLACEMENT(); placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement); GetWindowPlacement(hwnd, ref placement); size = new Size(placement.normal_position.Right - (placement.normal_position.Left * 2), placement.normal_position.Bottom - (placement.normal_position.Top * 2)); point = new Point(placement.normal_position.Left, placement.normal_position.Top); } | cs |
인자로 윈도우 핸들, 포인트 저장할 객체, 윈도우 크기 저장할 객체를 전달받습니다.
함수 원형대로 따라가자면 인자가 더 많이 필요한데, 굳이 모두 사용하지 않겠다면, 위와 같이 줄여도 됩니다. 잘 봐야 할 부분은 WINDOWPLACEMENT로 위에서 선언한 구조체입니다. 이 구조체에 윈도우 크기, 위치 정보를 저장하여 리턴합니다.
[C# 윈도우 사이즈, 포지션 GetWindowPlacement]
위 그림처럼 조사식에서 확인해 볼 수 있습니다. 윈도우 위치와 크기가 연산 되어 정상적으로 삽입된 것이 확인됩니다.
아래는 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 | namespace GetWindowPosition { internal enum SHOW_WINDOW_COMMANDS : int { HIDE = 0, NORMAL = 1, MINIMIZED = 2, MAXIMIZED = 3, } internal struct WINDOWPLACEMENT { public int length; public int flags; public SHOW_WINDOW_COMMANDS showc_cmd; public Point min_position; public Point max_position; public Rectangle normal_position; } public partial class Form1 : Form { [DllImport("USER32.DLL", SetLastError = true)] public static extern IntPtr FindWindow(string class_name, string window_name); [DllImport("user32.dll")] internal static extern bool GetWindowPlacement(IntPtr handle, ref WINDOWPLACEMENT placement); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Point point = new Point(); Size size = new Size(); GetWindowPos(GetWinAscHandle(), ref point, ref size); this.lb_pos_x.Text = point.X.ToString(); this.lb_pos_y.Text = point.Y.ToString(); this.lb_size_width.Text = size.Width.ToString(); this.lb_size_height.Text = size.Height.ToString(); } private void GetWindowPos(IntPtr hwnd, ref Point point, ref Size size) { WINDOWPLACEMENT placement = new WINDOWPLACEMENT(); placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement); GetWindowPlacement(hwnd, ref placement); size = new Size(placement.normal_position.Right - (placement.normal_position.Left * 2), placement.normal_position.Bottom - (placement.normal_position.Top * 2)); point = new Point(placement.normal_position.Left, placement.normal_position.Top); } public IntPtr GetWinAscHandle() { return FindWindow(null, "제목 없음 - 메모장"); } } } | cs |
최대한 필요한 부분만 요약한 것으로 크기, 위치 외에 다른 정보가 필요하다면, 함수 원형을 그대로 사용하세요.
[C# 윈도우 사이즈, 포지션 GetWindowPlacement]
사실, Point, Size 클래스 객체를 따로 만들지 않아도 됩니다.
위 그림처럼 WINDOWPLACEMENT 구조체 변수에 모든 데이터가 들어있긴 합니다.
그러니, 예제처럼 객체를 따로 만들어 관리해도 되고, WINDOWPLACEMENT 변수만 이용해도 됩니다. 편한 대로 선택하면 돼요.
C# 윈도우 크기, 위치 확인 (GetWindowPlacement, FindWindow)