Win32 - 为什么没有任何东西被绘制到屏幕上? [英] Win32 - why is nothing being drawn to the screen?

查看:24
本文介绍了Win32 - 为什么没有任何东西被绘制到屏幕上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用屏幕外缓冲区,以便我可以跟踪 WM_PAINT 之前/之后对屏幕的更改,并且只需通过 WM_PAINT 中的一行复制它们.这是我必须设置图形的一些代码:

I'm trying to use an off-screen buffer so that I can keep track of changes to the screen before/after WM_PAINT and just copy them through one line in WM_PAINT. Here's some code I have to set up the graphics:

hdc = GetDC(hWnd);
hdcmem = CreateCompatibleDC(hdc);
hbcmem = CreateCompatibleDC(hdcmem);

// Load bitmaps
bg = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
side = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEINFO));
mainCont = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_GAME_CONT));
if(bg == NULL || side == NULL || mainCont == NULL)
            ThrowError("A bitmap failed to load.");

// Background
hdcold = (HBITMAP)SelectObject(hbcmem, bg);
BitBlt(hdcmem, 0, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237 * 2, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 0, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237 * 2, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);

// Side Info
hdcold = (HBITMAP)SelectObject(hbcmem, side);
BitBlt(hdcmem, 339, 26, 154, 300, hbcmem, 0, 0, SRCCOPY);

// Main Game Container
hdcold = (HBITMAP)SelectObject(hbcmem, mainCont);
BitBlt(hdcmem, 26, 26, 300, 300, hbcmem, 0, 0, SRCCOPY);

hdc、hdcmem、hbcmem、hdcold、bg、side 和 mainCont 之前已声明.它们的范围包括此文件中的所有内容(包括此代码和 WM_PAINT 中的代码).

hdc, hdcmem, hbcmem, hdcold, bg, side, and mainCont are declared previously. Their scope includes everything in this file (including this code and the code in WM_PAINT).

这是 WM_PAINT 中的代码:

Here's the code in WM_PAINT:

PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);

BitBlt(hdc, 0, 0, 518, 401, hdcmem, 0, 0, SRCCOPY);

EndPaint(hWnd, &ps);

出于某种原因,屏幕上没有绘制任何内容.我绞尽脑汁想弄清楚.一个正确方向的指针将不胜感激.

For some reason, nothing is being drawn to the screen. I'm racking my brain trying to figure it out. A pointer in the right direction would be much appreciated.

推荐答案

首先为您的内存设备上下文创建一个兼容的位图,然后将该位图选择到内存 dc 中,它应该可以工作!

Create a compatible bitmap for your memory device context first, then select that bitmap to the memory dc and it should work !

hdc = GetDC(hWnd); // used only to create compatibles.
hdcmem = CreateCompatibleDC(hdc);
hbcmem = CreateCompatibleDC(hdc);

// Create client-area-sized compatible bitmap.
RECT rc;
GetClientRect(hWnd, &rc);
HBITMAP hbm_memdc = CreateComptibleBitmap(hdc, rc.right, rc.bottom);
HBITMAP hbm_memdc_old = (HBITMAP)SelectObject(hdcmem, hbm_memdc)

ReleaseDC(hdc); // this no longer needed

// now start rendering into hdcmem... 

在关闭时销毁自定义的位图句柄之前,您需要保持选中旧位图句柄以将其放回原处.您如何管理完全取决于您.

You'll want to keep the old bitmap handle selected out to put it back before destroying your custom one on shutdown. How you manage that is entirely up to you.

这篇关于Win32 - 为什么没有任何东西被绘制到屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆