SSブログ

ウィンドウを検索する [JavaScript]

他のプログラムが表示しているデータを欲しいときってあるけど、まあ無理。
せめてウインドウのタイトルだけでも、、ということでまずは調査。
以下のサイトを参考というか、HTMLアプリケーション(HTA)用に書き直しました。

Win32API ウィンドウを検索する FindWindowEx - s-kita’s blog
https://s-kita.hatenablog.com/entry/20121218/1355839657

コールバック関数、、よくわからなかったので、とりあえず動けばということで作りました。

windowkensaku.jpg
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9" >
<title>ウィンドウを検索する</title>
<style>
    table            {  width: 100%;  border-collapse: collapse;  border-spacing: 0; }
    th, td           {  padding: 0px;  text-align: left; }
    tr:nth-child(odd){  background-color: #eee; }
    tr:nth-child(1)  {  background-color: #ccc; }
</style>
<script>
var excel = new ActiveXObject( 'Excel.Application' );           // Excelオブジェクト生成
var tableHtml;                                                  // 表示する表のHTML
var depth;                                                      // ウインドウ階層の深さ

function EnumetareWindows(hWndParent) {
    var strClassName, strCaption;
    var isWinVisible  = excel.ExecuteExcel4Macro( 'CALL("user32", "IsWindowVisible", "JJ",   '  + hWndParent + ')' ) 
    if( isWinVisible || !document.getElementById( 'limitVisible' ).checked  ) {     // 可視Windowか、可視のみの指定がないなら表示する
        strClassName  = excel.ExecuteExcel4Macro( 'CALL("user32", "GetClassNameA"  , "2JFJ", '  + hWndParent +', 0, 128)' );
        strCaption    = excel.ExecuteExcel4Macro( 'CALL("user32", "GetWindowTextA" , "2JFJ", '  + hWndParent +', 0, 128)' );
        tableHtml  += ( isWinVisible ? '<tr>' : '<tr style="color:#aaa">' )         // 非可視Windowはグレーで表示
                   +  '<td style="text-indent:' + depth + 'rem">' + hWndParent + '</td><td>' + strClassName + '</td><td>' + strCaption + '</td></tr>';
    }
    
    var hWndChild     = excel.ExecuteExcel4Macro( 'CALL("user32", "FindWindowExA"  , "JJJJJ", ' + hWndParent + ', 0, 0, 0)' );
    if( !hWndChild )  return;
    if(  document.getElementById( 'limitDepth' ).checked  &&  depth ) return;
    depth++;
    do {
        EnumetareWindows(hWndChild);
        hWndChild     = excel.ExecuteExcel4Macro( 'CALL("user32", "FindWindowExA"  , "JJJJJ", ' + hWndParent + ', ' + hWndChild + ', 0, 0)' ); 
    } while ( hWndChild );
    depth--;
}

function getWindowInfo() {
    var hWndParent    = excel.ExecuteExcel4Macro( 'CALL("user32", "GetDesktopWindow", "J")' );
    tableHtml  = '<table><tr><th>window handle</th><th>class name</th><th>caption</th></tr>';
    depth = 0;
    EnumetareWindows( hWndParent );
    tableHtml += '</table>';
    document.getElementById( 'result' ).innerHTML = tableHtml;
}
</script>
</head>
<body>
<h1>ウィンドウを検索する</h1>
<p>
<form>
<input type="checkbox" id="limitVisible" checked ><label for="limitVisible">可視ウィンドウのみ</label> 
<input type="checkbox" id="limitDepth"   checked ><label for="limitDepth"  >親ウィンドウのみ</label> 
<input type="button" value="検索"   onclick="getWindowInfo()" >
<input type="reset"  value="クリア" onclick="document.getElementById('result').innerHTML=''" >
</form>
<p><div id="result"></div>
</body>
</html>

IE9モードにしたのは、表を「しましま」にするためだけです。
excel.ExecuteExcel4Macro(’CALL(~)’) でWindowsAPI関数を実行しています。
なので、エクセルがインストールされていることが前提です。
JJ、2JFJ、JJJJJとかは「CALL 関数と REGISTER 関数の使い方 - Microsoft Support」で。
タグ:HTA
nice!(0)  コメント(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。