Windowsでウインドウを利用するプログラムでは、WinMain()関数を実装しますが、 WinMain()関数を実装すると、printfやcoutなどで標準出力(コンソール)に出力 する内容が表示されない為、手軽にprintfデバッグが行いにくくなります。そこで コンソールアプリケーションのままで、ウインドウを表示を行う手順について説明 します。
ウインドウを表示する為には、RegisterClassEx()関数と、CreateWindowEx()関数 を呼び出す必要があります。この際にコンソールアプリの場合、HINSTANCEを引数 に取らない為に、HINSTANCEを取得する必要があります。
HINSTANCEはGetModuleHande()関数にNULLを渡す事で取得する事ができます。 HINSTANCEを取得できれば、WinMain()関数を利用する時と同じ手順でウインドウ を表示する事が可能となります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #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