<转载>Win32控制台工程中创建窗口

有的时候,用控制台同步输出调试信息、程序状态量,比出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 CreateMyWindow()
{
    std::cout << "Console_Win Demo" << std::endl << std::endl;
    std::cout << "================================" << std::endl << std::endl;

    HINSTANCE hInstance = NULL;
    int       nCmdShow  = SW_SHOW;      // 该变量取值参见MSDN
/*
    GetModuleHandle(NULL);
    这将返回自身应用程序句柄
*/
    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;
}

 

 

 

posted @ 2013-11-18 20:38  z折腾  阅读(784)  评论(0编辑  收藏  举报