Win32API 関数 ウィンドウ操作 ウィンドウ名でトップレベルウィンドウ ( 親を持たないウィンドウ ) のハンドルを取得 メモ帳や電卓等のウィンドウ名でトップレベルウィンドウ ( 親を持たないウィンドウ ) のハンドルを取得する方法を 下記に示す Visual Basic Imports System.Runtime.InteropServices Public Class WindowFromWindowName Private Shared Function FindWindow ( _ ByVal lpclassname As String, _ ByVal lpwindowname As String ) As IntPtr Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click Dim hwnd As IntPtr ' 名前を付けて保存ダイアログのハンドル取得 hwnd = FindWindow ( vbnullstring, " 名前を付けて保存 " ) If hwnd = IntPtr.Zero Then MessageBox.Show( " フォームが取得出来ません!" ) Exit Sub ' ハンドルの表示 txthandle.text = hwnd.tostring( ) End Class using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; C# namespace WindowFromName public partial class WindowFromName : Form [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindow ( string lpclassname, string lpwindowname ); -1-
public WindowFromName( ) InitializeComponent( ); // ボタン ( 取得 ) がクリックされた時の処理 private void btnget_click ( object sender, EventArgs e ) IntPtr hwnd = IntPtr.Zero; // 名前を付けて保存ダイアログのハンドル取得 hwnd = FindWindow ( null, " 名前を付けて保存 " ); if ( hwnd == IntPtr.Zero ) MessageBox.Show( " フォームが取得出来ません!" ); return; // ハンドルの表示 txthandle.text = hwnd.tostring( ); クラス名でトップレベルウィンドウ ( 親を持たないウィンドウ ) のハンドルを取得 Notepad や SciCalc 等のクラス名でトップレベルウィンドウ ( 親を持たないウィンドウ ) のハンドルを取得する方法を 下記に示す Visual Basic Imports System.Runtime.InteropServices Public Class WindowFromClassName Private Shared Function FindWindow ( _ ByVal lpclassname As String, _ ByVal lpwindowname As String ) As IntPtr Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click Dim hwnd As IntPtr ' メモ帳のハンドル取得 hwnd = FindWindow ( "Notepad", vbnullstring ) If hwnd = IntPtr.Zero Then MessageBox.Show( " フォームが取得出来ません!" ) Exit Sub ' ハンドルの表示 txthandle.text = hwnd.tostring( ) End Class -2-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; C# namespace WindowFromClass public partial class WindowFromClass : Form [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindow ( string lpclassname, string lpwindowname ); public WindowFromClass( ) InitializeComponent( ); // ボタン ( 取得 ) がクリックされた時の処理 private void btnget_click ( object sender, EventArgs e ) IntPtr hwnd = IntPtr.Zero; // 名前を付けて保存ダイアログのハンドル取得 hwnd = FindWindow ( "Notepad", null ); if ( hwnd == IntPtr.Zero ) MessageBox.Show ( " フォームが取得出来ません!" ); return; // ハンドルの表示 txthandle.text = hwnd.tostring( ); キャプションでコントロールのハンドルを取得 名前を付けて保存ダイアログの保存ボタンの様にキャプションを有するコントロールのハンドルを取得する方法を 下記に示す Visual Basic Imports System.Runtime.InteropServices Public Class WindowFromCaption Private Shared Function FindWindow ( _ ByVal lpclassname As String, _ ByVal lpwindowname As String ) As IntPtr -3-
Private Shared Function FindWindowEx ( _ ByVal parenthandle As IntPtr, _ ByVal childafter As IntPtr, _ ByVal lclassname As String, _ ByVal windowtitle As String ) As IntPtr Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click Dim Ph, Ch As IntPtr ' 親ウィンドウ ( フォーム ) の取得 Ph = FindWindow ( vbnullstring, " ファイルを開く " ) If Ph = IntPtr.Zero Then MessageBox.Show ( " フォームが取得出来ません!" ) Exit Sub ' 子ウィンドウ ( ボタン ) の取得 Ch = FindWindowEx ( Ph, Nothing, vbnullstring, " 開く (&O)" ) If Ch = IntPtr.Zero Then MessageBox.Show ( " ボタンが取得出来ません!" ) Exit Sub ' ハンドルの表示 txthandle.text = Ch.ToString( ) End Class using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; C# namespace WindowFromCaption public partial class WindowFromCaption : Form [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindow ( string lpclassname, string lpwindowname ); [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindowEx ( IntPtr hwndparent, IntPtr hwndchildafter, string lpszclass, string lpszwindow ); public WindowFromCaption( ) InitializeComponent( ); -4-
private void btnget_click ( object sender, EventArgs e ) IntPtr ph, ch; // 親ウィンドウ ( フォーム ) の取得 ph = FindWindow ( null, " ファイルを開く " ); if ( ph == IntPtr.Zero ) MessageBox.Show ( " フォームが取得出来ません!" ); return; // 子ウィンドウ ( ボタン ) の取得 ch = FindWindowEx ( ph, IntPtr.Zero, null, " 開く (&O)" ); if ( ch == IntPtr.Zero ) MessageBox.Show( " ボタンが取得出来ません!" ); return; // ハンドルの表示 txthandle.text = ch.tostring( ); コントロール ID でコントロールのハンドルを取得 名前を付けて保存ダイアログのファイル名入力ボックスの様にキャプションの無いコントロールのハンドルを取得する方法を 下記に示す 猶 コントロール ID は Visual Studio 付属の Spy++ で予め調べて置く必要が有る Visual Basic Imports System.Runtime.InteropServices Public Class WindowFromControlID Private Shared Function FindWindow ( _ ByVal lpclassname As String, _ ByVal lpwindowname As String ) As IntPtr < DllImport ( "user32.dll" )> _ Public Shared Function GetDlgItem ( _ ByVal hdlg As IntPtr, _ ByVal niddlgitem As UInteger ) As IntPtr Private Shared Function SendMessage ( _ ByVal hwnd As IntPtr, _ ByVal Msg As UInteger, _ ByVal wparam As Integer, _ -5-
ByVal lparam As System.Text.StringBuilder ) As IntPtr Private Const WM_GETTEXT As UInteger = 13 Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click Dim Ph, Ch As IntPtr ' 名前を付けて保存ダイアログの取得 Ph = FindWindow ( vbnullstring, " 名前を付けて保存 " ) If Ph = IntPtr.Zero Then MessageBox.Show ( " フォームが取得出来ません!" ) Exit Sub ' ファイル名テキストボックスの取得 Ch = GetDlgItem( Ph, &H480 ) If Ch = IntPtr.Zero Then MessageBox.Show ( " テキストボックスが取得出来ません!" ) Exit Sub ' ハンドルの表示 txthandle.text = Ch.ToString( ) ' テキストの取得 Dim B As System.Text.StringBuilder = New System.Text.StringBuilder( 256 ) Dim N As Integer = 256 SendMessage ( Ch, WM_GETTEXT, N, B ) txttext.text = B.ToString( ) End Class C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowFromControlID public partial class WindowFromControlID : Form [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindow ( string lpclassname, string lpwindowname ); [ DllImport ( "user32.dll" )] static extern IntPtr GetDlgItem ( IntPtr hdlg, int niddlgitem ); [ DllImport ( "user32.dll", CharSet = CharSet.Auto )] -6-
static extern IntPtr SendMessage ( IntPtr hwnd, UInt32 Msg, int wparam, System.Text.StringBuilder lparam ); private const UInt32 WM_GETTEXT = 13; public WindowFromControlID( ) InitializeComponent( ); private void btnget_click ( object sender, EventArgs e ) IntPtr ph, ch; // 名前を付けて保存ダイアログの取得 ph = FindWindow ( null, " 名前を付けて保存 " ); if ( ph == IntPtr.Zero ) MessageBox.Show ( " フォームが取得出来ません!" ); return; // ファイル名テキストボックスの取得 ch = GetDlgItem( ph, 0x480 ); if ( ch == IntPtr.Zero ) MessageBox.Show ( " テキストボックスが取得出来ません!" ); return; // ハンドルの表示 txthandle.text = ch.tostring( ); // テキストの取得 System.Text.StringBuilder b = new System.Text.StringBuilder( 256 ); int n = 256; SendMessage ( ch, WM_GETTEXT, n, b ); txttext.text = b.tostring( ); 指定された座標を含むウィンドウのハンドルを取得 指定された座標に在るウィンドウのハンドルを取得する方法を 下記に示す 下記のプログラムでは マウスクリックした位置に在るウィンドウやコントロールのハンドルを取得し 其のハンドル クラス名 キャプション ( テキスト ) 等を表示する Visual Basic Imports System.Runtime.InteropServices Public Class WindowFromPoint ' WindowFromPoint( 指定された座標を含むウィンドウのハンドルを取得 ) の宣言 -7-
< DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ Private Shared Function WindowFromPoint ( _ ByVal lppoint As Point ) As IntPtr ' GetCursorPos( マウスの座標を取得 ) の宣言 < DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ Private Shared Function GetCursorPos ( _ ByRef lppoint As Point ) As Boolean ' GetWindowRect(Window の位置等を取得 ) の宣言 < DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ Private Shared Function GetWindowRect ( _ ByVal hwnd As IntPtr, _ ByRef lprect As RECT ) As Boolean ' RECT 構造体 Private Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure ' GetClassName( クラス名を取得 ) の宣言 < DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ Private Shared Sub GetClassName ( _ ByVal hwnd As System.IntPtr, _ ByVal lpclassname As System.Text.StringBuilder, _ ByVal nmaxcount As Integer ) ' GetWindowText( テキストを取得 ) の宣言 Private Shared Function GetWindowText ( _ ByVal hwnd As IntPtr, _ ByVal lpstring As System.Text.StringBuilder, _ ByVal cch As Integer ) As Integer ' PostMessage の宣言 Private Shared Function PostMessage ( _ ByVal hwnd As IntPtr, _ ByVal Msg As UInteger, _ ByVal wparam As Integer, _ ByVal lparam As System.Text.StringBuilder ) As Boolean Private Shared Function SendMessage ( _ ByVal hwnd As IntPtr, _ -8-
ByVal Msg As UInteger, _ ByVal wparam As Integer, _ ByVal lparam As System.Text.StringBuilder ) As IntPtr Private Const WM_GETTEXT As UInteger = 13 ' フォームが読み込まれた時の処理 Private Sub WindowFromPoint_Load ( ByVal sender As System.Object, _ ByVal e As System.EventArgs ) Handles MyBase.Load tmrget.start( ) ' タイマーが一定間隔で自動的に行う処理 Private Sub tmrget_tick( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles tmrget.tick If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then Dim P As Point ' マウス座標の取得 GetCursorPos( P ) lblmouseposition.text = _ "X:" & P.X.ToString( ).PadLeft( 4 ) & " " & _ "Y:" & P.Y.ToString( ).PadLeft( 4 ) ' マウス座標よりハンドル取得 Dim hwnd As IntPtr hwnd = WindowFromPoint( P ) If hwnd = IntPtr.Zero Then Exit Sub txthandle.text = hwnd.tostring( ) ' 現在のハンドルの座標値の取得 Dim R As RECT Dim S As System.Text.StringBuilder = New System.Text.StringBuilder() GetWindowRect( hwnd, R ) S.Append( "Top : " & R.Top.ToString( ) & ControlChars.NewLine ) S.Append( "Left : " & R.Left.ToString( ) & ControlChars.NewLine ) S.Append( "Bottom : " & R.Bottom.ToString( ) & ControlChars.NewLine ) S.Append( "Right : " & R.Right.ToString( )) lblformlocation.text = S.ToString( ) ' クラス名の取得 Dim B As System.Text.StringBuilder = New System.Text.StringBuilder( 256 ) Dim N As Integer = 256 GetClassName( hwnd, B, N ) txtclass.text = B.ToString( ).Trim( ) ' テキストの取得 GetWindowText( hwnd, B, N ) If B.ToString( ) = "" Then ' GetWindowText でテキストが取得出来ない場合 ' GetWindowText では他アプリケーションのテキストを取得出来ない -9-
SendMessage( hwnd, WM_GETTEXT, N, B ) txttext.text = B.ToString( ) End Class C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowFromPoint public partial class WindowFromPointCS : Form [ DllImport ( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )] static extern IntPtr WindowFromPoint ( Point point ); [ DllImport ( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )] static extern bool GetCursorPos ( ref Point lppoint ); [ DllImport ( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )] static extern bool GetWindowRect ( IntPtr hwnd, ref RECT lprect ); private struct RECT public int left; public int top; public int right; public int bottom; [ DllImport ( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )] static extern int GetClassName ( IntPtr hwnd, StringBuilder lpclassname, int nmaxcount ); [ DllImport ( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )] static extern int GetWindowText ( IntPtr hwnd, StringBuilder lpstring, int nmaxcount ); [ DllImport ( "user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage ( IntPtr hwnd, UInt32 Msg, int wparam, System.Text.StringBuilder lparam ); private const uint WM_GETTEXT = 13; public WindowFromPointCS( ) InitializeComponent( ); -10-
// フォームが読み込まれた時の処理 private void WindowFromPoint_Load ( object sender, EventArgs e ) tmrget.start( ); // タイマーが一定間隔で自動的に行う処理 private void tmrget_tick ( object sender, EventArgs e ) if ( Control.MouseButtons == MouseButtons.Left ) Point p = new Point( ); // Point p = Cursor.Position; でも良い // マウス座標の取得 GetCursorPos( ref p ); lblmouseposition.text = "X:" + p.x.tostring( ).PadLeft( 4 ) + " " + "Y:" + p.y.tostring( ).PadLeft( 4 ); // マウス座標よりハンドル取得 IntPtr hwnd; hwnd = WindowFromPoint( p ); if ( hwnd == IntPtr.Zero ) txthandle.text = ""; return; txthandle.text = hwnd.tostring( ); // 現在のハンドルの座標値の取得 RECT r = new RECT( ); System.Text.StringBuilder s = new System.Text.StringBuilder( ); bool ret = GetWindowRect ( hwnd, ref r ); s.append( "Top : " + r.top.tostring( ) + " n"); s.append( "Left : " + r.left.tostring( ) + " n"); s.append( "Bottom : " + r.bottom.tostring( ) + " n"); s.append( "Right : " + r.right.tostring( )); lblformlocation.text = s.tostring( ); // クラス名の取得 System.Text.StringBuilder b = new System.Text.StringBuilder( 256 ); int n = 256; GetClassName( hwnd, b, n ); txtclass.text = b.tostring( ).Trim( ); // テキストの取得 GetWindowText( hwnd, b, n ); if ( b.tostring( ) == "" ) // GetWindowText でテキストが取得出来ない場合 // GetWindowText では他アプリケーションのテキストを取得出来ない SendMessage( hwnd, WM_GETTEXT, n, b ); -11-
txttext.text = b.tostring( ); トップレベルウィンドウのハンドルを総て列挙 画面上の総てのトップレベルウィンドウを列挙する 此の関数を呼び出すと 各ウィンドウのハンドルが順々にアプリケーション定義のコールバック関数に渡される EnumWindows 関数は 総てのトップレベルリンドウを列挙し終えるか 又は アプリケーション定義のコールバック関数から 0(False) が返される迄 処理を続ける Visual Basic Imports System.Runtime.InteropServices Public Class GetWindowsList Private Shared Function EnumWindows ( _ ByVal Proc As EnumWinProc, _ ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から呼び出されるアプリケーション定義の コールバック関数のプレースホルダー Private Delegate Function EnumWinProc ( _ ByVal hwnd As IntPtr, ByVal lparam As Integer ) As Boolean ' ボタン (GET) がクリックされた時の処理 Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click lstwindows.items.clear( ) EnumWindows( AddressOf AddWindowsToList, 0 ) ' EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 Private Function AddWindowsToList( ByVal hwnd As IntPtr, ByVal lparam As Integer ) _ As Boolean lstwindows.items.add( hwnd.tostring( )) Return True End Class C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace GetWindowsList -12-
public partial class GetWindowsList : Form [ DllImport ( "user32.dll" )] [return: MarshalAs( UnmanagedType.Bool )] static extern bool EnumWindows ( EnumWinProc lpenumfunc, IntPtr lparam ); // EnumWindows 関数から呼び出されるアプリケーション定義の // コールバック関数のプレースホルダー private delegate bool EnumWinProc ( IntPtr hwnd, int lparam ); public GetWindowsList( ) InitializeComponent( ); // ボタン (GET) がクリックされた時の処理 private void btnget_click ( object sender, EventArgs e ) lstwindows.items.clear( ); EnumWindows ( AddWindowsToList, IntPtr.Zero ); // EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 private bool AddWindowsToList ( IntPtr hwnd, int lparam ) lstwindows.items.add( hwnd.tostring( )); return true; 上記では 総てのトップレベルウィンドウを列挙して居るが 列挙を中止する場合 ( 例えば 対象と成るウィンドウを取得した場合等 ) は コールバック関数で Return False とする 指定した親ウィンドウに属する子ウィンドウを列挙 指定された親ウィンドウに属する子ウィンドウを列挙する 此の関数を呼び出すと 子ウィンドウのハンドルが順々にアプリケーション定義のコールバック関数に渡される EnumChildWindows 関数は 総ての子ウィンドウを列挙し終えるか 又は アプリケーション定義のコールバック関数から 0(False) が返される迄 列挙を続ける Visual Basic Imports System.Runtime.InteropServices Imports System.Text Public Class GetControlsList Private Shared Function FindWindow ( _ ByVal lpclassname As String, _ ByVal lpwindowname As String ) As IntPtr -13-
< DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ Private Shared Function EnumChildWindows ( _ ByVal hwndparent As System.IntPtr, _ ByVal lpenumfunc As EnumChildProc, _ ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から呼び出されるアプリケーション定義の ' コールバック関数のプレースホルダー Delegate Function EnumChildProc ( _ ByVal hwnd As IntPtr, ByVal lparam As Integer ) As Boolean ' ボタン (GET) がクリックされた時の処理 Private Sub btnget_click( ByVal sender As System.Object, ByVal e As System.EventArgs ) _ Handles btnget.click Dim hwnd As IntPtr = IntPtr.Zero ' 名前を付けて保存 ダイアログボックスのハンドルの取得 hwnd = FindWindow ( vbnullstring, " 名前を付けて保存 " ) If hwnd = IntPtr.Zero Then MessageBox.Show ( " ウィンドウが取得出来ません!" ) Exit Sub lstcontrols.items.clear( ) EnumChildWindows ( hwnd, AddressOf AddControlsToList, 0 ) ' EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 Private Function AddControlsToList ( ByVal hwnd As IntPtr, ByVal lparam As Integer ) _ As Boolean Dim S As String = "" ' コントロールのハンドルの取得 S &= hwnd.tostring( ).PadLeft( 8 ) S &= "(" & hwnd.tostring( "X" ).PadLeft( 8, "0"c ) & ") " ' リストボックスへ追加 lstcontrols.items.add( S ) ' 継続 Return True End Class using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; C# -14-
namespace GetControlsList public partial class GetControlsList : Form [ DllImport ( "user32.dll", SetLastError = true )] static extern IntPtr FindWindow ( string lpclassname, string lpwindowname ); [ DllImport ( "user32.dll" )] [ return: MarshalAs( UnmanagedType.Bool )] static extern bool EnumChildWindows ( IntPtr hwndparent, EnumChildProc lpenumfunc, IntPtr lparam ); // EnumWindows 関数から呼び出されるアプリケーション定義の // コールバック関数のプレースホルダー private delegate bool EnumChildProc ( IntPtr hwnd, int lparam ); public GetControlsList( ) InitializeComponent( ); // ボタン (GET) がクリックされた時の処理 private void btnget_click ( object sender, EventArgs e ) IntPtr hwnd = IntPtr.Zero; // 名前を付けて保存 ダイアログボックスのハンドルの取得 hwnd = FindWindow ( null, " 名前を付けて保存 " ); if ( hwnd == IntPtr.Zero ) MessageBox.Show ( " ウィンドウが取得出来ません!" ); return; lstcontrols.items.clear( ); EnumChildWindows( hwnd, AddControlsToList, IntPtr.Zero ); // EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 private bool AddControlsToList ( IntPtr hwnd, int lparam ) string s = ""; // コントロールのハンドルの取得 s += hwnd.tostring( ).PadLeft( 8 ); s += "(" + hwnd.tostring( "X" ).PadLeft( 8, '0' ) + ") "; // リストボックスへ追加 lstcontrols.items.add( s ); // 継続 return true; -15-
上記では 指定したウィンドウに含まれる総てのコントロールを列挙して居るが 列挙を中止する場合 ( 例えば 対象と成るウィンドウを取得した場合等 ) は コールバック関数で Return False とする トップレベルウィンドウと其のウィンドウに属する子ウィンドウのハンドルを総て列挙 前記 2 個のプログラムを組み合わせて トップレベルウィンドウと其のウィンドウに属する子ウィンドウのハンドルを総て列挙する例を 下記に示す Visual Basic Imports System.Runtime.InteropServices Public Class GetWinCtrlList '========================= ' トップレベルウィンドウ用 '========================= Private Shared Function EnumWindows ( _ ByVal Proc As EnumWinProc, _ ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から呼び出されるアプリケーション定義の ' コールバック関数のプレースホルダー Private Delegate Function EnumWinProc ( _ ByVal hwnd As IntPtr, ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 Private Function AddWindowsToList ( ByVal hwnd As IntPtr, ByVal lparam As Integer ) _ As Boolean Dim S As String = "" ' コントロールのハンドルの取得 S &= hwnd.tostring( ).PadLeft( 8, "0"c ) S &= "(" & hwnd.tostring( "X" ).PadLeft( 8, "0"c ) & ") " ' リストボックスへ追加 lstwinctrl.items.add( S ) ' コントロールの取得 EnumChildWindows ( hwnd, AddressOf AddControlsToList, 0 ) ' 継続 Return True '=============== ' コントロール用 '=============== < DllImport ( "user32.dll", CharSet:=CharSet.Auto )> _ -16-
Private Shared Function EnumChildWindows ( _ ByVal hwndparent As System.IntPtr, _ ByVal lpenumfunc As EnumChildProc, _ ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から呼び出されるアプリケーション定義の ' コールバック関数のプレースホルダー Delegate Function EnumChildProc ( _ ByVal hwnd As IntPtr, ByVal lparam As Integer ) As Boolean ' EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 Private Function AddControlsToList( ByVal hwnd As IntPtr, ByVal lparam As Integer ) _ As Boolean Dim S As String = " " ' コントロールのハンドルの取得 S &= hwnd.tostring( ).PadLeft( 8, "0"c ) S &= "(" & hwnd.tostring( "X" ).PadLeft( 8, "0"c ) & ") " ' リストボックスへ追加 lstwinctrl.items.add( S ) ' 継続 Return True '===================== ' イベントプロシージャ '===================== ' ボタン (GET) がクリックされた時の処理 Private Sub btnget_click(byval sender As System.Object, ByVal e As System.EventArgs) _ Handles btnget.click lstwinctrl.items.clear( ) EnumWindows( AddressOf AddWindowsToList, 0 ) End Class C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace GetWinCtrlList public partial class GetWinCtrlList : Form //========================= // トップレベルウィンドウ用 //========================= -17-
[ DllImport ( "user32.dll" )] [return: MarshalAs ( UnmanagedType.Bool )] static extern bool EnumWindows ( EnumWinProc lpenumfunc, IntPtr lparam ); // EnumWindows 関数から呼び出されるアプリケーション定義の // コールバック関数のプレースホルダー private delegate bool EnumWinProc ( IntPtr hwnd, int lparam ); // EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 private bool AddWindowsToList ( IntPtr hwnd, int lparam ) string s = ""; // コントロールのハンドルの取得 s += hwnd.tostring( ).PadLeft( 8, '0' ); s += "(" + hwnd.tostring( "X" ).PadLeft( 8, '0' ) + ") "; // リストボックスへ追加 lstwinctrl.items.add( s ); // コントロールの取得 EnumChildWindows ( hwnd, AddControlsToList, IntPtr.Zero ); // 継続 return true; //=============== // コントロール用 //=============== [ DllImport ( "user32.dll" )] [return: MarshalAs ( UnmanagedType.Bool )] static extern bool EnumChildWindows ( IntPtr hwndparent, EnumChildProc lpenumfunc, IntPtr lparam ); // EnumWindows 関数から呼び出されるアプリケーション定義の // コールバック関数のプレースホルダー private delegate bool EnumChildProc ( IntPtr hwnd, int lparam ); // EnumWindows 関数から実際に呼び出されるアプリケーション定義のコールバック関数 private bool AddControlsToList ( IntPtr hwnd, int lparam ) string s = " "; // コントロールのハンドルの取得 s += hwnd.tostring( ).PadLeft( 8, '0' ); s += "(" + hwnd.tostring( "X" ).PadLeft( 8, '0' ) + ") "; // リストボックスへ追加 lstwinctrl.items.add( s ); // 継続 return true; -18-
public GetWinCtrlList( ) InitializeComponent( ); //===================== // イベントプロシージャ //===================== // ボタン (GET) がクリックされた時の処理 private void btnget_click ( object sender, EventArgs e ) lstwinctrl.items.clear( ); EnumWindows ( AddWindowsToList, IntPtr.Zero ); 上記では 動作確認の為 総てのトップレベルウィンドウと其のウィンドウに属する子ウィンドウのハンドルを列挙して居るが トップレベルウィンドウのハンドルを取得した時 其のハンドルを使用してテキスト ( タイトルバーに表示されるキャプション ) を取得し 其のテキストに或る文字列が含まれる場合而巳 其のウィンドウに属する子ウィンドウのハンドルを取得する等すれば 実用に利用出来る -19-