编译器:Microsoft Visual Basic 2015
入口代码:
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
//回调函数
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_POPUPWINDOW,//窗口的风格
0,//位置
0,
256,//宽高
256,
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);
CELL::Raster raster(width, height, buffer);
//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));//分发消息
}
raster.clear();
for (int i = 0; i < 100; ++i) {
raster.drawPoint(rand() % 256, rand() % 256, CELL::Rgba(255, 0, 0), 2);
}
memcpy(buffer, raster._buffer, raster._width*raster._height*sizeof(CELL::Rgba));
BitBlt(hDC, 0, 0, width, height, hMem, 0, 0, SRCCOPY);
}
return 0;
}
.h文件:
namespace CELL {
class Raster {
public:
Rgba* _buffer;
int _width;
int _height;
public:
Raster(int w, int h, void *buffer);
~Raster(void) {
// delete []_buffer;
}
void clear();
void drawPoint(int x, int y, Rgba color, int ptSize);
public:
inline void setPixedl(unsigned x, unsigned y, Rgba color) {
if (x >= _width || y >= _height) {
return;
}
_buffer[y*_width + x] = color;
}
};
}
.cpp文件:
#include "stdafx.h"
#include "Raster.h"
namespace CELL
{
Raster::Raster(int w, int h, void* buffer) {
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
void Raster:: clear() {
memset(_buffer, 0, _width*_height * sizeof(Rgba));
}
void Raster::drawPoint(int x, int y, Rgba color, int ptSize) {
switch (ptSize)
{
case 1:
setPixedl(x, y, color);
break;
case 2:
setPixedl(x + 0, y + 0, color);
setPixedl(x + 1, y + 0, color);
setPixedl(x + 0, y + 1, color);
setPixedl(x + 1, y + 1, color);
break;
default:
break;
}
}
}
需要引入:CELLMath.hpp
CELLMath.hpp 下载地址:https://download.csdn.net/download/li_jin_/12401729
效果图: