Windowsでウインドウを利用するプログラムでは、WinMain()関数を実装しますが、 WinMain()関数を実装すると、printfやcoutなどで標準出力(コンソール)に出力 する内容が表示されない為、手軽にprintfデバッグが行いにくくなります。そこで コンソールアプリケーションのままで、ウインドウを表示を行う手順について説明 します。
ウインドウを表示する為には、RegisterClassEx()関数と、CreateWindowEx()関数 を呼び出す必要があります。この際にコンソールアプリの場合、HINSTANCEを引数 に取らない為に、HINSTANCEを取得する必要があります。
HINSTANCEはGetModuleHande()関数にNULLを渡す事で取得する事ができます。 HINSTANCEを取得できれば、WinMain()関数を利用する時と同じ手順でウインドウ を表示する事が可能となります。
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <windows.h>
#include <wingdi.h>
/*
メインウインドウイベント処理
*/
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"
, tpCreateSt->lpCreateParams
, tpCreateSt->hInstance
, tpCreateSt->hMenu
, tpCreateSt->hwndParent
, tpCreateSt->cy
, tpCreateSt->cx
, tpCreateSt->y
, tpCreateSt->x
, tpCreateSt->style
, tpCreateSt->lpszName
, tpCreateSt->lpszClass
, tpCreateSt->dwExStyle
);
// ウインドウを表示する
::ShowWindow( hWnd, SW_SHOW );
}
break;
case WM_DESTROY:
//--------------------------------------------
// WM_DESTROY
//--------------------------------------------
{
// 終了する( 引数はそのまま終了コードとなります )
::PostQuitMessage( 0 );
}
break;
}
// デフォルト処理呼び出し
return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}
CREATESTRUCT
lpCreateParams = 0x12345678
hInstance = 0x01250000
hMenu = 0x00000000
hwndParent = 0x00000000
cy = 480
cx = 640
y = 225
x = 225
style = 0x00cf0000
lpszName = "コンソールアプリでウインドウを生成する"
lpszClass = "MainWindowClass"
dwExStyle = 0x00000100
