http://msdn.microsoft.com/en-us/library/aa923962.aspx :
BOOL DeleteObject(
HGDIOBJ hObject
);
Parameters
hObject
[in] Handle to a logical pen, brush, font, bitmap, region, or palette.
Return Value
Nonzero indicates success.
Zero indicates that the specified handle is not valid or that the handle is currently selected into a device context.
To get extended error information, call GetLastError.
Remarks
Do not delete a drawing object (pen or brush) while it is still selected into a device context.
Lamentablemente esto también se aplica a otros objetos GDI, como por ejemplo, mapas de bits.
http://msdn.microsoft.com/en-us/library/aa932923.aspx :
HGDIOBJ SelectObject(
HDC hdc,
HGDIOBJ hgdiobj
);
Parameters
hdc
[in] Handle to the device context.
hgdiobj
[in] Handle to the object to be selected.
The specified object must have been created by using one of the following functions.
...................
Return Value
If the selected object is not a region, the handle of the object being replaced indicates success.
..................
Remarks
This function returns the previously selected object of the specified type.
An application should always replace a new object with the original, default object after it has finished drawing with the new object.
..................
Con el fin de que sea completamente compatible con las especificaciones de Windows, el código de RenderStretchFromDC () debería tener el siguiente aspecto:
Private Function RenderStretchFromDC(ByVal DestDC As Long, _
ByVal DestX As Long, _
ByVal DestY As Long, _
ByVal DestW As Long, _
ByVal DestH As Long, _
ByVal SrcDC As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal Width As Long, _
ByVal Height As Long, _
ByVal Size As Long, _
Optional MaskColor As Long = -1)
Dim Sx2 As Long
Sx2 = Size * 2
If MaskColor <> -1 Then
Dim mDC As Long
Dim mX As Long
Dim mY As Long
Dim DC As Long
Dim hBmp As Long
Dim hOldBmp As Long ' storage for the original BMP allocated automatically when CreateCompatibleDC() is called
mDC = DestDC
DC = GetDC(0)
DestDC = CreateCompatibleDC(0)
hBmp = CreateCompatibleBitmap(DC, DestW, DestH)
hOldBmp = SelectObject(DestDC, hBmp) ' save the original BMP for later reselection
mX = DestX: mY = DestY
DestX = 0: DestY = 0
End If
SetStretchBltMode DestDC, vbPaletteModeNone
BitBlt DestDC, DestX, DestY, Size, Size, SrcDC, x, y, vbSrcCopy 'TOP_LEFT
StretchBlt DestDC, DestX + Size, DestY, DestW - Sx2, Size, SrcDC, x + Size, y, Width - Sx2, Size, vbSrcCopy 'TOP_CENTER
BitBlt DestDC, DestX + DestW - Size, DestY, Size, Size, SrcDC, x + Width - Size, y, vbSrcCopy 'TOP_RIGHT
StretchBlt DestDC, DestX, DestY + Size, Size, DestH - Sx2, SrcDC, x, y + Size, Size, Height - Sx2, vbSrcCopy 'MID_LEFT
StretchBlt DestDC, DestX + Size, DestY + Size, DestW - Sx2, DestH - Sx2, SrcDC, x + Size, y + Size, Width - Sx2, Height - Sx2, vbSrcCopy 'MID_CENTER
StretchBlt DestDC, DestX + DestW - Size, DestY + Size, Size, DestH - Sx2, SrcDC, x + Width - Size, y + Size, Size, Height - Sx2, vbSrcCopy 'MID_RIGHT
BitBlt DestDC, DestX, DestY + DestH - Size, Size, Size, SrcDC, x, y + Height - Size, vbSrcCopy 'BOTTOM_LEFT
StretchBlt DestDC, DestX + Size, DestY + DestH - Size, DestW - Sx2, Size, SrcDC, x + Size, y + Height - Size, Width - Sx2, Size, vbSrcCopy 'BOTTOM_CENTER
BitBlt DestDC, DestX + DestW - Size, DestY + DestH - Size, Size, Size, SrcDC, x + Width - Size, y + Height - Size, vbSrcCopy 'BOTTOM_RIGHT
If MaskColor <> -1 Then
TransparentBlt mDC, mX, mY, DestW, DestH, DestDC, 0, 0, DestW, DestH, MaskColor
SelectObject DestDC, hOldBmp ' reselect the original BMP and deselect the new BMP
DeleteObject hBmp ' now you are free to delete the new BMP
DeleteDC DC
DeleteDC DestDC ' delete DestDC with its original BMP
End If
End FunctionHay muchas piezas de código similar en las aplicaciones de ejemplo presentado en este sitio web. Mientras que las muestras pequeñas pueden parecer relativamente estable, porque la pintura relativamente poco lo que se hace, los grandes programas pueden sufrir caídas inesperadas y congelar al azar.
El manejo cuidadoso y limpio de objetos GDI a menudo pueden hacer que el código se comporte de una manera más previsible y previsto, debido al hecho de que la asignación automática / cancelación de asignación de memoria del sistema será compatible con los requisitos generales de Windows.
Espero sinceramente que mis observaciones hará que el contenido de su excelente sitio web todavía un poco mejor, si se puede hacer en absoluto.
Mike
