#include "stdafx.h"
#include
#include
//回调函数
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// 当前实例的指针(句柄) 上一个实例的指针(句柄) 命令行(给这个exe传递的信息) 是否显示窗口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
//1 注册窗口类
::WNDCLASSEXA winClass;
winClass.lpszClassName ="Raster" ;//*
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;//*回到函数的指针
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground=(HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
//2 创建窗口
//返回的窗口句柄
HWND hWnd = CreateWindowExA(
NULL,
"Raster",//更具注册的名称找到注册窗口类的数据
"Raster",//窗口的标题
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,//窗口的风格
0,//位置
0,
480,//宽高
320,
0,//菜单
0,//父窗口
hInstance,//句柄
0//用户自定义的变量
);
UpdateWindow(hWnd);//更新窗口
ShowWindow(hWnd, SW_SHOW);//显示窗口
//用于得到窗口的大小
RECT rt = { 0 };
GetClientRect(hWnd, &rt);//传递一个窗口句柄,返回窗口大小
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
//HDC是Windows的设备描述表句柄。
HDC hDC = GetDC(hWnd);
// CreateCompatibleDC解释: https://blog.csdn.net/phenixyf/article/details/7916646
HDC hMem = ::CreateCompatibleDC(hDC);
//创建一张图片
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize =sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth =width;
bmpInfor.bmiHeader.biHeight =height;
bmpInfor.bmiHeader.biPlanes =1;
bmpInfor.bmiHeader.biBitCount =32;
bmpInfor.bmiHeader.biCompression=BI_RGB;
bmpInfor.bmiHeader.biSizeImage =0;
bmpInfor.bmiHeader.biXPelsPerMeter=0;
bmpInfor.bmiHeader.biYPelsPerMeter=0;
bmpInfor.bmiHeader.biClrUsed =0;
bmpInfor.bmiHeader.biClrImportant=0;
//创建一张位图
HBITMAP hBmp = CreateDIBSection(hDC, &bmpInfor, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
SelectObject(hMem, hBmp);//内存DC与图片关联到一起
memset(buffer, 0, width*height * 4);
//window 消息循环
MSG msg = { 0 };
while (true)
{
if (msg.message == WM_DESTROY
|| msg.message == WM_CLOSE
|| msg.message == WM_QUIT)
{
break;
}
//从队列中查看是否有消息 消息在window中为数字 这是是设置消息的最大值,最小值,全是0,默认是所有
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);//解释消息
DispatchMessage((&msg));//分发消息
}
//memset是计算机中C/C++语言初始化函数。作用是将某一块内存中的内容全部设置为指定的值, 这个函数通常为新申请的内存做初始化工作。
memset(buffer, 0, width*height * 4);
unsigned char* rgba = (unsigned char*)buffer;//将buffer转换成char类型
int pitch = width * 4;
memset(rgba+pitch * (height/2), 255, pitch);
BitBlt(hDC, 0, 0, width, height, hMem, 0, 0, SRCCOPY);
}
return 0;
}