| VB.NET | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 |
Option Strict On Imports System Imports System.Text Imports System.Runtime.InteropServices Class EmumerateWindows ' コールバックメソッドのデリゲート Private Delegate Function EnumerateWindowsCallback(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Integer ' EnumWindows API関数の宣言 <DllImport("user32", EntryPoint:="EnumWindows")> _ Private Shared Function EnumWindows( _ ByVal lpEnumFunc As EnumerateWindowsCallback, _ ByVal lParam As Integer) _ As Integer End Function ' GetWindowText API関数の宣言 <DllImport("user32", EntryPoint:="GetWindowText", CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowText( _ ByVal hWnd As IntPtr, _ ByVal lpString As StringBuilder, _ ByVal nMaxCount As Integer) _ As Integer End Function ' IsWindowVisible API関数の宣言 <DllImport("user32", EntryPoint:="IsWindowVisible")> _ Private Shared Function IsWindowVisible( _ ByVal hWnd As IntPtr) _ As Integer End Function ' ウィンドウを列挙するためのコールバックメソッド Public Shared Function EnumerateWindows(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Integer Dim sb As New StringBuilder(&H1000) ' ウィンドウが可視の場合 If IsWindowVisible(hWnd) <> 0 Then ' ウィンドウのキャプションを取得 If GetWindowText(hWnd, sb, &H1000) <> 0 Then ' 取得できたキャプションを表示 Console.WriteLine(sb.ToString()) End If End If ' 列挙を継続するには0以外を返す必要がある Return Not 0 End Function ' Mainメソッド Public Shared Sub Main() ' 列挙を開始 EnumWindows(AddressOf EnumerateWindows, 0) End Sub End Class |
| C# | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 |
using System; using System.Text; using System.Runtime.InteropServices; namespace EnumerateWindows { class EmumerateWindows { // コールバックメソッドのデリゲート private delegate int EnumerateWindowsCallback( IntPtr hWnd, int lParam ); // EnumWindows API関数の宣言 [DllImport( "user32", EntryPoint = "EnumWindows" )] private static extern int EnumWindows( EnumerateWindowsCallback lpEnumFunc, int lParam ); // GetWindowText API関数の宣言 [DllImport( "user32", EntryPoint = "GetWindowText", CharSet = CharSet.Auto )] private static extern int GetWindowText( IntPtr hWnd, StringBuilder lpString, int nMaxCount ); // IsWindowVisible API関数の宣言 [DllImport( "user32", EntryPoint = "IsWindowVisible" )] private static extern int IsWindowVisible( IntPtr hWnd ); // ウィンドウを列挙するためのコールバックメソッド public static int EnumerateWindows( IntPtr hWnd, int lParam ) { StringBuilder sb = new StringBuilder( 0x1000); // ウィンドウが可視の場合 if ( IsWindowVisible( hWnd ) != 0 ) { // ウィンドウのキャプションを取得 if ( GetWindowText( hWnd , sb , 0x1000 ) != 0 ) { // 取得できたキャプションを表示 Console.WriteLine( sb.ToString()); } } // 列挙を継続するには0以外を返す必要がある return 1; } // Mainメソッド [STAThread] public static void Main() { // 列挙を開始 EnumWindows( new EnumerateWindowsCallback( EnumerateWindows ), 0 ); } } } |
| 出力結果 | |
Light Executer E:\Visual C# .NET Program\Samples\EnumerateWindows\bin\Debug\EnumerateWindows.exe EnumerateWindows - Microsoft Visual C# .NET [デザイン] - Class1.cs EnumerateWindows - Microsoft Visual C# .NET [デザイン] - Class1.cs Windows Media Player VBCS翻訳機 version 1.2 CodeToHTML version 1.5 Program Manager Press any key to continue | |