2017.8.22
写给自己,开始接触DirectX,这是对自己学习的一个备份吧。
开始接触WinMain和WinProc,大部分都是照着书上敲的,也是为了以后能方便的找个参考
第一个代码:
#include<Windows.h>
#include"iostream"
#include <time.h>
using namespace std;
const string ProgramTitle = "Hello World!";
HWND window;
HDC device;
bool gameOver = false;
//从文件加载一个bmp,然后释放内存
//"E:\\Win32\\test1.bmp"
void DrawBitmap(char *fileName,int x,int y)
{
//LOAD BMP
HBITMAP image = (HBITMAP)LoadImage(0, fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//read the bitmap's properties
BITMAP bm;
GetObject(image, sizeof(BITMAP), &bm);
//create a device context from the bmp
HDC hdcImage = CreateCompatibleDC(device);
SelectObject(hdcImage, image);
//Draw the bitMap to Window
BitBlt(device, x, y, bm.bmWidth, bm.bmHeight, hdcImage, 0, 0, SRCCOPY);
DeleteDC(hdcImage);
DeleteObject((HBITMAP)image);
}
bool Game_Init()
{
//开始产生随机数
srand(time(NULL));
return true;
}
//UPDATE
void Game_Run()
{
if (gameOver)
return;
RECT rect;
GetClientRect(window, &rect);
//draw bitmap at random location
int x = rand() % (rect.right - rect.left);
int y = rand() % (rect.bottom - rect.top);
DrawBitmap("E:\\Win32\\test1.bmp", x, y);
}
void Game_End()
{
ReleaseDC(window, device);
}
//窗口回调过程,通过他将消息事件传递给程序
//接受并处理消息
//消息是在GetMessage中获取的
LRESULT CALLBACK WinProc(HWND hWnd, UINT message,
WPARAM wParam,LPARAM lParam)
{
//HWND:窗口句柄,使用窗口句柄创建一个新的设备环境句柄HDC,只要引用一个窗口或空间就必须得用到窗口句柄
RECT drawRect;
PAINTSTRUCT ps; //用于启动以及停止屏幕更新
HDC hdc;
switch (message)
{
/*
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps); //StartDrawing 锁住设备环境
for (int n = 0; n < 20; n++)
{
int x = n * 20;
int y = n * 20;
drawRect = { x, y, x + 100, y + 100 };
//写字符到屏幕
DrawText(hdc, ProgramTitle.c_str(), ProgramTitle.length(), &drawRect, DT_CENTER);
}
EndPaint(hWnd, &ps);
break;
*/
case WM_DESTROY:
gameOver = true;
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//设置窗口类的值
//Helper Function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hIntance)
{
//set the new window's properties
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW; //在移动或尺寸更新完|高度调整后重新绘制
wc.lpfnWndProc = (WNDPROC)WinProc; //返回一个指向回调函数的指针,如果不设定这个值,消息就无法传递给HWND
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hIntance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = ProgramTitle.c_str();
wc.hIconSm = NULL;
return RegisterClassEx(&wc); //如果成功则将窗口注册给了Windows,这个值将传会给InitInstance
}
//创建程序所需要的新窗口并显示,只运行一次
bool InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//从全局实例检查这个参数看看新实例是否需要终止
//Create
window = CreateWindow(
ProgramTitle.c_str(), //窗口类型
ProgramTitle.c_str(), //标题
WS_OVERLAPPEDWINDOW, //窗口格式 style
CW_USEDEFAULT,CW_USEDEFAULT,//位置
640,480, //长宽
NULL, //父窗口
NULL, //菜单
hInstance, //应用实例
NULL //窗口参数
);
if (window == 0) //如果创建gg
return false;
//显示
ShowWindow(window, nCmdShow);
UpdateWindow(window);
device = GetDC(window);
return true;
}
//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, LPSTR lpCmdLine, int nCmdShow)
{
//参数:被调用的程序实例,前一个实例(与1有关),传递给程序的命令行参数的字符串,显示方式
//创建窗口
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow))
{
return 0;
}
if (!Game_Init())
return 0;
//主消息循环
MSG msg;
//处理消息的主循环
//GetMessage参数:
/* LPMSG : 只想用于处理的msg的指针
HWND : 特定窗口的消息句柄,NULL则为所有消息
UINT MIN和MAX :范围
*/
while (!gameOver)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);//虚拟键盘消息翻译成字符信息
DispatchMessage(&msg);//发送回Windows消息系统
}
Game_Run();
}
Game_End();
return msg.wParam;
}