Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetDC

 

Windows function to get a handle to a device context from a window handle.

 

HDC GetDC(HWND handle_to_window);

 

If handle_to_window is null, the HDC of the desktop is obtained

If GetDC fails, null is returned

 

Do not forget to call ReleaseDC on the obtained HDC!

 

GetDC is declared in winuser.h (at least in the library that shipped with C++ Builder 6.0).

 

 

 

 

 

Example: MakeScreenshot

 

MakeScreenshot retrieves the HDC of the desktop and copies it (using BitBlt) on a bitmap (or more specific: a Graphics::TBitmap).

 

MakeScreenshot uses the VCL data type Graphics::TBitmap supplied with C++ Builder 6.0.

 

#include <cassert>
#include <windows.h>
#include <Extctrls.hpp>
//From http://www.richelbilderbeek.nl/CppMakeScreenshot.htm
void MakeScreenshot(Graphics::TBitmap * const b)
{
  assert(b);
  assert(b->PixelFormat == pf32bit);
  const HDC desktop = GetDC(0);
  b->Width  = Screen->Width;
  b->Height = Screen->Height;
  BitBlt(b->Canvas->Handle, 0, 0, b->Width, b->Height, desktop, 0, 0, SRCCOPY);
  b->Modified = true;
  ReleaseDC(0, desktop);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict