-5

I'm new to C++ and have to take a screenshot. I think I have set up everything besides the print method itself and that's where I'm struggling.

I found a post on so which describes how to take a screenshot, but it somehow doesn't work for me. (How to capture part of the screen and save it to a BMP?)

My Method looks like this:

STDOVERRIDEMETHODIMP VImplPrintable::Print(HDC hdc, CRect* pCr)
{  
  HDC hdcSource = GetDC(NULL);
  HDC hdcMemory = CreateCompatibleDC(hdcSource);   

  int capX = GetDeviceCaps(hdcSource, HORZRES);
  int capY = GetDeviceCaps(hdcSource, VERTRES);

  HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, pCr->Width(), pCr->Height());
  HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);

  BitBlt(hdcMemory, 0, 0, pCr->Width(), pCr->Height(), hdcSource, pCr->top, pCr->left, SRCCOPY);

  DeleteDC(hdcSource);
  DeleteDC(hdcMemory);

  return S_OK;
}

Problem is the screenshot seems to be an empty bitmap. I don't really know if I it makes sense to create a new HDC when I'm already getting one as a parameter. Any help is appreciated.

user3292642
  • 711
  • 2
  • 8
  • 32
  • Define "print it" - this could mean any of many things – Lightness Races in Orbit May 31 '17 at 13:34
  • @BoundaryImposition The printing part is somewhat magic to me. I think this already happens somewhere else. Problem is in the Print Preview the place where my Wpf View should be, there is just a frame with an empty space inside. So I guess the title is a bit missleading, this method is not really about printing if I understand correctly, even if it's name is Print. – user3292642 May 31 '17 at 13:40
  • Um... even if its magic, you would have to somehow interact with it. **Define** how you would wish to use the screenshot – Passer By May 31 '17 at 13:41
  • @user3292642: Well if the question has nothing to do with printing then you should remove mentions of that from the question. – Lightness Races in Orbit May 31 '17 at 13:42
  • Is the `HDC` your method receive, the printer HDC? – Serge Ballesta May 31 '17 at 13:45
  • @SergeBallesta I think so. Documentation says: Context for graphical output – user3292642 May 31 '17 at 13:47
  • @BoundaryImposition I know my question is not very clear, but that's because all this is not very clear to me. I'm trying my best to explain and appreciate any comments, but yours are just not helpfull at all. If you feel like you can't help because theres too little information then why don't you go and answer some other questions. – user3292642 May 31 '17 at 13:50
  • @user3292642: You are mistaken; I _am_ trying to help you. If you remove the irrelevant references to "printing" then your question will be clearer, and you will get more help faster. I don't spend my time trying to help you improve your question just for fun. – Lightness Races in Orbit May 31 '17 at 13:52
  • "Documentation says: Context for graphical output" Documentation of what? You should describe what's calling this function you provided, since it seems this mysterious API is what you think will do printing. Also [you forgot to undo the `SelectObject()` after the `BitBlt()`](https://blogs.msdn.microsoft.com/oldnewthing/20130306-00/?p=5043). And I can guess that you are right, you don't need `hdcMemory` as you have the destination DC passed in as a parameter, but again, we would need to know more about the surrounding code. – andlabs May 31 '17 at 13:52
  • 1
    @user3292642: your last comment is just uselessly offensive. BoundaryImposition is right in one part, your question is unclear, that's the reason why I asked you details on what was the HDC parameter of this method. – Serge Ballesta May 31 '17 at 13:53

1 Answers1

1

I cannot test because I do not know what is the framework that calls that method, but as you receive the HDC you want to write to, you simply should not use a memory DC and directly BitBlt there. But you should also test the return value of WinAPI calls to return error conditions to the caller:

STDOVERRIDEMETHODIMP VImplPrintable::Print(HDC hdc, CRect* pCr)
{  
  HDC hdcSource = GetDC(NULL);
  if (NULL == hdcSource) return E_FAIL;

  HRESUL cr = S_OK;

  if (!BitBlt(hdc, 0, 0, pCr->Width(), pCr->Height(), hdcSource, pCr->top, pCr->left, 
      SRCCOPY)) cr = E_FAIL;

  DeleteDC(hdcSource);

  return cr;
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252