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

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

有什么想法吗?

最佳答案

我相信您需要在您的设备环境中选择一个位图。

“必须在设备上下文中选择位图,否则,将在所有像素上返回 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;
}

关于c++ - 使用 BitBlt 获取另一个窗口的单个像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8698049/

相关文章:

c++ - 获取事件 udp 连接的目标 IP/端口?

c - 将消息值转换为宏?

winapi - 给定一个 HBITMAP,有没有办法找出它是否包含 alpha channel ?

C# GDI 如何绘制适合矩形的文本?

c++ - 我如何运行 quickfix 示例?

c++ - ffmpeg C++ 和 AVFormatContext

Delphi 窗体恢复状态位置和大小

c++ - MFC中背景绘制到位图的线程类型

c++ - 以下程序中哪个是耗时的构造?

c++ - 在哪里可以找到 STL 集合类中的 _M_key_compare 函数定义?