image in the grid

1 Answer 26 Views
GridView
Laurent
Top achievements
Rank 1
Iron
Laurent asked on 14 Aug 2024, 07:55 AM

Objective: get the icon of a file (word,excel,pdf,...) 

In L4G, I use the ExtractAssociatedIconA function to obtain a handle for an icon stored as a resource in a file or an icon stored in the executable file associated with a file.
And I associate it with a button using the SendMessageA function.

Example code:
  DEFINE VARIABLE FILENAME AS CHARACTER NO-UNDO.        
  DEFINE VARIABLE IconInd AS INTEGER NO-UNDO.
  DEFINE VARIABLE hIcon AS INTEGER NO-UNDO.
  DEFINE VARIABLE iOldIcon AS INTEGER NO-UNDO.

  DO WITH FRAME {&FRAME-NAME}: 
    IF NUM-ENTRIES(RowObject.com_cha:SCREEN-VALUE,CHR(9)) > 0 THEN
      DO i = 1 TO NUM-ENTRIES(RowObject.com_cha:SCREEN-VALUE,CHR(9)):
          VIEW hbutton[i] hfillin[i].
          hbutton[i]:SELECTABLE = TRUE.
          ASSIGN DisplayName = ENTRY(i,RowObject.com_cha:SCREEN-VALUE,CHR(9)).
          IF Filename <> "" THEN
          DO:
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT NomFich,INPUT-OUTPUT IconInd,OUTPUT hIcon).
            RUN SendMessageA( hButton[i]:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, 0, OUTPUT iOldIcon).
            RUN SendMessageA( hButton[i]:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
          END.

I want to do the same thing with a RadGridView? I've added an ima column as type GridViewImageColumn and in the : 
METHOD PRIVATE VOID GridLien_CellFormatting I'd like to put the image 
RUN SendMessageA( e:CellElement:?????? {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
Can you help me?
Thank you

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 14 Aug 2024, 10:54 AM

Hello, Laurent,

GridViewImageColumn  displays images for database columns of Image data type. If you are creating GridViewImageColumn, and you have a whole column with images, I would recomend you to specify the FieldName property to which the column is bound. More useful information is available here: GridViewImageColumn - WinForms GridView Control - Telerik UI for WinForms

Alternatively, you can set image in CellFormatting event of RadGridView, passing the desired image to e.CellElement.Image:

void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.ColumnIndex == 3)
    {
        e.CellElement.Image = Resources.test;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.ImageProperty, Telerik.WinControls.ValueResetFlags.Local);
    }
}

I hope this information helps. If you have any other questions, please let me know. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Laurent
Top achievements
Rank 1
Iron
commented on 14 Aug 2024, 12:16 PM | edited

Thank you for your feedback,
But I don't think we understand.
Using:
e:CellElement:value = Image:FromFile(clsSession:RepDir + ‘\gui\icone\taskC-16.png’).                                         
It works but I use ExtractAssociatedIconA and SendMessageA and the 1st INPUT of SendMessageA is a HWND

Here is my code:
IF e:CellElement:ColumnInfo:name EQ ‘ima_lien’ AND e:CellElement:value EQ ? THEN DO:        
            
            cFile= ttEtuLien.rep_lien + ttEtuLien.fic_lien + ‘.’ + ttEtuLien.ext_lien.     
          .    
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT cFile,INPUT-OUTPUT IconInd,OUTPUT hIcon).  
                
            RUN SendMessageA( HERE YOU NEED xxxx:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
         
        END.
Nadya | Tech Support Engineer
Telerik team
commented on 15 Aug 2024, 11:57 AM

Hello, Laurent,

If I understand your case correctly, you use ExtractAssociatedIconA function to gets a handle to an icon stored as a resource. Then, you need to use this icon in cells in GridViewImageColumn. Please correct me if I am wrong. 

Note, RadGridview can display images which are Bitmap. The possible solution that I can suggest is to construct a new BitMap from a file, and the pass it to the cells in grid. You can use Bitmap.FromHicon() method to create a Bitmap from a Windows handle to an icon. Feel free to research any other solutions that might be suitable for your specific case to extract the icon.

Here is how you can assign a BitMap to cells in CellFormatting event:

void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.RowIndex == count - 1 && e.ColumnIndex == header)
    {
        e.CellElement.Image = new Bitmap(Resources.test);
    }
}

I hope this information is useful. Please let me know if you have other questions. 

Laurent
Top achievements
Rank 1
Iron
commented on 21 Aug 2024, 07:58 AM

Merci pour les informations.

J'ai réussi.

DEFINE VARIABLE oImageGrid      AS Bitmap NO-UNDO.
DEFINE VARIABLE oIntPtr         AS IntPtr NO-UNDO.

 

METHOD PRIVATE VOID GridLien_CellFormatting( INPUT sender AS System.Object, INPUT e AS Telerik.WinControls.UI.CellFormattingEventArgs ):
DEFINE VARIABLE cFichier    AS CHARACTER NO-UNDO.
DEFINE VARIABLE IconInd     AS INTEGER NO-UNDO.
        DEFINE VARIABLE hIcon       AS INTEGER NO-UNDO. 
        DEFINE VARIABLE iRow        AS INTEGER NO-UNDO.
        DEFINE VARIABLE hImaHwnd    AS INT64  NO-UNDO.
        
        DEFINE VARIABLE cRepLien AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFicLien AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cExtLien AS CHARACTER NO-UNDO.
        
        IF NOT CAN-FIND (FIRST ttEtuLien) THEN RETURN.        
        IF ttEtuLien.cle_lien EQ 0 THEN RETURN.
        
        iRow = THIS-OBJECT:GridLien:CurrentRow:Index.        
        
        ASSIGN iRow = e:CellElement:RowIndex.
      
        IF iRow LT 0 OR iRow GT GridLien:ChildRows:Count - 1 THEN RETURN.        
                    
        IF TYPE-OF(e:CellElement,GridImageCellElement)  THEN DO:
                        
            cRepLien = GridLien:Rows:item[iRow]:Cells:Item["rep_lien"]:value:ToString().
            cFicLien = GridLien:Rows:item[iRow]:Cells:Item["fic_lien"]:value:ToString().
            cExtLien = GridLien:Rows:item[iRow]:Cells:Item["ext_lien"]:value:ToString().               
            cFichier= cRepLien + cFicLien + '.' + cExtLien.
            
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT cFichier,INPUT-OUTPUT IconInd,OUTPUT hIcon). 
                        
            oIntPtr= NEW IntPtr(hIcon).            
            oImageGrid = Bitmap:FromHicon(oIntPtr).                        
            e:CellElement:image = NEW Bitmap(oImageGrid).
            
            RETURN.                                         
        END.
        
RETURN.

END METHOD.
Nadya | Tech Support Engineer
Telerik team
commented on 21 Aug 2024, 09:02 AM

Hello, Laurent,

I am glad that the suggested solution helps you to achieve your goal. Thank you for sharing your code snippet.

Do not hesitate to contact us if you have other questions.

Tags
GridView
Asked by
Laurent
Top achievements
Rank 1
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or