win32程序启用控制台

 1 #include <cstdio>
 2 #define USE_WIN32_CONSOLE
 3 
 4 int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
 5                      _In_opt_ HINSTANCE hPrevInstance,
 6                      _In_ LPWSTR    lpCmdLine,
 7                      _In_ int       nCmdShow)
 8 {
 9     UNREFERENCED_PARAMETER(hPrevInstance);
10     UNREFERENCED_PARAMETER(lpCmdLine);
11 
12 #ifdef USE_WIN32_CONSOLE
13     AllocConsole();
14     freopen("CONIN$", "r", stdin);
15     freopen("CONOUT$", "w", stdout);
16     freopen("CONOUT$", "w", stderr);
17 #endif
18 
19     // TODO: Place code here.
20 
21     // Initialize global strings
22     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
23     LoadStringW(hInstance, IDC_WIN32TEST, szWindowClass, MAX_LOADSTRING);
24     MyRegisterClass(hInstance);
25 
26     // Perform application initialization:
27     if (!InitInstance (hInstance, nCmdShow))
28     {
29         return FALSE;
30     }
31 
32     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32TEST));
33 
34     MSG msg;
35 
36     // Main message loop:
37     while (GetMessage(&msg, nullptr, 0, 0))
38     {
39         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
40         {
41             TranslateMessage(&msg);
42             DispatchMessage(&msg);
43         }
44     }
45 
46 #ifdef USE_WIN32_CONSOLE
47     FreeConsole();
48 #endif
49 
50     return (int) msg.wParam;
51 }

主要是12-17行,46-48行的部分.

posted on 2016-02-24 10:06  Ayahime  阅读(329)  评论(0编辑  收藏  举报