René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

MemoryDC: a C++ class for memory device contects

This class is used in

The header file

MemoryDC.h
/* 
   MemoryDC.h

   Copyright (C) 2002-2005 René Nyffenegger

   This source code is provided 'as-is', without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original source code.

   3. This notice may not be removed or altered from any source distribution.

   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/


#ifndef MEM_DC_H_
#define MEM_DC_H_

class Bitmap;

#include "DC.h"

/*! The class MemoryDC encapsulates a memory device context.  */
class MemoryDC : public DC {
  public:
    /*! This constructor creates a memory device context that is compatible
        with the screen, using CreateCompatibleDC(0), which is described at
        http://msdn.microsoft.com/library/en-us/gdi/devcons_499f.asp?frame=true */
    MemoryDC();

    using DC::Select;
    Bitmap Select(Bitmap&);

   virtual ~MemoryDC();
};

#endif

The implementation file

MemoryDC.cpp
/* 
   MemoryDC.cpp

   Copyright (C) 2002-2005 René Nyffenegger

   This source code is provided 'as-is', without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original source code.

   3. This notice may not be removed or altered from any source distribution.

   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/


#include "MemoryDC.h"
#include "Bitmap.h"
#include "win32_LastError.h"

MemoryDC::MemoryDC() : DC(::CreateCompatibleDC(0)) {}

MemoryDC::~MemoryDC() {
  ::DeleteDC(dc_);
  dc_ = 0;
}

Bitmap MemoryDC::Select(Bitmap& bmp) {
  HBITMAP old_bmp = (HBITMAP) ::SelectObject(dc_, bmp);

//  if (!b) {
//    ::MessageBox(0, "MemoryDC::SelectObject failed", Win32_LastError().c_str(), 0);
//  }
//
//  ::MessageBox(0, "MemoryDC::SelectObject OK", 0, 0);

  Bitmap ret(old_bmp);
  return ret;
}