Win32控制檯工程中創建窗口
// Test.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include <tchar.h>
#include <iostream>
#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;
}
//
#include "stdafx.h"
#include <tchar.h>
#include <iostream>
#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.