Win32控制台工程中创建窗口

有的时候,用控制台同步输出调试信息、程序状态量,,比出Log、弹出报错对话框等方法来得有效。那么如何做到呢?如下:  看MSDN中这句:

If this parameter is NULL,GetModuleHandlereturns a handle to the file used to create the calling process.

  所以“GetModuleHandle(NULL)”返回的就是当前程序实例句柄。  例程:#include <iostream>#include <tchar.h>#include <windows.h>#define MAX_STR 100// 全局变量:HINSTANCE hInst; // 当前实例TCHAR szTitle[MAX_STR] = _TEXT("Console_Win Demo"); // 标题栏文本TCHAR szWindowClass[MAX_STR] = _TEXT("Console_Win Demo"); // 主窗口类名// 此代码模块中包含的函数的前向声明:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int _tmain(int argc, _TCHAR* argv[]){ std::cout << "Console_Win Demo" << std::endl << std::endl; std::cout << "================================" << std::endl << std::endl; HINSTANCE hInstance = NULL; int nCmdShow= SW_SHOW; // 该变量取值参见MSDN hInstance = GetModuleHandle(NULL); std::cout << "hInstance: " << hInstance << std::endl; std::cout << "hInstance->unused: " << hInstance->unused << std::endl << std::endl; std::cout << "================================" << std::endl << std::endl; MSG msg; MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { std::cout << "Error in InitInstance()!" << std::endl; return FALSE; } // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;}////函数: MyRegisterClass()////目的: 注册窗口类。//ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName= szWindowClass; wcex.hIconSm = NULL; return RegisterClassEx(&wcex);}//// 函数: InitInstance(HANDLE, int)//// 目的: 保存实例句柄并创建主窗口。//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ HWND hWnd; hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE;}////函数: WndProc(HWND, unsigned, WORD, LONG)////目的: 处理主窗口的消息。//LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_CREATE: std::cout << "Hello! I’m a CONSOLE. The WINDOW is my baby." << std::endl; break; case WM_COMMAND: break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); std::cout << "Goodbye!" << std::endl << std::endl; break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;}  最后,记得在Release版中用这句把控制台隐藏掉:#pragma comment(linker, "/subsystem:\&;quot;windows\&;quot; /entry:\&;quot;mainCRTStartup\&;quot;")

为何是一个人?也有善意的提醒:

Win32控制台工程中创建窗口

相关文章:

你感兴趣的文章:

标签云: