.NET/Windows API for .NET

GetWindowRect - Window의 위치및 크기 반환

클리엘 2019. 7. 31. 16:33
728x90

GetWindowRect함수는 화면상의 특정 Window에 대한 위치및 크기를 반환하는 함수입니다.

Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

- VB.NET 선언

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

- C# 선언

 

선언 GetWindowRect함수의 첫번째 인수는 위치및 크기를 얻고자 하는 Window의 Handle을 지정하고 두번째 인수에 실제 위치와 크기에 대한 값이 저장될 구조체를 지정합니다.

여기서 사용되는 구조체는 다음과 같이 선언됩니다.

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

- VB.NET

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

- C#

 

만일 실행중인 현재 Form에 대한 위치및 크기값을 가져오려면 GetWindowRect함수를 다음과 같이 선언합니다.

Dim stRect As RECT
GetWindowRect(Me.Handle, stRect)

- VB.NET 호출

RECT stRect = default(RECT);
GetWindowRect((int)this.Handle, ref stRect);

- C# 호출

728x90