Win32控制台工程中创建窗口
2013-01-29 19:25
387 查看
http://bbs.gameres.com/thread_63553_1_1.html
有的时候,用控制台同步输出调试信息、程序状态量,比出Log、弹出报错对话框等方法来得有效。那么如何做到呢?如下:
简而言之,用GetModuleHandle()函数获得当前程序实例句柄,其它地方与常见的Win32创建窗体方法相同。
看MSDN中这句:
If this parameter is NULL, GetModuleHandle returns 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:\"windows\" /entry:\"mainCRTStartup\"")
有的时候,用控制台同步输出调试信息、程序状态量,比出Log、弹出报错对话框等方法来得有效。那么如何做到呢?如下:
简而言之,用GetModuleHandle()函数获得当前程序实例句柄,其它地方与常见的Win32创建窗体方法相同。
看MSDN中这句:
If this parameter is NULL, GetModuleHandle returns 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:\"windows\" /entry:\"mainCRTStartup\"")
相关文章推荐
- Win32控制台工程中创建窗口
- <转载>Win32控制台工程中创建窗口
- 从win32控制台创建窗口过程
- Win32应用程序中创建控制台窗口
- opengl 使用 vc6.0中win32控制台工程去掉控制台窗口
- Win32应用程序中创建控制台窗口
- win下创建win32控制台工程,执行lua脚本
- 用Win32 API创建不规则窗口
- 用Win32 API创建不规则窗口
- 【OpenGL】VS2005下创建基于Win32项目的OpenGL窗口
- [Win32]创建模态窗口
- Win32应用中创建多窗口
- win32程序窗口的创建
- 用2005创建WIN32控制台程序
- MFC程序对win32的封装总结(窗口类的注册,窗口的创建,窗口的窗口过程指定)
- Win32应用程序工程学习笔记一 - 构建第一个窗口
- VC中控制台程序如何创建窗口
- VC++2005运行Win32控制台程序时,为什么弹不出控制台窗口(无法看到运行结果)?
- VC/win32 windows 窗口的注册和创建
- win32创建窗口及其消息循环