qq_39479426 2017-07-19 07:47 采纳率: 0%
浏览 1010

关于两个内存DC不能加载位图的问题

例如声明了两个内存DC mdc和bufdc,先将位图bg加载到bufdc中,再将bufdc加载到mdc中,结果就不能显示位图了,这是为什么?
源代码如下:
#include "stdafx.h"
#include "Win32Project5.h"

// 全局变量:
HINSTANCE hInst; // 当前实例
HDC hdc, mdc, bufdc;
HBITMAP bg;

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

int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

while (GetMessage(&msg, nullptr, 0, 0))
{
        TranslateMessage(&msg);
        DispatchMessage(&msg);
}

return  msg.wParam;

}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX 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          = NULL;
    wcex.hCursor        = NULL;
wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = NULL;
wcex.lpszClassName  = _T("canvas");
wcex.hIconSm        = NULL;

return RegisterClassEx(&wcex);

}

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

HWND hWnd = CreateWindow(_T("canvas"), _T("绘图窗口"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd, 0, 0, 640, 480, true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);

//如下运行后是一个空白窗口

SelectObject(bufdc, bg);
BitBlt(mdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);
BitBlt(hdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);

//如果改为下面这样,运行后可以加载出bg.bmp位图
SelectObject(bufdc, bg);
BitBlt(hdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);

return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
ReleaseDC(hWnd, hdc);
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • shen_wei 2017-07-19 08:12
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果