본문 바로가기
개발언어/C#

[C#] 윈도우 좌표

by 창용이랑 2021. 12. 14.
728x90

Please note that this does NOT work properly with Windows Vista Aero (this includes Windows 10), and reports faulty values. This is largely due to Aero incorporating additional invisible borders which are used to "resize" the window using the cursor. In this instance, the developer should look into using DwmGetWindowAttribute (dwmapi.dll) with DWMWA_EXTENDED_FRAME_BOUNDS.

For .NET CF you may need to replace user32.dll with coredll.dll

 

이것은 Windows Vista Aero(Windows 10 포함)에서는 제대로 작동하지 않으며 잘못된 값을 보고합니다. 

이는 커서를 사용하여 창의 "크기를 조정"하는 데 사용되는 추가 보이지 않는 테두리를 Aero에 통합했기 때문입니다. 

이 경우 개발자는 DWMWA_EXTENDED_FRAME_BOUNDS와 함께 DwmGetWindowAttribute(dwmapi.dll)를 사용하는 방법을 살펴봐야 합니다.

.NET CF의 경우 user32.dll을 coredll.dll로 교체해야 할 수도 있습니다.

 

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
     public int Left;        // x position of upper-left corner
     public int Top;         // y position of upper-left corner
     public int Right;       // x position of lower-right corner
     public int Bottom;      // y position of lower-right corner
}

Rectangle myRect = new Rectangle();

private void button1_Click(object sender, System.EventArgs e)
{
     RECT rct;

     if(!GetWindowRect(new HandleRef(this, this.Handle), out rct ))
     {
     MessageBox.Show("ERROR");
             return;
     }
     MessageBox.Show( rct.ToString() );

     myRect.X = rct.Left;
     myRect.Y = rct.Top;
     myRect.Width = rct.Right - rct.Left + 1;
     myRect.Height = rct.Bottom - rct.Top + 1;
}

 

출처 : https://www.pinvoke.net/default.aspx/user32.getwindowrect