i did 2 nice functions for creating an HDC and other HBITMAP:
Code:
class MemoryDC
{
private:
HDC memoryDC;
public:
MemoryDC ()
{
HDC hdc=GetDC(GetDesktopWindow());
memoryDC=CreateCompatibleDC(hdc);
ReleaseDC(GetDesktopWindow(),hdc);
}
operator HDC() const
{
return memoryDC;
}
~MemoryDC ()
{
DeleteDC(memoryDC);
}
};
class BitmapDC
{
private:
MemoryDC hdcbitmap;
HGDIOBJ bitmapold;
HBITMAP bitmapcurrent;
int intwidth;
int intheight;
void init(int width, int height)
{
bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
intwidth=width;
intheight=height;
}
void destroy()
{
SelectObject(hdcbitmap, bitmapold);
DeleteObject(bitmapcurrent);
}
public:
BitmapDC(int width, int height)
{
init(width, height);
}
BitmapDC()
{
init(1, 1);
}
void size(int width, int height)
{
destroy();
init(width, height);
}
int Width()
{
return intwidth;
}
int Height()
{
return intheight;
}
BitmapDC& operator= (const BitmapDC &bitmapsource)
{
if (this == &bitmapsource) // Same object?
return *this;
destroy();
init(bitmapsource.intwidth, bitmapsource.intheight);
BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
return *this;
}
BitmapDC& operator= (const HBITMAP &bitmapsource)
{
destroy();
BITMAP bm;
GetObject(bitmapsource,sizeof(bm),&bm);
init(bm.bmWidth, bm.bmHeight);
DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
return *this;
}
//testing the bitmao value if is nullptr
bool operator != ( nullptr_t ) const
{
return bitmapcurrent != nullptr;
}
bool operator==(const BitmapDC &other) const
{
return (other.bitmapcurrent == this->bitmapcurrent);
}
bool operator!=(const BitmapDC &other) const
{
return !(*this == other);
}
operator HBITMAP() const
{
return bitmapcurrent;
}
operator HDC() const
{
return hdcbitmap;
}
~BitmapDC()
{
destroy();
}
};
the MemoryDC seems ok, because the HDC is returned.
the BitmapDC seems wrong... why don't return correctly the HBITMAP?