c - 数组缓冲区和winapi
问题描述
语境 :
- 视窗 10
- WinAPI 和 C 代码
问题 :
我有一个像素缓冲区,表示uint8_t buffer[width * height * PIXEL_SIZE]
我想定期修改内容数据并将缓冲区重绘到窗口中。
我遇到了两个与我迷路的winapi有关的问题:
- 如何在窗口上打印像素缓冲区?
- 我如何稍后在 dame 修改缓冲区的代码中重绘?
我做了很多研究,但没有代码片段成功地帮助我解决了我的问题。
这是一个不工作的代码示例,以显示我想用我拥有的代码元素归档的内容:
new_image.c
// Global variables
static HDC hdc;
static HDC context_hdc;
static HBITMAP hDib;
static HGDIOBJ obj;
static void set_bmi_object(BITMAPINFO *bmi, int width, int height) {
memset(bmi, 0, sizeof(BITMAPINFO));
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = width;
bmi->bmiHeader.biHeight = -height;
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biBitCount = 32;
bmi->bmiHeader.biCompression = BI_RGB;
}
// Allocate a new image buffer
void *new_image(HWND hwnd, int width, int height)
{
BITMAPINFO bmi;
BYTE *bits = NULL;
void *buffer;
if (NULL == (buffer = (char*)malloc(width * height * PIXEL_SIZE)))
return (NULL);
set_bmi_object(&bmi, width, height);
hdc = GetDC(hwnd);
hDib = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)(&bits),
NULL, 0);
if (hDib != NULL) {
context_hdc = CreateCompatibleDC(hdc);
if (context_hdc == NULL) {
DeleteObject(hDib);
} else {
obj = SelectObject(context_hdc, hDib);
CopyMemory(bits, buffer, width * height * sizeof(PIXEL_SIZE));
}
}
return (newimg);
}
// Print the buffer of pixel on the window
void put_image_to_window(HWND hwnd, void *buffer, int x, int y)
{
(void)hwnd;
// Void buffer because i should use directly HDCcontext_hdc linked to HGDIOBJ obj ?
(void)buffer;
BitBlt(hdc, // destination
x,
y,
500, // width of the region
500, // height
context_hdc, // source
0, // x
0, // y
SRCCOPY);
UpdateWindow(hwnd);
}
主程序
static const char g_szClassName[] = "myWindowClass";
static void paint(HWND hwnd) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
paint(hwnd);
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int main(void) {
MSG Msg;
HINSTANCE hInstance;
HWND hwnd;
STARTUPINFOA startup_info;
WNDCLASSEX wc;
HWND hwnd;
GetStartupInfoA(&startup_info);
hInstance = GetModuleHandle(NULL);
memset(&wc, 0, sizeof(wc));
// Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
// ... etc
wc.lpszClassName = TEXT(g_szClassName);
if (!RegisterClassEx(&wc)) {
return (-1);
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Title,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL) {
return (-1);
}
ShowWindow(hwnd, startup_info.wShowWindow);
image = new_image(hwnd, 500, 500);
put_image_to_window(hwnd, image, 0, 0);
UpdateWindow(hwnd);
// The Message Loop
while (GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (Msg.wParam);
}
这是在new_image.c
一个返回像素缓冲区指针的函数
new_image
,一个
put_image_to_window
在窗口上显示像素缓冲区的函数。
解决方案
CopyMemory(bits, buffer, width * height * sizeof(PIXEL_SIZE));
运算符的使用sizeof
不正确。位图可以是 1、4、8、16、24 或 32 位。32 位位图每个像素有 4 个字节。如果PIXEL_SIZE
被声明为int32_t
那么巧合你得到正确的大小。否则使用正确的公式来计算大小。
此外,复制buffer
到bits
. 你可以bits
直接使用。bits
只要你没有销毁就有效hDib
hdc = GetDC(hwnd);
调用GetDC
应该以 结束ReleaseDC
,否则你可能会导致资源泄漏。Windows 设备上下文并不意味着存储为常量。将其用作临时值。
您还声明HDC context_hdc
为全局变量。这对于内存设备上下文是可以的,但这不是必需的。您需要的唯一全局变量hDib
可能是buffer
#include <windows.h>
static const char g_szClassName[] = "myWindowClass";
static HBITMAP hDib;
BYTE *new_image(int width, int height)
{
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
BYTE *buffer;
HDC hdc = GetDC(HWND_DESKTOP);
hDib = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)(&buffer), NULL, 0);
ReleaseDC(HWND_DESKTOP, hdc);
return buffer;
}
static void paint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if(hDib)
{
HDC context_hdc = CreateCompatibleDC(hdc);
HGDIOBJ old_obj = SelectObject(context_hdc, hDib);
BitBlt(hdc, 0, 0, 500, 500, context_hdc, 0, 0, SRCCOPY);
SelectObject(context_hdc, old_obj);
DeleteDC(context_hdc);
}
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
paint(hwnd);
break;
case WM_MOUSEMOVE:
{
if(hDib)
{
//draw something
HDC context_hdc = CreateCompatibleDC(NULL);
HGDIOBJ old_obj = SelectObject(context_hdc, hDib);
SetDCBrushColor(context_hdc, RGB(255, 0, 0));
int x = (int)(short)LOWORD(lParam);
int y = (int)(short)HIWORD(lParam);
RECT rc = { x, y, x + 10, y + 10};
FillRect(context_hdc, &rc, (HBRUSH)GetStockObject(DC_BRUSH));
SelectObject(context_hdc, old_obj);
DeleteDC(context_hdc);
InvalidateRect(hwnd, NULL, FALSE);
}
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int main(void) {
MSG Msg;
HINSTANCE hInstance;
HWND hwnd;
STARTUPINFOA startup_info;
WNDCLASSEX wc;
GetStartupInfoA(&startup_info);
hInstance = GetModuleHandle(NULL);
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = TEXT(g_szClassName);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Title",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 500, NULL, NULL, hInstance, NULL);
BYTE* image = new_image(500, 500);
ShowWindow(hwnd, SW_SHOW);//startup_info.wShowWindow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if (hDib)
DeleteObject(hDib);
return (Msg.wParam);
}
推荐阅读
- python-3.x - 我的多线程进程无法正常工作
- ruby-on-rails - 在 Rails 中为 Wikimedia Maps 添加电子邮件到 HTTP 用户代理
- javascript - 当我在反应 js 中运行构建文件时,UI 出现放大
- c# - webApi OAuthAuthorizationServerProvider 我该如何编写注销
- python - Python 2.7 _ 添加到字符串会删除字符串?
- ruby - 从控制台读取输入后程序不继续
- python - 'AttributeError:模块'usbiss'在创建类实例时没有属性Aclass'
- wpf - DotTrace 没有明确的大量用户代码执行时间
- java - GWTP Presenter 和 View 保留旧值
- json - JSON代码beutify得到一个错误期望'STRING','NUMBER','NULL','TRUE','FALSE','{','[',得到'undefined'