Re: DrawIconEx weirdness under Win2K

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



That's because all painting takes place in the Form_Resize event.
Andy.

"Robert" <noone@xxxxxxxxxxx> ha scritto nel messaggio
news:%23OuMT$PvFHA.908@xxxxxxxxxxxxxxxxxxxxxxx
> Somehow, I miss the Form_Paint event.
>
> Robert
>
>
> "Andy DF" <nospam@xxxxxxxxxx> wrote in message
> news:432e898b$0$8481$5fc30a8@xxxxxxxxxxxxxxxxxx
>> I use DrawIconEx to draw an icon onto a metafile DC.
>> Everything works ok under WinXP but under Win2K I get a black square.
>>
>> What am I doing wrong?
>>
>> Code below is a simplified version of what I'm using (needs to be pasted
> in
>> Form1 of a standard project and needs an icon in Form_Load).
>>
>> TIA,
>> Andy
>>
>> ' CODE ***************
>>
>> Option Explicit
>>
>> Private Const DI_MASK As Long = &H1& ' Draws the icon
> or
>> cursor using the mask.
>> Private Const DI_IMAGE As Long = &H2& ' Draws the icon
> or
>> cursor using the image.
>> Private Const DI_NORMAL As Long = DI_MASK Or DI_IMAGE ' Combination of
>> DI_IMAGE and DI_MASK.
>> Private Type ICONINFO
>> fIcon As Long
>> xHotspot As Long
>> yHotspot As Long
>> hbmMask As Long
>> hbmColor As Long
>> End Type
>> Private Declare Function GetIconInfo Lib "user32.dll" (ByVal hIcon As
> Long,
>> ByRef piconinfo As ICONINFO) As Long
>> Private Declare Function DrawIconEx Lib "user32.dll" (ByVal hdc As Long,
>> ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal
> cxWidth
>> As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal
>> hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
>> Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As
>> Long) As Long
>> Private Declare Function GetObjectType Lib "gdi32.dll" (ByVal hgdiobj As
>> Long) As Long
>> Private Const OBJ_ENHMETAFILE As Long = 13
>> Private Const OBJ_BITMAP As Long = 7
>> Private Const OBJ_METAFILE As Long = 9
>>
>> ' Map mode
>> Private Const MM_LOMETRIC As Long = 2
>> Private Declare Function SetMapMode Lib "gdi32.dll" (ByVal hdc As Long,
>> ByVal nMapMode As Long) As Long
>>
>> ' Metafile
>> Private Type RECT
>> Left As Long
>> Top As Long
>> Right As Long
>> Bottom As Long
>> End Type
>> Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1
>> As
>> Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
>> Private Declare Function CreateEnhMetaFile Lib "gdi32.dll" Alias
>> "CreateEnhMetaFileA" (ByVal hDCref As Long, ByVal lpFileName As String,
>> ByRef lpRect As RECT, ByVal lpDescription As String) As Long
>> Private Declare Function PlayEnhMetaFile Lib "gdi32.dll" (ByVal hdc As
> Long,
>> ByVal hEmf As Long, ByRef lpRect As RECT) As Long
>> Private Declare Function CloseEnhMetaFile Lib "gdi32.dll" (ByVal hdc As
>> Long) As Long
>> Private Declare Function DeleteEnhMetaFile Lib "gdi32.dll" (ByVal hEmf As
>> Long) As Long
>>
>> Private m_objIcon As StdPicture
>>
>> Private Sub Form_Load()
>> ' Load icon from resource...
>> Set m_objIcon = LoadResPicture(101, vbResIcon)
>> ' ... or from a file
>> ' Set m_objIcon = LoadPicture("C:\Path\icon.ico")
>>
>> ' Set up form
>> Me.ScaleMode = vbMillimeters
>> Me.AutoRedraw = True
>> End Sub
>>
>> Private Sub Form_Resize()
>> Dim ifT As ICONINFO
>> Dim lOldMapMode As Long
>> Dim lDC As Long
>> Dim hMeta As Long
>> Dim rc As RECT
>>
>> ' Make sure picture is an Icon
>> If CBool(GetIconInfo(m_objIcon.Handle, ifT)) Then
>>
>> ' Clean up
>> With ifT
>> DeleteObject .hbmMask
>> DeleteObject .hbmColor
>> End With
>>
>> ' Clean up previous drawing
>> Me.Cls
>>
>> ' Make in-memory screen compatible metafile DC
>> SetRect rc, 0, 0, ScaleWidth * 100, ScaleHeight * 100 ' Size in
>> Hi
>> Metrics (0.01 mm)
>> lDC = CreateEnhMetaFile(0&, vbNullString, rc, vbNullString)
>>
>> ' Set map mode of metafile DC to LOMETRIC (0.1 mm)
>> lOldMapMode = SetMapMode(lDC, MM_LOMETRIC)
>>
>> ' DrawIocnEx
>> With m_objIcon
>> Call DrawIconEx(lDC, 0, 0, .Handle, _
>> ScaleWidth * 10, _
>> -ScaleHeight * 10, _
>> 0&, 0&, DI_NORMAL)
>> End With
>>
>> ' Restore map mode of metafile DC
>> Call SetMapMode(lDC, lOldMapMode)
>>
>> ' Close metafile DC and get metafile
>> hMeta = CloseEnhMetaFile(lDC)
>>
>> ' Set drawing coordinates
>> SetRect rc, 0, 0, ScaleWidth * 10, -ScaleHeight * 10
>>
>> ' Set map mode of form to LOMETRIC
>> lOldMapMode = SetMapMode(Me.hdc, MM_LOMETRIC)
>>
>> ' Draw onto form
>> PlayEnhMetaFile Me.hdc, hMeta, rc
>>
>> ' Clean up
>> DeleteEnhMetaFile hMeta
>> Call SetMapMode(Me.hdc, lOldMapMode)
>>
>> End If
>>
>> End Sub
>>
>> Private Sub Form_Unload(Cancel As Integer)
>> ' Clean up
>> Set m_objIcon = Nothing
>> End Sub
>>
>>
>
>


.


Quantcast