Mixing GDI+ & GDI drawing

L

Lloyd Dupont

In the same OnPaint() method I could mix GDI+ and GDI drawing.
I have some hard to track nasty bug that appears sometimes (mainly when I
print) and I wonder if any of the things I do are not exactly proper.....

If you've some comments about the code below, speak up!

The GDI part is done in managed C++ and I initialize and end each GDI block
with the following code:

=== the instance variables =====

Graphics^ graphics;
int SavedDC;
HGDIOBJ hregion;
::HDC hdc;

=== initialization of GDI =====

static void internal_SetupMapMode(HDC hdc)
{
HDC screenDc = GetDC(NULL);
int screenDpiX = GetDeviceCaps(screenDc, LOGPIXELSX);
int screenDpiY = GetDeviceCaps(screenDc, LOGPIXELSY);
::ReleaseDC(NULL, screenDc);

int devDpiX = GetDeviceCaps(hdc, LOGPIXELSX);
int devDpiY = GetDeviceCaps(hdc, LOGPIXELSY);

// debug magic :-/, why is that? (I mean why is it not just: devDpi)
devDpiX = (devDpiX / screenDpiX) * screenDpiX;
devDpiY = (devDpiY / screenDpiY) * screenDpiY;

::SetMapMode(hdc, MM_ANISOTROPIC);
::SetWindowExtEx(hdc, screenDpiX, screenDpiY, NULL);
::SetViewportExtEx(hdc, devDpiX, devDpiY, NULL);
//::SetWindowExtEx(hdc, 100, 100, NULL);
//::SetViewportExtEx(hdc, GetDeviceCaps(hdc, LOGPIXELSX),
// GetDeviceCaps(hdc, LOGPIXELSY), NULL);
}

NScribe::GdiGraphics::GdiGraphics(Graphics^ g)
{
if( !g )
throw gcnew ArgumentException();

array<float>^ matrix = g->Transform->Elements;
pin_ptr<float> p_transform = &matrix[0];

hregion = (HGDIOBJ)(void*)g->Clip->GetHrgn(g);
hdc = :):HDC)(void*)g->GetHdc();
SavedDC = ::SaveDC(hdc);
graphics = g;

::SetGraphicsMode(hdc, GM_ADVANCED);
::SetBkMode(hdc, TRANSPARENT);
if(hregion)
::SelectObject(hdc, hregion);

::internal_SetupMapMode(hdc);
::SetWorldTransform(hdc, (XFORM*) p_transform);
}

=== leaving GDI mode by disposing of the GdiGraphics object===

NScribe::GdiGraphics::~GdiGraphics()
{
if( hdc )
{
::RestoreDC(hdc, SavedDC);
graphics->ReleaseHdc((IntPtr)(void*) hdc);
if(hregion)
graphics->Clip->ReleaseHrgn((IntPtr)(void*) hregion);
}
hdc = NULL;
}

======================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top