David Youngblood
6/3/2012 12:04:00 AM
How to create a Windows Metafile? The below sample errors on the LoadPicture
line (Error 481 - Invalid picture). The resulting file can be opened in
Windows Picture Viewer and MS Paint, but not by VB's LoadPicture.
David
Private Declare Function CreateMetaFile Lib "gdi32" _
Alias "CreateMetaFileA" ( _
ByVal lpString As String) As Long
Private Declare Function SetMapMode Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal nMapMode As Long) As Long
Private Declare Function SetWindowExtEx Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal nX As Long, _
ByVal nY As Long, _
lpSize As Long) As Long
Private Declare Function SetWindowOrgEx Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal nX As Long, _
ByVal nY As Long, _
lpPoint As Long) As Long
Private Declare Function CloseMetaFile Lib "gdi32" ( _
ByVal hMF As Long) As Long
Private Declare Function DeleteMetaFile Lib "gdi32" ( _
ByVal hMF As Long) As Long
Private Declare Function Rectangle Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function SetViewportExtEx Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal nX As Long, _
ByVal nY As Long, _
lpSize As Long) As Long
Private Declare Function SetViewportOrgEx Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal nX As Long, _
ByVal nY As Long, _
lpPoint As Long) As Long
Private Declare Function PlayMetaFile Lib "gdi32" ( _
ByVal hdc As Long, _
ByVal hMF As Long) As Long
Private Const MM_ANISOTROPIC = 8
Private Sub Command1_Click()
Dim hdcMF As Long
Dim hMF As Long
hdcMF = CreateMetaFile("I:\Temp\Graph.wmf")
SetMapMode hdcMF, MM_ANISOTROPIC
Debug.Print SetWindowOrgEx(hdcMF, 0, 0, ByVal 0)
Debug.Print SetWindowExtEx(hdcMF, 100, 100, ByVal 0)
Debug.Print SetViewportExtEx(hdcMF, Me.ScaleWidth, Me.ScaleHeight, ByVal
0)
Rectangle hdcMF, 10, 10, 90, 90
hMF = CloseMetaFile(hdcMF)
PlayMetaFile Me.hdc, hMF
DeleteMetaFile hMF
Set Form1.Picture1 = LoadPicture("I:\Temp\Graph.wmf")
End Sub