Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

¿Vistas en Miniatura e Iconos de Archivos para un ListView?
Ir a página 1, 2  Siguiente
 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: ¿Vistas en Miniatura e Iconos de Archivos para un ListView? Responder citando

Estoy desarrollando un proyecto en donde uso un ListView para mostrar archivos como en el explorador pero no sé como obtener las vistas en miniatura de los archivos o los iconos por lo menos de los tipos de archivo.
Ayuda
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Te muestro un ejemplo de como listar en un ListView los archivos de una carpeta con sus iconos correspondientes:

Pon en un Formulario:
Un ListView con ViewStyle = vsReport, SmallImages = ImageList;
Un TImageList
Un Botón

Código:

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  DWORD Flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
  SHFILEINFO fi = {0};
  int SystemSmallIcons = SHGetFileInfo("", 0, &fi, sizeof(SHFILEINFO), Flags);
  ImageList1->Handle = SystemSmallIcons;
}

// Añade un icono a la lista del sistema y devuelve su índice
int __fastcall TForm1::AddIcon(WIN32_FIND_DATA WFData)
{
   SHFILEINFO fi;
   DWORD Flags =  SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES;

   if(WFData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
     SHGetFileInfo("", FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(SHFILEINFO), Flags);
   else
     SHGetFileInfo(WFData.cFileName, WFData.dwFileAttributes, &fi, sizeof(SHFILEINFO), Flags);

   return fi.iIcon;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TListItem *pItem;
    WIN32_FIND_DATA  WFData;
    HANDLE hFindFile;
    char Path[] = "C:\\*.*";
    if((hFindFile = FindFirstFile(Path, &WFData))!=INVALID_HANDLE_VALUE)
    do{
       pItem = ListView1->Items->Add();
       pItem->Caption = String(WFData.cFileName);
       pItem->ImageIndex = AddIcon(WFData);
    } while (FindNextFile(hFindFile, &WFData));
    FindClose(hFindFile);
}



Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

escafandra, gracias por responder. Tengo problemas para mostrar los iconos de la forma en miniatura, como el explorador:



Usted sabe cómo hacer esto?

¡Gracias!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Te muestro otro ejemplo con el que podrás visualizar los archivos en vsReport, vsList, vsIcon, y vsSmallIcon:

Partiendo de ejemplo anterior añade:
Otro ImageList.
Cuatro botones mas.


El código será este:
Código:

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
   Button1->Caption = "Dir";
   Button2->Caption = "vsSmallIcon";
   Button3->Caption = "vsReport";
   Button4->Caption = "vsIcon";
   Button5->Caption = "vsList";
   ListView1->SmallImages = ImageList1;
   ListView1->LargeImages = ImageList2;
   
   DWORD Flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
   SHFILEINFO fi = {0};
   int SystemSmallIcons = SHGetFileInfo("", 0, &fi, sizeof(SHFILEINFO), Flags);
   ImageList1->Handle = SystemSmallIcons;
   Flags = SHGFI_SYSICONINDEX | SHGFI_LARGEICON;
   int SystemLargeIcons = SHGetFileInfo("", 0, &fi, sizeof(SHFILEINFO), Flags);
   ImageList2->Handle = SystemLargeIcons;
}

// Añade un icono a la lista del sistema y devuelve su índice
int __fastcall TForm1::AddIcon(WIN32_FIND_DATA WFData)
{
   SHFILEINFO fi;
   DWORD Flags =  SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES;

   if(WFData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
     SHGetFileInfo("", FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(SHFILEINFO), Flags);
   else if(SHGetFileInfo(WFData.cFileName, FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(SHFILEINFO), SHGFI_EXETYPE)){
     SHGetFileInfo(WFData.cFileName, WFData.dwFileAttributes, &fi, sizeof(SHFILEINFO), Flags);
     ImageList_AddIcon((HIMAGELIST)ImageList1->Handle, fi.hIcon);
   }
   else
     SHGetFileInfo(WFData.cFileName, WFData.dwFileAttributes, &fi, sizeof(SHFILEINFO), Flags);

   return fi.iIcon;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TListItem *pItem;
    WIN32_FIND_DATA  WFData;
    HANDLE hFindFile;
    char Path[] = "C:\\*.*";
    if((hFindFile = FindFirstFile(Path, &WFData))!=INVALID_HANDLE_VALUE)
    do{
       pItem = ListView1->Items->Add();
       pItem->Caption = String(WFData.cFileName);
       String FileName = ExtractFilePath(String(Path)) + String(WFData.cFileName);
       strncpy(WFData.cFileName, FileName.c_str(), MAX_PATH);
       pItem->ImageIndex = AddIcon(WFData);
    } while (FindNextFile(hFindFile, &WFData));
    FindClose(hFindFile);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   ListView1->ViewStyle = vsSmallIcon;
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   ListView1->ViewStyle = vsReport;
}

void __fastcall TForm1::Button5Click(TObject *Sender)
{
   ListView1->ViewStyle = vsList;
}

void __fastcall TForm1::Button4Click(TObject *Sender)
{
   ListView1->ViewStyle = vsIcon;
}


Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Me da el siguiente error Responder citando

Hola de nuevo a todos. A mi me da el siguiente error:

[BCC32 Error] Unit2.cpp(19): E2034 Cannot convert 'char const[1]' to 'const wchar_t *'
Full parser context
Unit2.cpp(13): parsing: _fastcall TForm2::TForm2(TComponent *)

y mira que he buscado en el foro pero no doy con la solución, gracias a quien quiera ayudarme.
Utilizo el Rad studio xe sobre win7 64.
_________________
¡ADELANTE CON ESTE SUPER FORO!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Re: Me da el siguiente error Responder citando

Tugui escribió:
Hola de nuevo a todos. A mi me da el siguiente error:

[BCC32 Error] Unit2.cpp(19): E2034 Cannot convert 'char const[1]' to 'const wchar_t *'
Full parser context
Unit2.cpp(13): parsing: _fastcall TForm2::TForm2(TComponent *)

y mira que he buscado en el foro pero no doy con la solución, gracias a quien quiera ayudarme.
Utilizo el Rad studio xe sobre win7 64.


Cambia strncpy por wcsncpy.

Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Muchas gracias escafandra, pero me sigue dando el mismo error concretamente en esta línea:
Código:
int SystemSmallIcons = SHGetFileInfo("", 0, &fi, sizeof(SHFILEINFO), Flags);

_________________
¡ADELANTE CON ESTE SUPER FORO!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Problemas del Unicode...

Trata de cambiar así:
Código:

int SystemSmallIcons = SHGetFileInfo(L"", 0, &fi, sizeof(SHFILEINFO), Flags);


Si siguen los errores cambia además:
SHGetFileInfo por SHGetFileInfoW
y
SHFILEINFO por SHFILEINFOW

Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Gracias de nuevo ahora me da el siguiente fallo:
Código:
[BCC32 Error] Unit2.cpp(34): E2316 '_fastcall TForm2::AddIcon(_WIN32_FIND_DATAW)' is not a member of 'TForm2'

¿Qué puedo hacer? ¿Tal vez debería definir la función AddIcon en archivo h?
Y luego tras comentar el trozo de código anterior me da el siguiente fallo:
Código:
[BCC32 Error] Unit2.cpp(55): E2034 Cannot convert 'char[7]' to 'const wchar_t *'
  Full parser context
    Unit2.cpp(50): parsing: void _fastcall TForm2::Button1Click(TObject *)

supongo que es del unicode otra vez y me lo da en la siguiente línea:
Código:
if((hFindFile = FindFirstFile(Path, &WFData))!=INVALID_HANDLE_VALUE)

Me podrias seguir ayudando, muchas gracias.
_________________
¡ADELANTE CON ESTE SUPER FORO!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Tugui escribió:
Gracias de nuevo ahora me da el siguiente fallo:
Código:
[BCC32 Error] Unit2.cpp(34): E2316 '_fastcall TForm2::AddIcon(_WIN32_FIND_DATAW)' is not a member of 'TForm2'

¿Qué puedo hacer? ¿Tal vez debería definir la función AddIcon en archivo h?


Si. Se debe a que la definición en la clase del formulario no existe, debes editar el archivo.h y declararla como miembro publico.

Tugui escribió:

Y luego tras comentar el trozo de código anterior me da el siguiente fallo:
Código:
[BCC32 Error] Unit2.cpp(55): E2034 Cannot convert 'char[7]' to 'const wchar_t *'
  Full parser context
    Unit2.cpp(50): parsing: void _fastcall TForm2::Button1Click(TObject *)

supongo que es del unicode otra vez y me lo da en la siguiente línea:
Código:
if((hFindFile = FindFirstFile(Path, &WFData))!=INVALID_HANDLE_VALUE)

Me podrias seguir ayudando, muchas gracias.


Si, cambia las API a su versión W aunque el compilador debería hacerlo por ti.

Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Lo siento no se como se hace todo eso que me contestas. (La ayuda de mi builder no me funciona, no sé porqué).
lo único que se del archivo h es que debo declararlo donde pone public.
Y lo pasar las librerias a la versión w no sé cómo se hace.
_________________
¡ADELANTE CON ESTE SUPER FORO!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Las API que usan cadenas de Texto directamente o en las estructuras pasadas como parámetros, están definidas en versión A(ASCII Normal) y W(Unicode). Las estructuras que usan, también. Añadiendo una A al final del nombre del tipo de la estructura o del nombre de la función, forzamos a usar las versiones No Unicode. Si añadimos una W forzamos el uso de Unicode. Smile

No puedo simular tu situación con exactitud porque uso Builder 5.

Si tengo un rato tranquilo puedo tratar de reescribir el código forzando Unicode. Ya veré...

Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

A este paso vamos a forzar a escafandra a modernizarse Wink quizás en el XE3, Unicode junto con el compilador de 64bits para que compense (ya que el XE2 solo traerá el compilador de 64bits para Delphi por el momento).

Cambiando un par de detalles en el código de escafandra para que compile directamente en las versiones Ansi y Unicode:
(los pequeños cambios estan señalados con <--)
Código:
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
   Button1->Caption = "Dir";
   Button2->Caption = "vsSmallIcon";
   Button3->Caption = "vsReport";
   Button4->Caption = "vsIcon";
   Button5->Caption = "vsList";
   ListView1->SmallImages = ImageList1;
   ListView1->LargeImages = ImageList2;

   DWORD Flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
   SHFILEINFO fi = {0};
   int SystemSmallIcons = SHGetFileInfo(TEXT("") /*<--*/, 0, &fi, sizeof(SHFILEINFO), Flags);
   ImageList1->Handle = SystemSmallIcons;
   Flags = SHGFI_SYSICONINDEX | SHGFI_LARGEICON;
   int SystemLargeIcons = SHGetFileInfo(TEXT("") /*<--*/, 0, &fi, sizeof(SHFILEINFO), Flags);
   ImageList2->Handle = SystemLargeIcons;
}

// Añade un icono a la lista del sistema y devuelve su índice
int __fastcall TForm1::AddIcon(WIN32_FIND_DATA WFData)
{
   SHFILEINFO fi;
   DWORD Flags =  SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES;

   if(WFData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
     SHGetFileInfo(TEXT("") /*<--*/, FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(SHFILEINFO), Flags);
   else if(SHGetFileInfo(WFData.cFileName, FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(SHFILEINFO), SHGFI_EXETYPE)){
     SHGetFileInfo(WFData.cFileName, WFData.dwFileAttributes, &fi, sizeof(SHFILEINFO), Flags);
     ImageList_AddIcon((HIMAGELIST)ImageList1->Handle, fi.hIcon);
   }
   else
     SHGetFileInfo(WFData.cFileName, WFData.dwFileAttributes, &fi, sizeof(SHFILEINFO), Flags);

   return fi.iIcon;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TListItem *pItem;
    WIN32_FIND_DATA  WFData;
    HANDLE hFindFile;
    TCHAR Path[] = TEXT("C:\\*.*");  // <--
    if((hFindFile = FindFirstFile(Path, &WFData))!=INVALID_HANDLE_VALUE)
    do{
       pItem = ListView1->Items->Add();
       pItem->Caption = String(WFData.cFileName);
       String FileName = ExtractFilePath(String(Path)) + String(WFData.cFileName);
       lstrcpyn(WFData.cFileName, FileName.c_str(), MAX_PATH);  // <--
       pItem->ImageIndex = AddIcon(WFData);
    } while (FindNextFile(hFindFile, &WFData));
    FindClose(hFindFile);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   ListView1->ViewStyle = vsSmallIcon;
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   ListView1->ViewStyle = vsReport;
}

void __fastcall TForm1::Button5Click(TObject *Sender)
{
   ListView1->ViewStyle = vsList;
}

void __fastcall TForm1::Button4Click(TObject *Sender)
{
   ListView1->ViewStyle = vsIcon;
}
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

_Leo escribió:
A este paso vamos a forzar a escafandra a modernizarse Wink quizás en el XE3, Unicode junto con el compilador de 64bits para que compense (ya que el XE2 solo traerá el compilador de 64bits para Delphi por el momento).


Es verdad, me fastidian este tipo de cosas y no poder probar el código en las nuevas versiones de Builder...

La verdad es que la verdadera revolución será la compilación a 64 bits.

Tu adaptación de mi código ha quedado muy bien. Very Happy

Saludos.
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Muchas gracias a los dos; Ahora sólo me queda solucionar el problema del AddIcon, me podríais decir como lo declaro en archivo h.
_________________
¡ADELANTE CON ESTE SUPER FORO!
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.