- PR -

MDIの子ウィンドウのハンドル取得について

1
投稿者投稿内容
シュガー
会議室デビュー日: 2006/08/04
投稿数: 3
投稿日時: 2008-03-10 13:44
別アプリケーション内の画面(MDI)の子ウィンドウのハンドルが取得できずに困っています。

別アプリケーションの親ウィンドウハンドルおよびその子ウィンドウハンドルは、APIの「FindWindow」「EnumChildWindows」を使用して取得できましたが、子ウィンドウハンドル内のMDI画面のウィンドウのハンドルおよびMDI画面の子ウィンドウハンドル(コントロールなど、親ウィンドウから見ると孫ウィンドウ)を取得することができません。

どのようにすれば取得できるでしょうか?

環境は、XP、ExcelVBAです。(別アプリは何でも可:テストではMSAccessを使用)

以下にサンプルソースを掲載します。

コード:
Type POINTAPI

x As Long
y As Long
End Type

Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Type WINDOWPLACEMENT
length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type

'ウィンドウハンドル取得
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'子ウィンドウハンドル列挙
Declare Function EnumChildWindows Lib "user32" ( _
ByVal hWndParent As Long, _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
'ウィンドウの位置・状態を取得
Declare Function GetWindowPlacement Lib "user32" Alias "GetWindowPlacement" ( _
ByVal hWnd&, lpwndpl As WINDOWPLACEMENT) AS Long
'ウィンドウのキャプション・テキスト取得
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
'ウィンドウのクラス名取得
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Public Sub Command1_Click()
On Error Resume Next
Dim Ret As Long
Dim putText As String
Dim hWnd As Long

rowNo = 1

'親ウィンドウハンドル取得(testというAccessのアプリ名)
hWnd = FindWindow(vbNullString, "test")
'子ウィンドウハンドル列挙
Ret = EnumChildWindows(hWnd, AddressOf EnumChildProc, 0)

End Sub

Public Function EnumChildProc(ByVal hWnd As Long) As Long

Dim Ret As Long
Dim Leng As Long
Dim Name As String
Dim putText As String
Dim WinPt As WINDOWPLACEMENT

'バッファ確保
Name = String(255, Chr(0))
Leng = Len(Name)

'名前を取得する
Ret = GetWindowText(hWnd, Name, Leng)

If Ret <> 0 Then
putText = Replace(Name, Chr(0), "")
End If
'ウィンドウのキャプション(テキスト)をExcelへ吐き出し
Range(Cells(rowNo, 1), Cells(rowNo, 1)).Value = putText

Name = String(255, Chr(0))
Leng = Len(Name)

'クラス名取得
Ret = GetClassName(hWnd, Name, Leng)

If Ret <> 0 Then
putText = Replace(Name, Chr(0), "")
'クラス名をExcelへ吐き出し
Range(Cells(rowNo, 2), Cells(rowNo, 2)).Value = putText
If InStr(1, putText, "MDIClient") > 0 Then
'MDIの場合、その子ウィンドウを取得する
Ret = EnumChildWindows(hWnd, AddressOf EnumChildProc, 0)
End If
End If

WinPt.length = Len(WinPt)
'ウィンドウの配置方法を取得
Ret = GetWindowPlacement(hWnd, WinPt)

If Ret <> 0 Then
'ウィンドウのポジションをExcelへ吐き出し
Range(Cells(rowNo, 3), Cells(rowNo, 3)).Value = WinPt.rcNormalPosition.top
Range(Cells(rowNo, 4), Cells(rowNo, 4)).Value = WinPt.rcNormalPosition.left
End If

rowNo = rowNo + 1
EnumChildProc = 1
End Function



[ メッセージ編集済み 編集者: シュガー 編集日時 2008-03-10 15:07 ]
1

スキルアップ/キャリアアップ(JOB@IT)