数组缓冲区与winapi 背景: 视窗10 WinAPI与C代码 问题:
我有一个像素缓冲区,表示为uint8_t buffer[width*height*pixel_SIZE],我希望定期修改内容数据并将缓冲区重新绘制到窗口中 我在winapi中遇到了两个问题,其中一个问题我不知道: 如何在窗口上打印像素缓冲区? 我以后如何在dame修改缓冲区的代码中重新绘制? 我做了很多研究,但没有一个代码片段能够成功地帮助我解决我的问题 下面是一个不起作用的代码示例,以显示我想用我拥有的代码元素归档的内容: 新图片 main.c 这是新的图片 函数new_image,返回像素缓冲区上的指针 一个函数,用于将图像放入显示窗口上像素缓冲区的窗口 sizeof运算符的用法不正确。位图可以是1、4、8、16、24或32位。32位位图每像素有4个字节。如果像素大小声明为int32\t,那么碰巧您得到了正确的大小。否则,请使用正确的公式计算尺寸 此外,将缓冲区复制到位也没有意义。您可以直接使用bits。只要没有破坏hDib,位就有效 对GetDC的调用应该以ReleaseDC结束,否则可能导致资源泄漏。Windows设备上下文不应存储为常量。将其用作临时值 您还将HDC context\u HDC声明为全局变量。这对于内存设备上下文来说是可以的,但不是必需的。您需要的唯一全局变量是hDib,可能还有buffer 解决方案 感谢@RemyLebeau@IInspectable@Raymond Chen和@BarmakShemirani answers的回答,这里有一个解决方案 现在,我根据CreateDIBSection函数获得的缓冲区成功地更新了窗口,而没有通过WM_PAINT事件 我使用UpdateLayeredWindow函数来更新窗口的像素 以下是解决方案代码: 新图片 main.c 以下是@IInspectable为像我这样的WinAPI初学者提供的必读资料:数组缓冲区与winapi 背景: 视窗10 WinAPI与C代码 问题:,c,windows,winapi,pixmap,C,Windows,Winapi,Pixmap,我有一个像素缓冲区,表示为uint8_t buffer[width*height*pixel_SIZE],我希望定期修改内容数据并将缓冲区重新绘制到窗口中 我在winapi中遇到了两个问题,其中一个问题我不知道: 如何在窗口上打印像素缓冲区? 我以后如何在dame修改缓冲区的代码中重新绘制? 我做了很多研究,但没有一个代码片段能够成功地帮助我解决我的问题 下面是一个不起作用的代码示例,以显示我想用我拥有的代码元素归档的内容: 新图片 main.c 这是新的图片 函数new_image,返回像素缓
在提问之前应该阅读此内容…如果UpdateWindow不起作用,您可能会触发重画,如果您的WM\u PAINT处理程序可以使用它,那么您可以详细说明更改的位。您在WM\u PAINT处理程序外部绘制,因此您绘制的任何内容都将在下次绘制请求时被覆盖。您的WM_绘制处理程序不会绘制任何内容,因此它只会绘制您的wc.hbrBackground.Mandatory读数:。确保阅读所有内容。无论如何,你必须能够在WM_PAINT内完成所有绘图,因为如果用户最小化窗口,然后恢复窗口,你需要重新生成内容。如果你在WM_PAINT之外画画,你需要记住足够的信息,以便WM_PAINT可以复制你所做的。大多数人只是让WM_画画。避免代码重复。有两种方法可以处理这种情况:1是处理WM_PAINT的旧方法,即在每次接收WM_PAINT时,将像素放入HBITMAP,然后重新绘制到窗口的HDC上;2使用WS_EX_分层窗口样式和UpdateLayeredWindow函数的新方法。将像素放入HBITMAP并创建一个内存中的HDC以将其选中,然后将该HDC传递给UpdateLayeredWindow,并且根本不处理WM_绘制。每当像素改变时,只需再次调用UpdateLayeredWindow即可。感谢您的回复。我试着从你的例子中写入BYTE*image=new_image500500;就在调用之后,按这种方式:对于大小\u t i=0;i<500*500;i++{uint32_t*image[i]=0xF0;}但实际上它正在失败。字节*图像似乎没有被CreateDIBSection分配大小为500*500
// 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);
}
CopyMemory(bits, buffer, width * height * sizeof(PIXEL_SIZE));
hdc = GetDC(hwnd);
#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);
}
// global variables
static HBITMAP hDib;
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;
void *buffer;
HDC hdc;
set_bmi_object(&bmi, width, height);
hdc = GetDC(hwnd);
hDib = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)(&buffer), NULL, 0);
ReleaseDC(instance->win_list->hwnd, hdc);
return (buffer);
}
// Print the buffer of pixel on the window
void put_image_to_window(HWND hwnd, void *buffer, int x, int y)
{
HDC hdc;
HDC context_hdc;
HGDIOBJ old_obj;
hdc = GetDC(hwnd);
context_hdc = CreateCompatibleDC(hdc);
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);
ReleaseDC(hwnd, hdc);
// Call UpdateLayeredWindow
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 128;// half transparent
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptLocation = {x, y};
SIZE szWnd = {500, 500};
POINT ptSrc = {0, 0};
UpdateLayeredWindow(hwnd, hdc, &ptLocation, &szWnd, context_hdc, &ptSrc, 0, &blend, ULW_ALPHA);
}
static const char g_szClassName[] = "myWindowClass";
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;
}
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);
// The Message Loop
while (GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (Msg.wParam);
}