Question

This is what I am currently doing:

  • get window DC via GetWindowDC
  • create a compatible DC with CreateCompatibleDC
  • call GetPixel on my compatible DC

Unfortunately, all of my GetPixel calls are returning CLR_INVALID. Here is my code.

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;
}

Any ideas?

Was it helpful?

Solution

I believe you need to select a bitmap into your device context.

"A bitmap must be selected within the device context, otherwise, CLR_INVALID is returned on all pixels." - 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top