/*
* $Header: /home/dscott/nasty/memory/autogdi.h,v 1.1 2001/11/06 21:49:18 dscott Exp $
* $NoKeywords: $
*/
#ifndef AUTOGDI_H_
#define AUTOGDI_H_ 1
#include
#include
#include
struct gdi_traits
{
// Specializing the handle_traits template
// for HGDIOBJ won't work. Both HANDLE and
// HGDIOBJ are defined as pointers to void
static HGDIOBJ null_value()
// GDI functions return NULL when they fail
{ return 0; }
static void dispose(HGDIOBJ hObj)
{ ::DeleteObject(hObj); }
private:
static objCount_;
};
typedef auto_handle > gdi_handle;
/*
struct image_list_traits
{
static HIMAGELIST null_value()
{ return 0; }
static void dispose(HIMAGELIST hImageList)
{ ImageList_Destroy(hImageList); }
};
typedef auto_handle image_list;
*/
// const DWORD maxObjType = OBJ_ENHMETAFILE;
// When a GDI object is selected in a DC one must make
// sure the original object is selected back BEFORE the
// object is destroyed.
/**
* A wrapper class around HDC
*/
class DeviceContext
{
public:
/**
* Default ctor, makes a memory device context
* compatible with the screen's context
*/
DeviceContext();
explicit DeviceContext(HWND/*, HRGN = 0, DWORD flags = 0 */);
DeviceContext(HWND, HDC);
DeviceContext(HWND, PAINTSTRUCT&);
DeviceContext(const DeviceContext&);
~DeviceContext();
HGDIOBJ SelectObject(gdi_handle&);
WPARAM GetWParam() const
{ return reinterpret_cast(hDC_); }
bool IsValid() const
{ return hDC_ != 0; }
bool CreateCompatibleBitmap(const DeviceContext& srcDC, int w, int h);
bool CopyBitmapFrom(const DeviceContext& srcDC,
const RECT& srcRect,
const RECT& destRect,
DWORD rop = SRCCOPY);
void CreateDIBSection(
const BITMAPINFO* pbi,
UINT usage,
void** ppvBits,
HANDLE hSection,
DWORD offset,
gdi_handle& retBmp) const;
int SetDIBits( const gdi_handle&,
UINT uStartScan, // starting scan line
UINT cScanLines, // number of scan lines
const void* lpvBits, // array of bitmap bits
const BITMAPINFO *lpbmi, // address of struct with
// bitmap data
UINT fuColorUse);
bool PatBlt(
int nXLeft, // x-coord. of upper-left corner of rect. to be filled
int nYLeft, // y-coord. of upper-left corner of rect. to be filled
int nWidth, // width of rectangle to be filled
int nHeight, // height of rectangle to be filled
DWORD dwRop // raster operation code
);
int FillRect(const RECT&, HBRUSH);
bool SetBkColor(COLORREF color)
{
ASSERT(IsValid());
return ::SetBkColor(hDC_, color) != CLR_INVALID;
}
bool SetTextColor(COLORREF color)
{
ASSERT(IsValid());
return ::SetTextColor(hDC_, color) != CLR_INVALID;
}
COLORREF GetPixel(int x, int y) const
{ return ::GetPixel(hDC_, x, y); }
private:
DeviceContext& operator=(DeviceContext&);
// context type
enum { window, memory, paint } type_;
HWND hWnd_;
HDC hDC_;
PAINTSTRUCT* pPaint_;
std::map orgSelected_;
std::map > curSelected_;
}; // DeviceContext
inline void SelectObject(DeviceContext& dc, HGDIOBJ hObj)
{
gdi_handle h(hObj);
dc.SelectObject(h);
}
#endif// AUTOGDI_H_