GDIPLUS.DLL

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

GDIPLUS.DLL

Message par Flype »

Après m'être un peu amusé avec,
voici un example d'utilisation de la bibliothèque graphique 'gdiplus.dll' de microsoft.

Pour plus d'informations :
http://msdn.microsoft.com/library/defau ... erence.asp

Code : Tout sélectionner

; test gdiplus.lib for pb4, flype, jul 2006
; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusreference.asp

Import "gdiplus.lib"
  
  ; init
  GdiplusStartup(*token, *input, *output) 
  GdiplusShutdown(*token)
  GdipAlloc(Size.l)
  GdipFree(*ptr)
  
  ; codec
  GdipGetImageEncoders(numDecoders.l, Size.l, *decoders)
  GdipGetImageEncodersSize(*numDecoders, *Size)
  GdipGetImageDecoders(numDecoders.l, Size.l, *decoders)
  GdipGetImageDecodersSize(*numDecoders, *Size)
  
  ; graphic
  GdipCreateFromHDC(*hDC, *graphics) 
  GdipDeleteGraphics(*graphics)
  GdipSetSmoothingMode(*graphics, Mode.l) 
  GdipGraphicsClear(*graphics, color.l)
  GdipDrawBezier(*graphics, *Pen, x1.f, y1.f, x2.f, y2.f, x3.f, y3.f, x4.f, y4.f) 
  GdipDrawLineI(*graphics, *Pen, x1.l, y1.l, x2.l, y2.l)
  GdipDrawEllipseI(*graphics, *Pen, x.l, y.l, Width.l, Height.l)
  GdipDrawString(*graphics, string.p-unicode, length.l, *Font, *layoutRect, *stringFormat, *Brush)
  
  ; font
  GdipCreateFont(*FontFamily, emSize.f, style.l, unit.l, *Font)
  GdipCreateFontFamilyFromName(FontName.p-unicode, *FontCollection, *FontFamily)
  GdipDeleteFontFamily(*FontFamily)
  GdipDeleteFont(*Font)
  
  ; brush
  GdipCreateHatchBrush(hatchstyle.l, forecol.l, backcol.l, *Brush)
  GdipCreateSolidFill(color.l, *Brush)
  GdipGetBrushType(*Brush, Type.l)
  GdipDeleteBrush(*Brush)
  
  ; pen
  GdipCreatePen1(color.l, Width.f, unit.l, *Pen) 
  GdipCreatePen2(Brush.l, Width.f, unit.l, *Pen) 
  GdipSetPenStartCap(*Pen, customCap.l) 
  GdipSetPenEndCap(*Pen, customCap.l) 
  GdipDeletePen(*Pen) 
  
EndImport

Structure RectF 
  left.f 
  top.f 
  Width.f 
  Height.f 
EndStructure 
Structure ImageCodecInfo
  clsid.CLSID
  FormatID.GUID
  *CodecName;.s
  *DllName;.s
  *FormatDescription;.s
  *FilenameExtension;.s
  *MimeType;.s
  flags.l
  Version.l
  SigCount.l
  SigSize.l
  *SigPattern
  *SigMask
EndStructure
Structure GdipStartupInput 
  GdiPlusVersion.l 
  DebugEventCallback.l 
  SuppressBackgroundThread.l 
  SuppressExternalCodecs.l 
EndStructure 

Enumeration 0 ; BrushType
  #BrushTypeSolidColor
  #BrushTypeHatchFill
  #BrushTypeTextureFill
  #BrushTypePathGradient
  #BrushTypeLinearGradient
EndEnumeration
Enumeration 0 ; FontStyle
  #FontStyleRegular    = 0
  #FontStyleBold       = 1
  #FontStyleItalic     = 2
  #FontStyleBoldItalic = 3
  #FontStyleUnderline  = 4
  #FontStyleStrikeout  = 8
EndEnumeration

Macro WCHAR(unicode)
  PeekS(unicode, -1, #PB_Unicode)
EndMacro
Macro ARGB(Alpha, RGB)
  (Blue(RGB)|Green(RGB)<<8|Red(RGB)<<16|Alpha<<24)
EndMacro 

;-

Procedure.l SetRectF(*rect.RectF, x.f, y.f, w.f, h.f)
  *rect\left   = x
  *rect\top    = y 
  *rect\Width  = w
  *rect\Height = h
EndProcedure

Procedure.l GdipImageDecoders()
  
  Protected num.l, Size.l
  
  GdipGetImageDecodersSize(@num, @Size)
  
  Dim info.ImageCodecInfo(Size/SizeOf(ImageCodecInfo))
  
  GdipGetImageDecoders(num, Size, @info(0))
  
  Debug "======================= DECODERS"
  For i = 0 To num - 1
    Debug WCHAR(info(i)\CodecName)
    Debug WCHAR(info(i)\MimeType)
    Debug "======================="
  Next
  
  ProcedureReturn num
  
EndProcedure
Procedure.l GdipImageEncoders()
  
  Protected num.l, Size.l
  
  GdipGetImageEncodersSize(@num, @Size)
  
  Dim info.ImageCodecInfo(Size/SizeOf(ImageCodecInfo))
  
  GdipGetImageEncoders(num, Size, @info(0))
  
  Debug "======================= ENCODERS"
  For i = 0 To num - 1
    Debug WCHAR(info(i)\CodecName)
    Debug WCHAR(info(i)\MimeType)
    Debug "======================="
  Next
  
  ProcedureReturn num
  
EndProcedure
Procedure.l GdipLine(*graphics, x1.l, y1.l, x2.l, y2.l, w.f, color.l, StartCap.l, EndCap.l) 
  Protected *Pen
  GdipCreatePen1(color, w, 2, @*Pen) 
  GdipSetPenStartCap(*Pen, StartCap) 
  GdipSetPenEndCap(*Pen, EndCap) 
  GdipDrawLineI(*graphics, *Pen, x1, y1, x2, y2) 
  GdipDeletePen(*Pen) 
EndProcedure
Procedure.l GdipBezier(*graphics, x1.f, y1.f, x2.f, y2.f, x3.f, y3.f, x4.f, y4.f, w.f, color.l, StartCap.l, EndCap.l) 
  Protected *Pen
  GdipCreatePen1(color, w, 2, @*Pen) 
  GdipSetPenStartCap(*Pen, StartCap) 
  GdipSetPenEndCap(*Pen, EndCap) 
  GdipDrawBezier(*graphics, *Pen, x1, y1, x2, y2, x3, y3, x4, y4)
  GdipDeletePen(*Pen) 
EndProcedure
Procedure.l GdipEllipse(*graphics, x.l, y.l, w.l, h.l, color.l) 
  Protected *Pen
  GdipCreatePen1(color, w, 0, @*Pen) 
  GdipDrawEllipseI(*graphics, *Pen, x, y, w, h)
  GdipDeletePen(*Pen) 
EndProcedure
Procedure.l GdipString(*graphics, string.s, x.l, y.l, w.l, h.l, fntName.s, fntSize.f, fntStyle.l, color1.l, color2.l) 
  Protected *Family, *Font, *Brush, layout.RectF
  GdipCreateFontFamilyFromName(fntName, #Null, @*Family) 
  GdipCreateFont(*Family, fntSize, fntStyle, 2, @*Font) 
  GdipCreateHatchBrush(20, color1, color2, @*Brush)
  SetRectF(layout, x, y, w, h)
  GdipDrawString(*graphics, string, -1, *Font, layout, #Null, *Brush) 
  GdipDeleteFontFamily(*Family) 
  GdipDeleteFont(*Font) 
  GdipDeleteBrush(*Brush) 
EndProcedure 

;-

Procedure.l myWindowCallback(*window, message.l, wParam.l, lParam.l)
  
  Protected result.l, *hDC, *graphics
  
  result = #PB_ProcessPureBasicEvents
  
  Select message
    
    Case #WM_MOUSEMOVE
      *hDC = StartDrawing(WindowOutput(0)) 
      If *hDC
        GdipCreateFromHDC(*hDC, @*graphics)
        GdipSetSmoothingMode(*graphics, 2)
        For i = 0 To 5
          GdipEllipse(*graphics, WindowMouseX(0)+Random(50)-25, WindowMouseY(0)+Random(50)-25, Random(30), Random(30), ARGB(10, #Gray)) 
        Next
        GdipDeleteGraphics(*graphics)
        StopDrawing() 
      EndIf
      
    Case #WM_ERASEBKGND
      *hDC = StartDrawing(WindowOutput(0)) 
      If *hDC
        GdipCreateFromHDC(*hDC, @*graphics)
        GdipSetSmoothingMode(*graphics, 2)
        GdipGraphicsClear(*graphics, ARGB(255, $222222))
        GdipLine(*graphics, 550, 70, 20, 200, 20, ARGB(255, #Yellow), 18, 18) 
        GdipLine(*graphics, 100, 50, 600, 200, 30, ARGB(127, #Green), 17, 2) 
        GdipLine(*graphics, 600, 100, 80, 400, 40, ARGB(127, #Blue), 19, 19) 
        GdipBezier(*graphics, 50, 50, 150, 60, 300, 250, 20, 500, 16, ARGB(127, #Red), 18, 18)
        GdipString(*graphics, "GDI+ 1.0 & PB4.0", 280, 280, 380, 200, "Arial", 80, #FontStyleBoldItalic|#FontStyleUnderline, ARGB(10, #Black), ARGB(80, #Blue)) 
        GdipDeleteGraphics(*graphics)
        StopDrawing() 
      EndIf
      
  EndSelect
  
  ProcedureReturn result
  
EndProcedure

;-

Define *token, input.GdipStartupInput

input\GdiPlusVersion = 1 
GdiplusStartup(@*token, @input, #Null) 

GdipImageDecoders()
GdipImageEncoders()

If OpenWindow(0, 0, 0, 640, 480, "GdiPlus 1.0", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  SetWindowCallback(@myWindowCallback(), 0)
  SendMessage_(WindowID(0), #WM_ERASEBKGND, 0, 0)
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

GdiplusShutdown(*token)

;-

End
Dernière modification par Flype le sam. 15/juil./2006 7:09, modifié 2 fois.
Image
Anonyme2
Messages : 3518
Inscription : jeu. 22/janv./2004 14:31
Localisation : Sourans

Message par Anonyme2 »

Excellent :D
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Il est où le .lib ?
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

apparemment moi je l'ai grace au PSDK Microsoft.

j'ai fais quelques recherches sur les forums PB à propos de cette lib graphique et j'ai pu compiler une archive de plusieurs codes vus à droite, à gauche. le tout mis à la sauce PB4...

voilà l'archive sur laquelle je bosse en ce moment - j'y ai inclus le .lib, la plus récente .dll (xp sp2), une 15aines de fichiers tests, et surtout l'include générale : gdiplus.pbi.

voir ici:
http://purebasic.forum-gratuit.com/view ... highlight=
Image
einander
Messages : 7
Inscription : mer. 04/mai/2005 9:40
Localisation : Galicia - Espagne

Message par einander »

Beau travail Flype!
erix14
Messages : 480
Inscription : sam. 27/mars/2004 16:44
Contact :

Message par erix14 »

Je travaille avec GDI+ depuis un petit moment, moi aussi j'avais répertorié tous les codes des différents forums. Mais tu en as fait une meilleure synthèse que moi. Et je t'en remercie :D
C'est une DLL vraiment formidable, qui mériterait d'être approfondi encore plus, avec la création d'un fichier d'aide .chm :wink:
Si il y a un projet de groupe sur l'analyse de cette DLL, je veux bien y participer :D
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

merci c'est surtout grace à PB4:

Import, Macro, Unicode, Pseudotype, ...

toutes ces fonctionnalités contribuent enfin à rendre l'utilisation de cette lib très agréable.
Image
erix14
Messages : 480
Inscription : sam. 27/mars/2004 16:44
Contact :

Message par erix14 »

Flype, encore merci pour ton travail... je voudrais en profiter pour te demander si tu as déjà essayé de charger des images(des png dans mon cas) depuis les ressources avec la fonction GdipCreateBitmapFromResource, car là, j'y suis pas encore arrivé :?
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

Message par Flype »

je vais essayé pour voir...

au fait c'est avec GDI que tu avais programmé ton IDE (le designer jamais fini) ? le rendu était super.
Image
erix14
Messages : 480
Inscription : sam. 27/mars/2004 16:44
Contact :

Message par erix14 »

Non, à l'époque je ne connaissais malheureusement pas GDI+, ce qui est bien en informatique, c'est que l'on en apprend tous les jours :D
Pour l'instant, j'utilise GDI+ seulement pour faire des widgets (voici un exemple : http://www.rx14.info/archives/Horloge.exe [bouton gauche de la souris pour déplacer l'horloge, clic droit pour la fermer])
Mais je compte l'utiliser de plus en plus, comme cette DLL est par défaut dans toutes les nouvelles versions de Windows depuis Windows 2000, il me semble.
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Vraiment excellent! :o
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Je crois que la Dll est en standard depuis WindowXP et server2003!

http://msdn.microsoft.com/library/defau ... erence.asp

C'est dommage que l'horloge consomme un peu trop de ressources, non?
erix14
Messages : 480
Inscription : sam. 27/mars/2004 16:44
Contact :

Message par erix14 »

C'est dommage que l'horloge consomme un peu trop de ressources, non?
Sur mon ordi, ça représente environ 1 à 2% du CPU. C'est le problème des horloges à aiguilles, il faut les faire tourner :D
Mais sache qu'une Widget horloge sous Opera 9 en prend plus de 15%...
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Beaucoup plus sur mon ordi: 46% 8O
erix14
Messages : 480
Inscription : sam. 27/mars/2004 16:44
Contact :

Message par erix14 »

8O Moi j'ai un P4 2.4GHz, il faudrait que d'autres personnes testent l'horloge...
Répondre