1. 모듈에 다음의 API 함수 및 상수를 선언합니다.
Public Declare Function EnumWindows Lib "user32" _
(ByVal wndenmprc As Long, _
ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String) As Long
Private Target_Name As String
Private Change_Name As String
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
Dim rtn As Long
' 윈도우의 타이틀을 얻어온다
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left(buf, length)
' 윈도우의 타이틀을 비교한다.
If InStr(title, Target_Name) <> 0 Then
' 찾는 윈도우가 발견되면 위도우의 타이틀을 바꾼다.
rtn = SetWindowText(app_hWnd, Change_Name)
End If
' 계속 찾는다
EnumCallback = 1
End Function
Public Sub FindWindowHwnd(app_name As String, cng_name As String)
Target_Name = app_name
Change_Name = cng_name
EnumWindows AddressOf EnumCallback, 0
End Sub
2. 폼에 두개의 텍스트박스와 하나의 버튼을 만들고 다음과 같이 코딩하십시요.
Private Sub Command1_Click()
FindWindowHwnd Text1.Text, Text2.Text
End Sub
3. 위와 같이 한후 실행을 하고 나서, 일단 메모장 프로그램을 띄우기 바랍니다. 그러면 메모장
프로그램의 타이틀은 "메모장-제목 없음" 이라고 써져 있을 것입니다.
실행된 프로그램의 Text1.Text = "메모장" 이라고 입력을 한후 Text2.Text = "나의 프로그램"
이라고 입력을 하고 나서 버튼을 클릭해 보십시요.
그러면 메모장 프로그램의 타이틀이 "나의 프로그램" 이라고 바뀌는 것을 볼수 있을 것입니다.