.NET/Windows API for .NET

GetClientRect - Window나 Control의 좌표값 얻기

클리엘 2019. 7. 31. 14:07
728x90
Declare Function GetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

- VB.NET 선언

[DllImport("user32")]
public static extern int GetClientRect(int hwnd, ref RECT lpRect);

- C# 선언

 

GetClientRect함수의 첫번째 인수는 좌표를 구하고자할 Window나 Control의 Handle을 넘기고 두번째 인수에는 해당 좌표값을 담아둘 구조체를 기술합니다.

만일 현재 실행중인 Form의 위치및 좌표를 구하고자 한다면 GetClientRect함수는 다음과 같이 선언될 수 있습니다.

Public Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
End Structure

Dim norRECT As RECT
GetClientRect(Me.Handle, norRECT)

- VB.NET 호출

public struct RECT
{
            public int left;
            public int top;
            public int right;
            public int bottom;
}

RECT norRECT = default(RECT);
GetClientRect((int)button1.Handle, ref norRECT);

- C# 호출

 

GetClientRect함수가 성공적으로 수행되면 해당 구조체의 left, top에 x, y에 해당하는 위치값이 그리고 right, bottom에는 left와 top으로 부터의 크기값이 저장됩니다.

GetClientRect함수는 정상적으로 실행되지 못한 경우 0을 반환합니다.

728x90