.NET/Windows API for .NET

GetWindowText - 특정 Window의 제목 문자열 확인

클리엘 2019. 8. 6. 11:43
728x90

GetWindowText함수는 지정한 Window의 제목을 반환하는 함수입니다.

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer

▶VB.NET 선언

[DllImport("user32")]
public static extern int GetWindowText(int hwnd, StringBuilder lpString, int cch);

▶C# 선언


GetWindowText함수의 첫번째 인수는 제목을 확인하고자 하는 Window의 Handle을 기술하고 두번째 인수에서 실제 제목을 저장할 변수를 지정합니다.

마지막 세번째 인수는 저장될 제목의 크기를 기술합니다.

위 인수를 토대로 현재 실행중인 Form의 제목을 조회하려면 GetWindowText함수를 다음과 같이 호출할 수 있습니다.

Dim strText As String = Space(50)
GetWindowText(Me.Handle, strText, 49)

▶VB.NET 호출

StringBuilder strText = new StringBuilder(50);
GetWindowText((int)this.Handle, strText, 49);

▶C# 호출


GetWindowText 함수는 실행에 실패하는 경우 0을 반환합니다.

728x90