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

PaintDC: a C++ class

When an Application receives a WM_PAINT, it usually aquires a DeviceContext for the invalidated Window with BeginPaint and releases it with EndPaint. This makes it ideal to derive a class from DC that calls BeginPaint in its constructor and EndPaint in its destructor. This class is called PaintDC.
This class is used in Fractal Generator.

The header file

PaintDC.h
/* 
   PaintDC.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 PAINT_DC__
#define PAINT_DC__

#include "DC.h"

class PaintDC : public DC {
public:
  PaintDC(HWND);
 ~PaintDC();

private:
  PAINTSTRUCT ps_;
  HWND        hwnd_;
};

#endif

The implementation file

PaintDC.cpp
/* 
   PaintDC.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 "PaintDC.h"

PaintDC::PaintDC(HWND hwnd) : DC(BeginPaint(hwnd, &ps_)), hwnd_(hwnd) {}

PaintDC::~PaintDC() {
  ::EndPaint(hwnd_, &ps_);
}