선언:

C#

[DllImport("user32")]
public static extern int SetWindowText(IntPtr hWnd, String lpString);


VB.NET

<DllImport("user32")> _
Public Shared Function SetWindowText(hWnd As IntPtr, lpString As [String]) As Integer
End Function




사용 예제:

C#

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        [DllImport("user32")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32")]
        public static extern int SetWindowText(IntPtr hWnd, String lpString);
        [DllImport("user32")]
        public static extern Int32 GetCursorPos(out POINT pt);
        [DllImport("user32")]
        public static extern IntPtr WindowFromPoint(POINT pt);
        
        public struct POINT {
            public Int32 x;
            public Int32 y;
        }
        
        public static void Main(string[] args) {
            POINT pt;
            GetCursorPos(out pt);
            
            IntPtr hWnd = WindowFromPoint(pt);
            Console.WriteLine("{0}, {1} 위치에 있는 윈도우 핸들: {2:X8}", pt.x, pt.y, hWnd.ToInt32());
            if ( hWnd != IntPtr.Zero ) {
                StringBuilder sbWinText = new StringBuilder(260);
                GetWindowText(hWnd, sbWinText, 260);
                Console.WriteLine("변경하기 전의 윈도우 창의 제목: {0}", sbWinText.ToString());
                
                Console.Write("새로운 윈도우 창의 제목을 입력하세요: ");
               EnterWindowText:
                String wndText = Console.ReadLine().Trim();
                if ( String.IsNullOrEmpty(wndText) ) {
                    Console.WriteLine("다시 입력하세요");
                    goto EnterWindowText;
                }
                
                SetWindowText(hWnd, wndText);
                sbWinText.Clear();
                GetWindowText(hWnd, sbWinText, 260);
                Console.WriteLine("변경된 후의 윈도우 창의 제목: {0}", sbWinText.ToString());
            }
            
            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Namespace ApiReference
    Class ApiExample
        <DllImport("user32")> _
        Public Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As IntegerAs Integer
        End Function
        <DllImport("user32")> _
        Public Shared Function SetWindowText(hWnd As IntPtr, lpString As [String]) As Integer
        End Function
        <DllImport("user32")> _
        Public Shared Function GetCursorPos(ByRef pt As POINT) As Int32
        End Function
        <DllImport("user32")> _
        Public Shared Function WindowFromPoint(pt As POINT) As IntPtr
        End Function

        Public Structure POINT
            Public x As Int32
            Public y As Int32
        End Structure

        Public Shared Sub Main(args As String())
            Dim pt As POINT
            GetCursorPos(pt)

            Dim hWnd As IntPtr = WindowFromPoint(pt)
            Console.WriteLine("{0}, {1} 위치에 있는 윈도우 핸들: {2:X8}", pt.x, pt.y, hWnd.ToInt32())
            If hWnd <> IntPtr.Zero Then
                Dim sbWinText As New StringBuilder(260)
                GetWindowText(hWnd, sbWinText, 260)
                Console.WriteLine("변경하기 전의 윈도우 창의 제목: {0}", sbWinText.ToString())

                Console.Write("새로운 윈도우 창의 제목을 입력하세요: ")
                EnterWindowText:
                Dim wndText As [String] = Console.ReadLine().Trim()
                If [String].IsNullOrEmpty(wndText) Then
                    Console.WriteLine("다시 입력하세요")
                    GoTo EnterWindowText
                End If

                SetWindowText(hWnd, wndText)
                sbWinText.Clear()
                GetWindowText(hWnd, sbWinText, 260)
                Console.WriteLine("변경된 후의 윈도우 창의 제목: {0}", sbWinText.ToString())
            End If

            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

hWnd - 제목을 변경할 윈도우 창의 핸들을 입력합니다.

lpString - 새로운 윈도우 창의 제목을 입력합니다.




API 설명:

윈도우 창의 제목을 변경합니다.




참고:

SetWindowText (MSDN)




비고:

탭 문자 (0x09, '\t')는 지원되지 않으므로, 탭 문자가 있는 경우엔 모두 '|' 로 대체됩니다.



'API Reference' 카테고리의 다른 글

25. GetCurrentProcessId  (0) 2014.10.07
24. DebugActiveProcessStop  (0) 2014.10.06
22. GetWindowTextLength  (0) 2014.10.04
21. GetWindowText  (0) 2014.10.04
20. DebugActiveProcess  (0) 2014.10.03

+ Recent posts