Domanda

I am working on a c++ browser app which will render html on the app screen. I wanna to print the entire contents of the app screen (as internet explorers do). What is the proper, and easiest, way where I only pass handle to app window and it do print/pdf.

I googled this question but found only manual drawing. Is there some lib/technique where i pass handler of current window & it take care of printing text & images.

È stato utile?

Soluzione

Windows provides the function PrintWindow for getting an image of a window. Here is an example, taken from my sources, it places a resulting screenshot into clipboard (it takes into account that the function may not be supported on some Windows versions, but it seems unimportant to the question):

void PrintCapturedWindow(HWND hwnd)
{
  HDC hdc = GetDC(hwnd);
  if (hdc)
  {
    HDC hdcMem = CreateCompatibleDC(hdc);
    if (hdcMem)
    {
      RECT rc;
      GetClientRect(hwnd, &rc);

      HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
      ReleaseDC(hwnd, hdc); hdc = 0;
      if (hbitmap)
      {
          typedef BOOL WINAPI (* pPrintWindow)(HWND hwnd, HDC hdcBlt, UINT nFlags);
          pPrintWindow ppw;

          HMODULE user_hand = GetModuleHandle("user32.dll");
          ppw = (pPrintWindow)GetProcAddress(user_hand, "PrintWindow");
          if(ppw)
          {
            SelectObject(hdcMem, hbitmap);
            (*ppw)(hwnd, hdcMem, 0);
          }

          ::OpenClipboard(NULL);
          ::EmptyClipboard();
          ::SetClipboardData(CF_BITMAP, hbitmap);
          ::CloseClipboard();

          DeleteObject(hbitmap);
      }
      DeleteObject(hdcMem);
    }
    if(hdc != 0) ReleaseDC(hwnd, hdc);
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top