By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Mixing GDI+ & GDI drawing

P: n/a
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);
::SelectObject(hdc, hregion);

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

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

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

Feb 16 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.