Win32控制台工程中创建窗口

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\"")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值