使用BitBlt获取另一个窗口的单个像素

时间:2012-01-02 06:30:51

标签: c++ winapi gdi bitblt getpixel

这就是我目前正在做的事情:

  • 通过GetWindowDC
  • 获取窗口DC
  • 使用CreateCompatibleDC
  • 创建兼容的DC
  • 在我的兼容DC
  • 上致电GetPixel

不幸的是,我的所有GetPixel调用都返回CLR_INVALID。这是我的代码。

bool Gameboard::Refresh()
{
  bool  ret = false;
  HDC   context, localContext;

  context = GetWindowDC(m_window);
  if (context != NULL)
  {
    localContext = CreateCompatibleDC(context);
    if (localContext != NULL)
    {
      if (BitBlt(localContext, 0, 0, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight,
        context, GameboardInfo::TopLeft.x, GameboardInfo::TopLeft.y, SRCCOPY))
      {
        ret = true;
        // several calls to GetPixel which all return CLR_INVALID
      }
      DeleteDC(localContext);
    }
    ReleaseDC(m_window, context);
  }
  return ret;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您需要在设备上下文中选择一个位图。

“必须在设备上下文中选择位图,否则,将在所有像素上返回CLR_INVALID。” - GetPixel()

bool Gameboard::Refresh()
{
  bool  ret = false;
  HDC   context, localContext;


  HGDIOBJ origHandle;


  context = GetWindowDC(m_window);
  if (context != NULL)
  {
    localContext = CreateCompatibleDC(context);


    origHandle = SelectObject(localcontext,CreateCompatibleBitmap(context, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight));


    if (localContext != NULL)
    {
      if (BitBlt(localContext, 0, 0, GameboardInfo::BoardWidth, GameboardInfo::BoardHeight,
        context, GameboardInfo::TopLeft.x, GameboardInfo::TopLeft.y, SRCCOPY))
      {
        ret = true;
        // several calls to GetPixel which all return CLR_INVALID
      }

      SelectObject(localcontext, origHandle);


      DeleteDC(localContext);
    }
    ReleaseDC(m_window, context);
  }
  return ret;
}