IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

passer d'une procédure à une fonction


Sujet :

Langage Delphi

  1. #1
    Membre régulier
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2010
    Messages : 104
    Points : 85
    Points
    85
    Par défaut passer d'une procédure à une fonction
    Bonjour à tous,
    J'ai une image et je veux pouvoir en cours d'éxécution changer la couleur du fond (le blanc).
    Voici un code que j'ai récupéré sur internet et un peu modifié (simplifié) et qui fonctionne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    procedure TForm1.Button2Click(Sender: TObject);
    type
       TRGBTripleArray = ARRAY[Word] of TRGBTriple;
       pRGBTripleArray = ^TRGBTripleArray;         
    var
        x,y : Integer;
        BitMap, NewBitMap : TBitMap;
        P, NewP : pRGBTripleArray;        
     
    begin
        BitMap := TBitMap.create;
        NewBitMap := TBitMap.create;
        try
          Bitmap.LoadFromResourceName(hInstance,'MonImage' );  // MonImage est une ressource bitmap en mémoire
          BitMap.PixelFormat := pf24bit;    
          NewBitMap.PixelFormat := pf24bit;
          NewBitMap.Height := BitMap.Height;
          NewBitMap.Width := BitMap.Width;
          for y := 0 to BitMap.Height - 1 do  begin
            for x := 0 to BitMap.Width - 1 do   begin
              P := BitMap.ScanLine[y];       
              NewP := NewBitMap.ScanLine[y]; 
     
             if P[x].rgbtRed=255 then P[x].rgbtRed:=255-100;                    // Remplacement du fond blanc 
             if P[x].rgbtGreen=255 then P[x].rgbtGreen:=255-100;               // Remplacement du fond blanc 
             if P[x].rgbtBlue=255 then P[x].rgbtBlue:=255-100;                    // Remplacement du fond blanc 
             NewP[x] := P[x];                
          end;
        end;
     
        Canvas.Draw(200, 200, NewBitMap);   / affichage de ma nouvelle image dont le fond a été modifié
     
       finally
          BitMap.Free;
          NewBitMap.Free;
       end;
    end;
    Je voudrais remplacer cette procédure par la fonction suivante (je n'ai quasiment rien changé) :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    function ModifierFondImage(ValRouge,ValVert,ValBleu:integer):TbitMap;
    type
     TRGBTripleArray = ARRAY[Word] of TRGBTriple;       ' <-----   j''obtiens une erreur ici : le "TRGBTriple" est souligné '
     pRGBTripleArray = ^TRGBTripleArray;                '           et j''ai le message suivant à la compil : E2003 Identificateur non déclaré ': 
    var
      x,y : Integer;
      BitMap, NewBitMap : TBitMap;
      P, NewP : pRGBTripleArray;        
     
    begin
      BitMap := TBitMap.create;
      NewBitMap := TBitMap.create;
      try
       Bitmap.LoadFromResourceName(hInstance,'MonImage' );  // MonImage est une ressource bitmap en mémoire
       BitMap.PixelFormat := pf24bit;    
       NewBitMap.PixelFormat := pf24bit;
       NewBitMap.Height := BitMap.Height;
       NewBitMap.Width := BitMap.Width;
       for y := 0 to BitMap.Height - 1 do  begin
        for x := 0 to BitMap.Width - 1 do   begin
         P := BitMap.ScanLine[y];       
         NewP := NewBitMap.ScanLine[y]; 
     
         if P[x].rgbtRed=255 then P[x].rgbtRed:=255-ValRouge;                  // Modification du  rouge
         if P[x].rgbtGreen=255 then P[x].rgbtGreen:=255-ValVert;               //       "           ''   vert 
         if P[x].rgbtBlue=255 then P[x].rgbtBlue:=255-ValBleu;                   //        ''          ''   bleu   
         NewP[x] := P[x];                
        end;
       end;
     
      result:= NewBitMap;   // Ma nouvelle image modifiée
     
      finally
        BitMap.Free;
        NewBitMap.Free;
      end;
    end;

    J'ai bien mis "Vcl.Graphics" dans les uses.

    Mais j'obtiens l'erreur suivante lors de la compilation :
    (DCC Erreur] Affichage_jeux.pas(513): E2003 Identificateur non déclaré : 'TRGBTriple'
    Merci de votre aide

    Jean-Louis

  2. #2
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 367
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 367
    Points : 3 138
    Points
    3 138
    Par défaut
    Bonjour,

    chez moi avec D7 ton code compile sans problème ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;
     
    type
      TForm1 = class(TForm)
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    function ModifierFondImage(ValRouge,ValVert,ValBleu:integer):TbitMap;
    type
     TRGBTripleArray = ARRAY[Word] of TRGBTriple;      // ' <-----   j''obtiens une erreur ici : le "TRGBTriple" est souligné '
     pRGBTripleArray = ^TRGBTripleArray;               // '           et j''ai le message suivant à la compil : E2003 Identificateur non déclaré ': 
    var
      x,y : Integer;
      BitMap, NewBitMap : TBitMap;
      P, NewP : pRGBTripleArray;        
     
    begin
      BitMap := TBitMap.create;
      NewBitMap := TBitMap.create;
      try
       Bitmap.LoadFromResourceName(hInstance,'MonImage' );  // MonImage est une ressource bitmap en mémoire
       BitMap.PixelFormat := pf24bit;    
       NewBitMap.PixelFormat := pf24bit;
       NewBitMap.Height := BitMap.Height;
       NewBitMap.Width := BitMap.Width;
       for y := 0 to BitMap.Height - 1 do  begin
        for x := 0 to BitMap.Width - 1 do   begin
         P := BitMap.ScanLine[y];       
         NewP := NewBitMap.ScanLine[y]; 
     
         if P[x].rgbtRed=255 then P[x].rgbtRed:=255-ValRouge;                  // Modification du  rouge
         if P[x].rgbtGreen=255 then P[x].rgbtGreen:=255-ValVert;               //       "           ''   vert 
         if P[x].rgbtBlue=255 then P[x].rgbtBlue:=255-ValBleu;                   //        ''          ''   bleu   
         NewP[x] := P[x];                
        end;
       end;
     
      result:= NewBitMap;   // Ma nouvelle image modifiée
     
      finally
        BitMap.Free;
        NewBitMap.Free;
      end;
    end;
     
     
     
    end.
    A+
    Charly

  3. #3
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 367
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 367
    Points : 3 138
    Points
    3 138
    Par défaut
    Je regarde avec D10.3 ...

  4. #4
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 367
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 367
    Points : 3 138
    Points
    3 138
    Par défaut
    ça compile bien aussi avec D10.3 ??

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    unit Unit1;
     
    interface
     
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
     
    type
      TForm1 = class(TForm)
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
     
    function ModifierFondImage(ValRouge,ValVert,ValBleu:integer):TbitMap;
    type
     TRGBTripleArray = ARRAY[Word] of TRGBTriple;      // ' <-----   j''obtiens une erreur ici : le "TRGBTriple" est souligné '
     pRGBTripleArray = ^TRGBTripleArray;               // '           et j''ai le message suivant à la compil : E2003 Identificateur non déclaré ':
    var
      x,y : Integer;
      BitMap, NewBitMap : TBitMap;
      P, NewP : pRGBTripleArray;
     
    begin
      BitMap := TBitMap.create;
      NewBitMap := TBitMap.create;
      try
       Bitmap.LoadFromResourceName(hInstance,'MonImage' );  // MonImage est une ressource bitmap en mémoire
       BitMap.PixelFormat := pf24bit;
       NewBitMap.PixelFormat := pf24bit;
       NewBitMap.Height := BitMap.Height;
       NewBitMap.Width := BitMap.Width;
       for y := 0 to BitMap.Height - 1 do  begin
        for x := 0 to BitMap.Width - 1 do   begin
         P := BitMap.ScanLine[y];
         NewP := NewBitMap.ScanLine[y];
     
         if P[x].rgbtRed=255 then P[x].rgbtRed:=255-ValRouge;                  // Modification du  rouge
         if P[x].rgbtGreen=255 then P[x].rgbtGreen:=255-ValVert;               //       "           ''   vert
         if P[x].rgbtBlue=255 then P[x].rgbtBlue:=255-ValBleu;                   //        ''          ''   bleu
         NewP[x] := P[x];
        end;
       end;
     
      result:= NewBitMap;   // Ma nouvelle image modifiée
     
      finally
        BitMap.Free;
        NewBitMap.Free;
      end;
    end;
     
    end.
    Teste juste mon code sans rien d'autre ...

    A+
    Charly

  5. #5
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 745
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 745
    Points : 13 306
    Points
    13 306
    Par défaut
    TRGBTriple est déclaré dans Winapi.Windows.

    ps : tu ne dois pas libérer NewBitMap en fin de routine, result := NewBitMap n'est qu'une copie du pointeur. C'est à la procédure appelante que revient la tâche de libérer l'image.
    Un seul TBitmap serait aussi suffisant et en déclarer un n'est pas utile, travaille directement sur Result.

  6. #6
    Membre régulier
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2010
    Messages : 104
    Points : 85
    Points
    85
    Par défaut
    Citation Envoyé par Charly910 Voir le message
    Bonjour,

    chez moi avec D7 ton code compile sans problème ?
    Je suis sous XE2

  7. #7
    Membre régulier
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2010
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2010
    Messages : 104
    Points : 85
    Points
    85
    Par défaut
    Merci,ça fonctionne en mettant la fonction dans la fiche principale (dans une autre fiche ça fonctionnerait également).
    J'avais mis la fonction dans une unité sans fiche.
    Et dans ces conditions, ça ne fonctionne pas. Mais je ne vois pas pourquoi .

    JL

  8. #8
    Modérateur
    Avatar de tourlourou
    Homme Profil pro
    Biologiste ; Progr(amateur)
    Inscrit en
    Mars 2005
    Messages
    3 862
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Biologiste ; Progr(amateur)

    Informations forums :
    Inscription : Mars 2005
    Messages : 3 862
    Points : 11 330
    Points
    11 330
    Billets dans le blog
    6
    Par défaut
    Une unité ajoutée en uses du fait de la fiche, qui déclare le type ?
    Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
    . Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !

  9. #9
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 367
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 367
    Points : 3 138
    Points
    3 138
    Par défaut
    En ajoutant Unit2 sans Form, et en déplaçant la fonction, cela compile bien en D10.3 (sans corriger les erreurs signalées par Andnotor) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    unit Unit2;
     
    interface
    uses
      Winapi.Windows, Vcl.Graphics ;
     
    implementation
    function ModifierFondImage(ValRouge,ValVert,ValBleu:integer):TbitMap;
    type
     TRGBTripleArray = ARRAY[Word] of TRGBTriple;      // ' <-----   j''obtiens une erreur ici : le "TRGBTriple" est souligné '
     pRGBTripleArray = ^TRGBTripleArray;               // '           et j''ai le message suivant à la compil : E2003 Identificateur non déclaré ':
    var
      x,y : Integer;
      BitMap, NewBitMap : TBitMap;
      P, NewP : pRGBTripleArray;
     
    begin
      BitMap := TBitMap.create;
      NewBitMap := TBitMap.create;
      try
       Bitmap.LoadFromResourceName(hInstance,'MonImage' );  // MonImage est une ressource bitmap en mémoire
       BitMap.PixelFormat := pf24bit;
       NewBitMap.PixelFormat := pf24bit;
       NewBitMap.Height := BitMap.Height;
       NewBitMap.Width := BitMap.Width;
       for y := 0 to BitMap.Height - 1 do  begin
        for x := 0 to BitMap.Width - 1 do   begin
         P := BitMap.ScanLine[y];
         NewP := NewBitMap.ScanLine[y];
     
         if P[x].rgbtRed=255 then P[x].rgbtRed:=255-ValRouge;                  // Modification du  rouge
         if P[x].rgbtGreen=255 then P[x].rgbtGreen:=255-ValVert;               //       "           ''   vert
         if P[x].rgbtBlue=255 then P[x].rgbtBlue:=255-ValBleu;                   //        ''          ''   bleu
         NewP[x] := P[x];
        end;
       end;
     
      result:= NewBitMap;   // Ma nouvelle image modifiée
     
      finally
        BitMap.Free;
        NewBitMap.Free;
      end;
    end;
    end.
    Bizarre ?

    A+
    Charly

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 28/11/2018, 08h23
  2. Réponses: 5
    Dernier message: 14/12/2011, 12h55
  3. Réponses: 1
    Dernier message: 23/02/2007, 08h49
  4. Réponses: 11
    Dernier message: 10/05/2004, 10h49
  5. passer FILE* en argument d une fonction
    Par Monsieur_Manu dans le forum C
    Réponses: 9
    Dernier message: 10/04/2003, 17h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo