gpt4 book ai didi

从像素缓冲区创建 HBITMAP 然后渲染

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:06 27 4
gpt4 key购买 nike

我想使用 CreateBitmap() 从一个简单的 unsigned char 像素缓冲区(每个像素 3 个字节)创建一个 HBITMAP 对象这……

HBITMAP hbm = CreateBitmap(width, height, 1, 24, buffer);

在我尝试渲染它之前,它似乎工作正常。现在,我只想保持简单,在屏幕上绘制一个 100 x 100 的黑色像素正方形,但什么也没有显示。

这是我做的最小设置:

HBITMAP hbm = NULL;

void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

static unsigned char buffer[30000] = {0};
hbm = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);

HBITMAP hbmOld = SelectObject(hdcMem, hbm);
BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {

case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
case WM_CLOSE: DestroyWindow(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wParam, lParam);

}

return 0;

}

这是我的全部代码,如果你想复制它......

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct {

const char *wc_name;
const char *title;

unsigned int width;
unsigned int height;

} Window;

static HBITMAP hbmp = NULL;
static PAINTSTRUCT ps;

void window_create(Window *window, const char *wc_name, const char *title, unsigned int width, unsigned int height) {

window->wc_name = wc_name;
window->title = title;

window->width = width;
window->height = height;

}
void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

static unsigned char buffer[30000] = {0};
hbmp = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);

HBITMAP hbmOld = SelectObject(hdcMem, hbmp);
BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);;

EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {

case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
case WM_CLOSE: DestroyWindow(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wParam, lParam);

}

return 0;

}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

Window window;
window_create(&window, "Parent Window", "My Window", 1000, 1000 / 16 * 9);

WNDCLASSEX wc = {0};
MSG msg = {0};
HWND hwnd = NULL;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszClassName = window.wc_name;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc)) {

MessageBox(NULL, "Failed to register application window!", "WC Registration Error!", MB_ICONEXCLAMATION | MB_OK);
return -1;

}

hwnd = CreateWindowEx(

0, window.wc_name, window.title,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
window.width, window.height, NULL, NULL, hInstance, NULL

);

if (!hwnd) {

MessageBox(NULL, "Failed to launch application window!", "HWND Creation Error!", MB_ICONEXCLAMATION | MB_OK);
return -1;

}

ShowWindow(hwnd, SW_MAXIMIZE);
UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0) > 0) {

TranslateMessage(&msg);
DispatchMessage(&msg);

}

return msg.wParam;

}

最佳答案

CreateBitmap 生成设备相关位图的句柄,这与 CreateDIBSection 不同,后者保证在成功时使用设备上下文创 build 备无关位图,并允许您访问位图中的实际位,这与 CreateCompatibleBitmap 不同。

如何使用CreateDIBSection:

HDC hdc = GetDC(hwnd);
BITMAPINFO bi = { 0 };
static BYTE *bits = NULL;
static unsigned char buffer[30000] = { 0 };

bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = 100;
bi.bmiHeader.biHeight = -100;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;

hbmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
CopyMemory(bits, buffer, 30000);
ReleaseDC(hwnd, hdc);

关于从像素缓冲区创建 HBITMAP 然后渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52124229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com