Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Use of SaveDC & RestoreDC functions

Sonx_hvn7Sonx_hvn7 Member Posts: 54
Came across the function RestoreDC()... tried using it in the folowing code

[code]

hBmp = RadarBgrnd.GetHandle();
hMemDC = CreateCompatibleDC(0);
hOldBmp = SelectObject(hMemDC, hBmp);

[color=Green]//Save the DC to be used later[/color]
savedDC = SaveDC(hMemDC);

SelectObject (hMemDC, hOldBmp);
DeleteDC(hMemDC);

[color=Green]// Restore the saved DC and draw line on it[/color]
RestoreDC(hMemDC, savedDC);

MoveToEx(hMemDC, coord.x,coord.y, &myPoint);
LineTo(hMemDC, coord.x+factor.x,coord.y+factor.y);

SelectObject (hMemDC, hOldBmp);
DeleteDC(hMemDC);
RadarBgrnd.Repaint ();

[/code]

Really this is just to test if Restore DC works... Results are as if hMemDC to draw to does not exist because no line is drawn... Is RestoreDC correctly used in the above code?

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Came across the function RestoreDC()... tried using it in the
    : folowing code
    :
    : [code]:
    :
    : hBmp = RadarBgrnd.GetHandle();
    : hMemDC = CreateCompatibleDC(0);
    : hOldBmp = SelectObject(hMemDC, hBmp);
    :
    : [color=Green]//Save the DC to be used later[/color]
    : savedDC = SaveDC(hMemDC);
    :
    : SelectObject (hMemDC, hOldBmp);
    : DeleteDC(hMemDC);
    :
    : [color=Green]// Restore the saved DC and draw line on it[/color]
    : RestoreDC(hMemDC, savedDC);
    :
    : MoveToEx(hMemDC, coord.x,coord.y, &myPoint);
    : LineTo(hMemDC, coord.x+factor.x,coord.y+factor.y);
    :
    : SelectObject (hMemDC, hOldBmp);
    : DeleteDC(hMemDC);
    : RadarBgrnd.Repaint ();
    :
    : [/code]:
    :
    : Really this is just to test if Restore DC works... Results are as if
    : hMemDC to draw to does not exist because no line is drawn... Is
    : RestoreDC correctly used in the above code?
    :
    [color=Blue]First you DeleteDC() and then begin calling API on a deleted handle. SaveDC() saves the objects which are currently selected into DC - it saves the DC state. RestoreDC() - simply restores them back. This is done just before releasing the DC back to Windows, because MSDN states that when you call DeleteDC() or ReleaseDC() - the DC should be in its original state - nothing of application handles should be left selected = otherwise GDI resources are leaking and application crashes from limit of GDI resources.[/color]
Sign In or Register to comment.