わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

現在の仮想デスクトップ上にウインドウが存在するかを調べる

指定したウインドウが、現在の仮想デスクトップ上にウインドウが存在するかを調べるには、 IVirtualDesktopManagerのIsWindowOnCurrentVirtualDesktopメソッドを呼び出して確認します。 TRUEが返ってくれば、現在のデスクトップ上に指定した、ウインドウは存在します。 FALSEが返ってきたら、現在のデスクトップ上に指定した、ウインドウは存在しません。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <ShObjIdl.h>



// IServiceProvider
EXTERN_C const GUID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };



/*
	メインウインドウイベント処理
*/
LRESULT CALLBACK eMainWindowProc(
	  HWND	 hwnd	// handle to window
	, UINT	 uMsg	// message identifier
	, WPARAM wParam // first message parameter
	, LPARAM lParam // second message parameter
);



/*
	現在の仮想デスクトップ上にウインドウが存在するかを調べる
*/
int _tmain
(
	int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコードを表示できるようにする
	setlocale( LC_ALL, "Japanese" );

	WNDCLASSEX tWndClass;
	HINSTANCE  hInstance;
	TCHAR*	   cpClassName;
	TCHAR*	   cpWindowName;
	TCHAR*	   cpMenu;
	HWND	   hWnd;
	MSG		   tMsg;


	// アプリケーションインスタンス
	hInstance = ::GetModuleHandle(NULL);

	// クラス名称
	cpClassName = _T( "MainWindowClass" );

	// メニュー
	cpMenu = MAKEINTRESOURCE( NULL );

	// ウインドウ名称
	cpWindowName = _T("現在の仮想デスクトップ上にウインドウが存在するかを調べる");

	// ウインドウクラスパラメータセット
	tWndClass.cbSize        = sizeof(WNDCLASSEX);
	tWndClass.style         = CS_HREDRAW | CS_VREDRAW;
	tWndClass.lpfnWndProc   = eMainWindowProc;
	tWndClass.cbClsExtra    = 0;	// ::GetClassLong で取得可能なメモリ
	tWndClass.cbWndExtra    = 0;	// ::GetWindowLong で取得可能なメモリ
	tWndClass.hInstance     = hInstance;
	tWndClass.hIcon         = ::LoadIcon(NULL, IDI_APPLICATION);
	tWndClass.hCursor       = ::LoadCursor(NULL, IDC_ARROW);
	tWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	tWndClass.lpszMenuName  = cpMenu;
	tWndClass.lpszClassName = cpClassName;
	tWndClass.hIconSm       = NULL;

	// ウインドウクラス生成
	if (0 == ::RegisterClassEx(&tWndClass)) {

		/* 失敗 */
		return(-1);
	}

	// ウインドウを生成する
	hWnd = ::CreateWindowEx(
		0						  // extended window style
		, tWndClass.lpszClassName // pointer to registered class name
		, cpWindowName			  // pointer to window name
		, WS_OVERLAPPEDWINDOW	  // window style
		, CW_USEDEFAULT			  // horizontal position of window
		, CW_USEDEFAULT			  // vertical position of window
		, 640					  // window width
		, 480					  // window height
		, NULL					  // handle to parent or owner window
		, NULL					  // handle to menu, or child-window identifier
		, hInstance				  // handle to application instance
		, (VOID*)0x12345678		  // pointer to window-creation data
	);


	/*
	メッセージループ
	*/
	while( 0 != ::GetMessage( &tMsg, NULL, 0, 0 ) ) {
		::TranslateMessage( &tMsg );
		::DispatchMessage( &tMsg );
	}

	// WM_QUITの終了コードを返却する
	return( tMsg.wParam );
}



/*
メインウインドウイベント処理
*/
LRESULT CALLBACK eMainWindowProc
(
	  HWND	 hWnd	// handle to window
	, UINT	 uMsg	// message identifier
	, WPARAM wParam // first message parameter
	, LPARAM lParam // second message parameter
)
{
	switch (uMsg) {
	case WM_CREATE:
	//--------------------------------------------
	// WM_CREATE
	//--------------------------------------------
		{
			CREATESTRUCT* tpCreateSt = (CREATESTRUCT*)lParam;

			/* パラメータ表示 */
			wprintf(
				L"CREATESTRUCT¥n"
				L"¥tlpCreateParams = 0x%08x¥n"
				L"¥thInstance	   = 0x%08x¥n"
				L"¥thMenu		   = 0x%08x¥n"
				L"¥thwndParent	   = 0x%08x¥n"
				L"¥tcy			   = %d¥n"
				L"¥tcx			   = %d¥n"
				L"¥ty			   = %d¥n"
				L"¥tx			   = %d¥n"
				L"¥tstyle		   = 0x%08x¥n"
				L"¥tlpszName	   = ¥"%s¥"¥n"
				L"¥tlpszClass	   = ¥"%s¥"¥n"
				L"¥tdwExStyle	   = 0x%08x¥n"
				, (UINT)tpCreateSt->lpCreateParams
				, (UINT)tpCreateSt->hInstance
				, (UINT)tpCreateSt->hMenu
				, (UINT)tpCreateSt->hwndParent
				, tpCreateSt->cy
				, tpCreateSt->cx
				, tpCreateSt->y
				, tpCreateSt->x
				, tpCreateSt->style
				, tpCreateSt->lpszName
				, tpCreateSt->lpszClass
				, tpCreateSt->dwExStyle
			);

			// タイマー起動
			::SetTimer( hWnd, 0x12345678, 1000, NULL );

			// ウインドウを表示する
			::ShowWindow (hWnd, SW_SHOW );
		}
		break;


	case WM_DESTROY:
	//--------------------------------------------
	// WM_DESTROY
	//--------------------------------------------
		{
			// 終了する( 引数はそのまま終了コードとなります )
			::PostQuitMessage( 0 );
		}
		break;


	case WM_TIMER:
	//--------------------------------------------
	// WM_TIMER
	//--------------------------------------------
		{
			/*
				現在の仮想デスクトップ上にウインドウが存在するかを調べる
			*/
			{
				IServiceProvider*       pServiceProvider = NULL;
				IVirtualDesktopManager* pDesktopManager  = NULL;

				// IServiceProviderの取得
				HRESULT hResult = ::CoCreateInstance( CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER, __uuidof( IServiceProvider ), (PVOID*)&pServiceProvider );
				if (S_OK == hResult) {

					// IVirtualDesktopManagerの取得
					hResult = pServiceProvider->QueryService( __uuidof( IVirtualDesktopManager ), &pDesktopManager );
					if (S_OK == hResult) {
						BOOL bOnCurrentDesktop = FALSE;

						// ウインドウがカレントのデスクトップにいるかを取得
						pDesktopManager->IsWindowOnCurrentVirtualDesktop( hWnd, &bOnCurrentDesktop );

						// ウインドウがカレントのデスクトップにいるかを表示
						std::wcout << L"bOnCurrentDesktop = " << bOnCurrentDesktop << std::endl;

						// IVirtualDesktopManager解放
						pDesktopManager->Release();
					}

					// IServiceProvider解放
					pServiceProvider->Release();
				}
			}
		}
		break;
	}

	// デフォルト処理呼び出し
	return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}



実行結果

CREATESTRUCT
lpCreateParams = 0x12345678
hInstance = 0x01350000
hMenu = 0x00000000
hwndParent = 0x00000000
cy = 480
cx = 640
y = 208
x = 208
style = 0x00cf0000
lpszName = "現在の仮想デスクトップ上にウインドウが存在するかを調べる"
lpszClass = "MainWindowClass"
dwExStyle = 0x00000100
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 0
bOnCurrentDesktop = 0
bOnCurrentDesktop = 0
bOnCurrentDesktop = 0
bOnCurrentDesktop = 0
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1
bOnCurrentDesktop = 1






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。