Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

# memory_dc.cpp

    00001 // memory_dc.cpp
    00002
    00003 // Copyright (C) 2004 Steven Weiß (steven11@gmx.de)
    00004 //
    00005 // Permission to copy, use, sell and distribute this software is granted
    00006 // provided this copyright notice appears in all copies.
    00007 // Permission to modify the code and to distribute modified code is granted
    00008 // provided this copyright notice appears in all copies, and a notice
    00009 // that the code was modified is included with the copyright notice.
    00010 //
    00011 // This software is provided "as is" without express or implied warranty,
    00012 // and with no claim as to its suitability for any purpose.
    00013
    00014
    00015 #include <background_wnd/detail/memory_dc.hpp>
    00016 #include <crtdbg.h>
    00017
    00018
    00019 namespace win32 { namespace gui {
    00020 namespace bg_wnd {
    00021
    00026 memory_dc::memory_dc(void) : hMemDC_(NULL), hOldBitmap_(NULL)
    00027 {}
    00028
    00029 memory_dc::~memory_dc(void)
    00030 {
    00031      delete_dc();
    00032 }
    00033
    00034 void memory_dc::create_dc(HDC hDC, int cx, int cy)
    00035 {
    00036      delete_dc();
    00037      _ASSERTE(hMemDC_ == NULL);                   // DC must be NULL
    00038
    00039     HBITMAP hBitmap = CreateCompatibleBitmap(hDC, cx, cy);
    00040     _ASSERTE(hBitmap);                            // Bitmap must be valid
    00041
    00042     hMemDC_ = CreateCompatibleDC(hDC);
    00043     _ASSERTE(hMemDC_);                            // DC must be valid
    00044     hOldBitmap_ = (HBITMAP) SelectObject(hMemDC_, (HGDIOBJ) hBitmap);
    00045
    00046      // Fill background
    00047      COLORREF cr = GetBkColor(hDC);
    00048      SetBkColor(hMemDC_, cr);
    00049     HBRUSH hbr = CreateSolidBrush(cr);
    00050      RECT rc = { 0, 0, cx, cy };
    00051     FillRect(hMemDC_, &rc, hbr);
    00052     DeleteObject((HGDIOBJ) hbr);
    00053 }
    00054
    00055 void memory_dc::delete_dc()
    00056 {
    00057     if (hMemDC_ && hOldBitmap_) {
    00058         HGDIOBJ hBmp = SelectObject(hMemDC_, (HGDIOBJ) hOldBitmap_);
    00059         DeleteObject(hBmp);
    00060         DeleteDC(hMemDC_);
    00061         hMemDC_ = NULL;
    00062         hOldBitmap_ = NULL;
    00063     }
    00064 }
    00065
    00066 memory_dc::operator HDC()
    00067 {
    00068      return hMemDC_;
    00069 }
    00070
    00071 void memory_dc::size(int cx, int cy)
    00072 {
    00073      if (hMemDC_) {
    00074           if (cx == 0 || cy == 0) {
    00075                _ASSERTE(false);                   // invalid size
    00076                return;
    00077           }
    00078
    00079           memory_dc tempDC;
    00080           tempDC.create_dc(*this, cx, cy);
    00081           bitblt(tempDC, 0, 0, cx, cy, 0, 0, SRCCOPY);
    00082           swap(tempDC);
    00083     }
    00084 }
    00085
    00086 SIZE memory_dc::size() const
    00087 {
    00088      SIZE size = { 0 };
    00089      if (hMemDC_) {
    00090         BITMAP bm;
    00091         HGDIOBJ hBmp = GetCurrentObject(hMemDC_, OBJ_BITMAP);
    00092           if (GetObject(hBmp, sizeof(BITMAP), (void*) &bm) != NULL) {
    00093                size.cx = bm.bmWidth;
    00094                size.cy = bm.bmHeight;
    00095           }
    00096      }
    00097      return size;
    00098 }
    00099
    00100 bool memory_dc::bitblt(HDC hDest, int x, int y, int cx, int cy, int xSrc, int ySrc, DWORD dwRop)
    00101 {
    00102      _ASSERTE(hMemDC_);                      // DC must be valid for this operation
    00103      return BitBlt(hDest, x, y, cx, cy, hMemDC_, xSrc, ySrc, dwRop) != FALSE;
    00104 }
    00105
    00106 void memory_dc::swap(memory_dc& dc)
    00107 {
    00108      HDC hDC         = dc.hMemDC_;
    00109      HBITMAP hBmp = dc.hOldBitmap_;
    00110      dc.hMemDC_        = hMemDC_;
    00111      dc.hOldBitmap_ = hOldBitmap_;
    00112      hMemDC_        = hDC;
    00113      hOldBitmap_ = hBmp;
    00114 }
    00115
    00116 }    // namespace bg_wnd
    00117 } }  // namespace win32::gui