Zugriff auf Datei- Icons zweck Darstelung in StringGrid



  • @akari

    Schau Du Dir mal die Funktion ExtractIcon an, die ist mit Sicherheit nicht die Richtige!



  • Hallo

    sorry, natürlich muß es ExtractAssociatedIcon sein

    bis bald
    akari



  • Danke!

    leider tue ich mich etwas schwer die Hilfe zu checken 😞

    HICON ExtractAssociatedIcon(
    
        HINSTANCE hInst,	// application instance handle
        LPTSTR lpIconPath,	// path and filename of file for which icon is wanted
        LPWORD lpiIcon 	// pointer to icon index
       );
    

    HINSTABCE = Application->Handle ??
    LPTSR = "c:\temp.test.pdf" ?? (keine Infos in der Hilfe)
    LPWORD = ???

    Besonders bei LPWord habe ich garkeine Vorstellung wodrum es sich dabei handeln soll. EIn Pointer auf eine ImageList?

    Ein OnDrawCell Event habe ich bereits. Es stellt auch fleißig Icons dar, aber nur jene, welche vorher manuell in eine ImageList geladenhabe (EIn Verfahren zweifelhafter Natur....)

    Könnt Ihr mir nochmal helfen?
    Danke!"



  • Hallo

    diese Funktion habe ich für sowas geschrieben :

    // Ermittelt das zugeordnete Icon einer Datei bzw. einer Extension und zeichnet sie
      // in ein Canvas
      int DrawAssociatedFileIcon(AnsiString FileName, TCanvas *Canvas, int X, int Y, unsigned short IconIndex)
      {
        HANDLE hicon;                         // Iconhandle
    
        hicon = ExtractAssociatedIcon(Application->Handle, FileName.c_str(), &IconIndex); // Icon bestimmen
        if (hicon == NULL) return(-1);        // Wenn kein Icon gefunden, abbrechen
        DrawIcon(Canvas->Handle, X, Y, hicon); // Icon zeichen
        return(0);
        }
    
    // Aufruf für Ausgabe in einem StringGrid an bestimmter Position
    DrawAssociatedFileIcon("C:\\test.pdf", StringGrid->Canvas, 100, 200, 0);
    

    bis bald
    akari



  • Vielen Dank!

    Jetzt habe ich nur noch zwei Fragen.

    1. Kann ich mir auch Icons nur Anhand des Filenamens geben lassen? Grund ist, dass ich Files in einer DB Anzeige und es diese Files folglich nicht auf der Festplatte des Computers gibt.

    2. Ich bekomme immer die große,also 32 *32 Pixel große Iconversion. Ich möchte das Icon aber in 16 * 16 Pixeln haben. Muss ich das Icon selber runterskalieren , oder kann ich mir auch ein kleines geben lassen?
    a.: wenn ich es selber runterskalieren muss, wie mache ich das???



  • Hallo

    1. Dazu mußt du wohl die Registry auswerten und selber die entsprechenden Dateierweitungs-Einträge und Programm-Links auswerten. (siehe HKEY_CLASSES_ROOT).

    2. Das interssiert mich auch, denn ich habe soetwas auch schon probiert. Eine spezielle Funktion habe ich nicht gefunden, und das einfache manuelle Runterrechnen (jede zweite Zeile und Spalte weglassen) sah Mist aus.
    Vielleicht solltest du mal im WinAPI-Foprum nachschauen.

    bis bald
    akari



  • zu 2:
    Ichhabe diese Funktion gefunden. Das Ergebnis sieht aber auch unschön aus:

    DrawIconEx Function
    
    The DrawIconEx function draws an icon or cursor into the specified device context, performing the specified raster operations, and stretching or compressing the icon or cursor as specified.
    
    Syntax
    
    BOOL DrawIconEx(      
    
        HDC hdc,
        int xLeft,
        int yTop,
        HICON hIcon,
        int cxWidth,
        int cyWidth,
        UINT istepIfAniCur,
        HBRUSH hbrFlickerFreeDraw,
        UINT diFlags
    );
    
    Parameters
    
        hdc
            [in] Handle to the device context into which the icon or cursor will be drawn. 
        xLeft
            [in] Specifies the logical x-coordinate of the upper-left corner of the icon or cursor. 
        yTop
            [in] Specifies the logical y-coordinate of the upper-left corner of the icon or cursor. 
        hIcon
            [in] Handle to the icon or cursor to be drawn. This parameter can identify an animated cursor. 
        cxWidth
            [in] Specifies the logical width of the icon or cursor. If this parameter is zero and the diFlags parameter is DI_DEFAULTSIZE, the function uses the SM_CXICON or SM_CXCURSOR system metric value to set the width. If this parameter is zero and DI_DEFAULTSIZE is not used, the function uses the actual resource width. 
        cyWidth
            [in] Specifies the logical height of the icon or cursor. If this parameter is zero and the diFlags parameter is DI_DEFAULTSIZE, the function uses the SM_CYICON or SM_CYCURSOR system metric value to set the width. If this parameter is zero and DI_DEFAULTSIZE is not used, the function uses the actual resource height. 
        istepIfAniCur
            [in] Specifies the index of the frame to draw, if hIcon identifies an animated cursor. This parameter is ignored if hIcon does not identify an animated cursor. 
        hbrFlickerFreeDraw
            [in] Handle to a brush that the system uses for flicker-free drawing. If hbrFlickerFreeDraw is a valid brush handle, the system creates an offscreen bitmap using the specified brush for the background color, draws the icon or cursor into the bitmap, and then copies the bitmap into the device context identified by hdc. If hbrFlickerFreeDraw is NULL, the system draws the icon or cursor directly into the device context. 
        diFlags
            [in] Specifies the drawing flags. This parameter can be one of the following values:
    
            DI_COMPAT
                Draws the icon or cursor using the system default image rather than the user-specified image. For more information, see About Cursors. Windows NT4.0 and later: This flag is ignored.
            DI_DEFAULTSIZE
                Draws the icon or cursor using the width and height specified by the system metric values for cursors or icons, if the cxWidth and cyWidth parameters are set to zero. If this flag is not specified and cxWidth and cyWidth are set to zero, the function uses the actual resource size. 
            DI_IMAGE
                Draws the icon or cursor using the image.
            DI_MASK
                Draws the icon or cursor using the mask.
            DI_NOMIRROR
                Windows XP: Draws the icon as an unmirrored icon. By default, the icon is drawn as a mirrored icon if hdc is mirrored.
            DI_NORMAL
                Combination of DI_IMAGE and DI_MASK.
    
    Return Value
    
        If the function succeeds, the return value is nonzero.
    
        If the function fails, the return value is zero. To get extended error information, call GetLastError.
    
    Remarks
    
        The DrawIconEx function places the icon's upper-left corner at the location specified by the xLeft and yTop parameters. The location is subject to the current mapping mode of the device context.
    
        To duplicate DrawIcon (hDC, X, Y, hIcon), call DrawIconEx as follows:
    
    DrawIconEx (hDC, X, Y, hIcon, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE);
    


  • Diese Funktion sieht vernünftig aus:

    ExtractIconEx Function
    
    The ExtractIconEx function creates an array of handles to large or small icons extracted from the specified executable file, dynamic-link library (DLL), or icon file.
    
    Syntax
    
    UINT ExtractIconEx(      
    
        LPCTSTR lpszFile,
        int nIconIndex,
        HICON *phiconLarge,
        HICON *phiconSmall,
        UINT nIcons
    );
    

    ledier habe ich keinen Plan wie sie "bedient" wird.
    Weiß es jemand???
    Danke!



  • schaut mal hier:

    http://www.c-plusplus.net/forum/viewtopic.php?t=42476
    http://www.c-plusplus.net/forum/viewtopic.php?t=17051

    komischer weise ist der faq eintrag zu SHGetFileInfo nicht mehr vorhanden.



  • Ja, bedauerlicherweise sind einige FAQ-Beiträge einem Datenbankproblem zum Opfer gefallen, so auch deiner (?) zur Verwendung der Standardicons. 😞



  • Danke, diese Funktion sieht sehr vielversprechend aus! 🙂

    Wenn ich jetzt noch rauskriege wie ich das icon male....

    SHFILEINFO Info;
        THandle    ImageHandle;
        ImageHandle = SHGetFileInfo("DLL.pdf", 0, &Info, sizeof(Info), SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
    
        DrawIcon(DataGrid->Canvas->Handle, Rect.Left+1,Rect.Top+1, &ImageHandle);
    

    meckert zwar nicht, will aber auch nicht. (kein Icon...). Dabei st es föllig egal was im ersten Parameter steht.

    jemand ne idee?



  • hier mal das beispiel aus der Win32-Hilfe.

    HICON  hGen32Icon;
    SHFILEINFO  shfi;
    
    if (SHGetFileInfo ((LPCSTR)"C:\\MySamplesDir\\Gen32\\Gen32.Exe",
                       0,
                       &shfi,
                       sizeof (SHFILEINFO),
                       SHGFI_ICON))
    {
      hGen32Icon = shfi.hIcon;
    
      ...
    }
    

    In Deinem Beispiel ist ImageHandle ist ein Zeiger auf die System-ImageList. Diese könntest Du zum Beispiel einer ListView zuweisen.



  • Danke alle, jetzt geht es!

    Mit Sundays Code wird das richtige Icon eingebunden. Ledier wird es immer default auf 32 *32 skaliert. Das mit 'SHGFI_SMALLICON' jedoch das kleine Icon genommen wird, sieht das Resultat ziemlich unschön aus. (sehr grobes großes Icon).
    Der Trick ist das Icon anschließend mit DrawIconEx() runter zu skalieren, dann wird alles schön.

    Code zum erstellen eines Systemicons im OnDrawCell Event eines String Grids:

    void __fastcall TfMain::DataGridDrawCell(TObject *Sender, int ACol,
          int ARow, TRect &Rect, TGridDrawState State)
      {
      // nur in der ersten Spalte, nicht in der ersten Zeile
      if (ACol == 1 && ARow != 0)
        {
        // nur wenn inhalt in der Spalte steht (Inhalt = Rückgabe ExtractFileExt() !!)
        if (DataGrid->Cells[ACol][ARow].data() != NULL)
          {
          String FileName = "*" + DataGrid->Cells[ACol][ARow];
          HICON  hGen16Icon;
          SHFILEINFO  shfi;
          unsigned short IconIndex = 0;
    
          // Systemincon zu einer Datei wird ermitelt
          if (SHGetFileInfo (FileName.c_str(),
                       0,
                       &shfi,
                       sizeof (SHFILEINFO),
                       SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_SMALLICON))
            {
            hGen16Icon = shfi.hIcon;
            if (hGen16Icon == NULL) return;
            // Text in Spalte übermalen (löschen, nur Icon ist gewollt)
            DataGrid->Canvas->FillRect(Rect);
            // Systemicon wird dargestellt.
            DrawIconEx(DataGrid->Canvas->Handle, Rect.Left+1,Rect.Top+1, hGen16Icon, 16, 16, 0, NULL, DI_NORMAL);
            }
          }
        }
      }
    

Anmelden zum Antworten