GDI绘制图形

GDI绘制图形

基本流程c++

 gdi_base

代码示例web

// WindowsProject1.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "WindowsProject1.h"
#include <cstdio>

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

void Render();

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                      _In_opt_ HINSTANCE hPrevInstance,
                      _In_ LPWSTR lpCmdLine,
                      _In_ int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// TODO: 在此处放置代码。

	// 初始化全局字符串
	LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}

	HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));

	MSG msg;

	// 主消息循环:
	while (GetMessage(&msg, nullptr, 0, 0))
	{
		// 若是消息可获得,返回非零值;若是没有消息可获得,返回值是零。
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Sleep(40);
			Render();
		}
	}
	return (int)msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEXW wcex;
	// 注册窗口类型
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassExW(&wcex);
}

const SSIZE_T pic_width = 320;
const SSIZE_T pic_height = 180;

static HWND g_hwnd;
static FILE* g_fp;

void Render();

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance; // 将实例句柄存储在全局变量中

	HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
	                          100, 100, pic_width, pic_height, nullptr, nullptr, hInstance, nullptr);

	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	g_hwnd = hWnd;

	int err = fopen_s(&g_fp, "./test_rgb24_320x180.rgb", "rb+");

	if (err)
	{
		printf_s("%d", err);
		fprintf(stderr, "Can't Open Source File \n");
		getchar();
		exit(-1);
	}
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (msg.message != WM_QUIT)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Sleep(40);
			Render();
		}
	}
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

static unsigned char buffer[pic_width * pic_height * 8];

void CHANGE_ENDIAN_PIC(unsigned char* image, const SSIZE_T pic_width, const SSIZE_T pic_height, int i);

void Render()
{
    // 在结束后从新播放
	if (fread(buffer, 1, pic_height * pic_width * 24 / 8, g_fp) != pic_height * pic_width * 24 / 8)
	{
		fseek(g_fp, 0, SEEK_SET);
		fread(buffer, 1, pic_height * pic_width * 24 / 8, g_fp);
	}

	HDC hdc = GetDC(g_hwnd);

	CHANGE_ENDIAN_PIC(buffer, pic_width, pic_height, 24);

	RECT rect;
	GetWindowRect(g_hwnd, &rect);

	size_t screen_w = rect.right - rect.left;
	size_t screen_h = rect.bottom - rect.top;

	BITMAPINFO bmpInfo = {0};
	DWORD dwBmpHdr = sizeof(BITMAPINFO);
	// BMP格式 
	bmpInfo.bmiHeader.biBitCount = 24; // 每一个像素是8bit,RGB三个像素为24bit
	bmpInfo.bmiHeader.biClrImportant = 0;
	bmpInfo.bmiHeader.biSize = dwBmpHdr;
	bmpInfo.bmiHeader.biSizeImage = 0;
	bmpInfo.bmiHeader.biWidth = pic_width;
	bmpInfo.bmiHeader.biHeight = -pic_height;
	bmpInfo.bmiHeader.biXPelsPerMeter = 0;
	bmpInfo.bmiHeader.biYPelsPerMeter = 0;
	bmpInfo.bmiHeader.biClrUsed = 0;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biCompression = BI_RGB;

	int nResult = StretchDIBits(hdc, 0, 0, screen_w, screen_h, 0, 0, pic_width, pic_height, buffer, &bmpInfo,
	                            DIB_RGB_COLORS, SRCCOPY);

	ReleaseDC(g_hwnd, hdc);
}

void CHANGE_ENDIAN_24(unsigned char* data)
{
	char temp2 = data[2];
	data[2] = data[0];
	data[0] = temp2;
}

void CHANGE_ENDIAN_PIC(unsigned char* image, const SSIZE_T w, const SSIZE_T h, int bpp)
{
	unsigned char* pixeldata = NULL;
	for (int i = 0; i < h; i++)
		for (int j = 0; j < w; j++)
		{
			pixeldata = image + (i * w + j) * bpp / 8;
			if (bpp == 24)
			{
				CHANGE_ENDIAN_24(pixeldata);
			}
		}
}

须要注意的就是位图的格式,不少的视频播放都是转换为位图,好比 RGB 须要转换为 BGR , 以及大小端的调整。svg