为什么BitBlt()在这里失败? [英] Why does BitBlt() fail here?

查看:197
本文介绍了为什么BitBlt()在这里失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次定时器关闭时,我都会尝试在不同的位置加载图像。这是我用来加载每个图像及其代码的函数:

I am trying to load an image at a different spot every time a timer goes off. Here is the function I am using to load each image and its code:

BOOL LoadBitmapImg(LPCSTR fileName, HDC hWinDC, int x, int y) {

  HBITMAP hBMP;

  hBMP = (HBITMAP)::LoadImage(NULL, fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

  if (hBMP == NULL) {
   MessageBox(NULL, _T("Loading Sequence Failed"), _T("Error"), MB_OK);
   return false;
 }

  HDC hlocaldc;

  hlocaldc = CreateCompatibleDC(hWinDC);

  if (hlocaldc == NULL) {
   MessageBox(NULL, _T("Could not create compatible DC"), _T("Error"), MB_OK);
   return false;
}

  BITMAP myBitmap;

  int tReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBMP), sizeof(BITMAP), reinterpret_cast<LPVOID>(&myBitmap));

  if (!tReturn) {
   MessageBox(NULL, _T("Failed to get object"), _T("Error"), MB_OK);
   return false;
}

  HBITMAP selBMP = (HBITMAP)::SelectObject(hlocaldc, hBMP);

  if (selBMP == NULL) {
   MessageBox(NULL, _T("Failed to select object"), _T("Error"), MB_OK);
   return false;
}

  BOOL winBlit = BitBlt(hWinDC, x, y, myBitmap.bmWidth, myBitmap.bmHeight, hlocaldc, 0, 0, SRCCOPY);

  if (!winBlit) {
   MessageBox(NULL, _T("Failed to blit bitmap to window"), _T("Error"), MB_OK);
   return false;
}

  SelectObject(hlocaldc, selBMP);
  DeleteDC(hlocaldc);
  DeleteObject(hBMP);
} 

以下是设置计时器并处理计时器事件的代码:

And here is the code that sets the timer and handles the timer event:

PAINTSTRUCT ps;  
    HDC hdc;   
    HDC drawHandle;
    HRGN rect;

   SetTimer(hWnd, 1020, 500, (TIMERPROC) NULL);

    switch (message) { 

    case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      
      EndPaint(hWnd, &ps);
      break;

    case WM_TIMER:
     
     rect = CreateRectRgn(0, 0, 1000, 1000);
     drawHandle = GetDCEx(hWnd, rect, DCX_INTERSECTRGN);

   switch (wParam) {
 
    case 1020:

      LoadBitmapImg("C:/Basic Game Pics/Cloud.bmp", drawHandle, increase, 0);
      increase += 1;

      break;
   }
  
       ReleaseDC(hWnd, drawHandle);
       break;

当我运行我的程序时,我没有出现控制台错误,但窗口弹出我的消息框说"无法将位图blit到窗口"每半秒,就像我的计时器所暗示的那样。出了什么问题? drawHandle HDC不是我要绘制的窗口
的一部分吗?因为那是BitBlt()的第一个参数。为什么会这样?

When I run my program, I get no console errors, but the window pops up my Message Box that says "Failed to blit bitmap to window" every half second, just as my timer suggests. So what's going wrong? Is the drawHandle HDC not the part of the window I want to draw to? Because that's what BitBlt()'s first parameter is. Why does this happen?

推荐答案

也许
BitBlt 不支持此类操作。尝试解决方法:

Maybe BitBlt does not support such operations. Try a workaround:

  
drawHandle = GetDC(
hWnd );

   drawHandle = GetDC( hWnd );

  
IntersectClipRect(drawHandle,0,0,1000,1000);

   IntersectClipRect( drawHandle, 0, 0, 1000, 1000 );

 

&NBSP;&NBSP;
drawHandle = GetDC(
hWnd );

   drawHandle = GetDC( hWnd );

  
SelectClipRgn(drawHandle,rect);

   SelectClipRgn( drawHandle, rect );


这篇关于为什么BitBlt()在这里失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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