プログラミング工房 > HSP > サンプルモジュール > HSPのウィンドウを確実にアクティブにする >

/***********************************************************

	HSPのウィンドウを確実にアクティブにする

		【2005/11/25 更新】


		命令一覧

		#deffunc activate_mywin int p1

			p1:	アクティブにしたいウィンドウのIDを指定する。
				現在の操作先ウィンドウでよいなら-1を指定してもよい。
				(数値変数も指定可)

		* この命令だけでうまく行かない場合は、この命令を
		 実行した直後に"gsel ?,1"や"gsel ?,2"を実行する
		 ようにして下さい。

		* このモジュールを作るにあたってはHSP2公式掲示板の
		 ログを参考にさせていただきました。

***********************************************************/
#module activate_hspwin

#const	TRUE		1
#const	FALSE		0

#uselib "user32.dll"
	#cfunc global GetForegroundWindow "GetForegroundWindow"
	#cfunc global GetWindowThreadProcessId "GetWindowThreadProcessId" sptr,sptr
	#func global AttachThreadInput "AttachThreadInput" sptr,sptr,sptr
	#func global SetForegroundWindow "SetForegroundWindow" sptr
#uselib "kernel32.dll"
	#cfunc global GetCurrentThreadId "GetCurrentThreadId"

/**********************************************************/
// 指定したIDのウィンドウを確実にアクティブにする
/**********************************************************/
#deffunc activate_mywin int p1
	/* 現在の操作先ウィンドウを記録 */
	selwin = ginfo(3)
	/* アクティブにするウィンドウを設定 */
	if p1>=0 {
		targetwin = p1
	}
	else {
		targetwin = selwin
	}
	/* 現在アクティブなウィンドウのハンドルを取得 */
	curwin = GetForegroundWindow()
	/* アクティブなウィンドウを作成したスレッドIDを取得 */
	pid = 0
	id1 = GetWindowThreadProcessId(curwin,varptr(pid))
	/* 自分のスレッドIDを取得 */
	id2 = GetCurrentThreadId()
	/* 自分のスレッドの入力処理機構をアクティブなウィンドウを作成したスレッドにアタッチ(?) */
	AttachThreadInput id2,id1,1
	/* 目的のウィンドウを前面に */
	gsel targetwin
	SetForegroundWindow hwnd
	/* デタッチ(?) */
	AttachThreadInput id2,id1,0
	/* 操作先ウィンドウを復帰 */
	gsel selwin
	return

/**********************************************************/
#global