elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  ayuda con programa en c y winapi que lista los directorios en un listview
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: ayuda con programa en c y winapi que lista los directorios en un listview  (Leído 3,155 veces)
suchil

Desconectado Desconectado

Mensajes: 1


Ver Perfil
ayuda con programa en c y winapi que lista los directorios en un listview
« en: 4 Julio 2011, 01:34 am »

Primeramente decirles que soy novato en esto de la programacion con las apis de windows y tambien en c ;)
el detalle es que el programa se supone que deberia de listar los nombres y los iconos de los directorios que se encuentran en c:\ y si muestra los nombres pero el problema es con los iconos que solo muestra un mismo icono para todos
el codigo es el siguiente y lo estoy haciendo en vc++ 6.0

Código:
#include  <windows.h>
#include  <stdio.h>
#include  <commctrl.h>

#pragma comment(lib, "comctl32.lib")

#define  ID_LISTVIEW    1

char directory[500]="C:\\";


typedef struct
{
   char  szItem[100];
}Item;

BOOL AdjustListView(HWND hList, LV_ITEM *lv, int iItems)
{
   int i = iItems;
  // int iRet;
   ListView_DeleteAllItems(hList);

   lv->mask = LVIF_TEXT;
   lv->iSubItem = 0;
   lv->pszText = LPSTR_TEXTCALLBACK;
   while(i > 0)
   {
       ListView_InsertItem(hList, lv);
      i--;
   }
   return TRUE;
}

BOOL WINAPI InitListViewImageLists(HWND hwndLV, char nFile[500])
{
//    HIMAGELIST himlLarge;   // image list for icon view
    HIMAGELIST himlSmall;   // image list for other views
    SHFILEINFO nFileInfo;
    // Create the full-sized and small icon image lists.
    //himlLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
      //  GetSystemMetrics(SM_CYICON), TRUE, 1, 1);
    himlSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
        GetSystemMetrics(SM_CYSMICON), TRUE, 1, 1);


    // Add an icon to each image list.
    SHGetFileInfo(nFile, FILE_ATTRIBUTE_NORMAL, &nFileInfo, sizeof(nFileInfo), SHGFI_ICON | SHGFI_SMALLICON);
//ImageList_AddIcon(himlLarge, nFileInfo.hIcon);
    ImageList_AddIcon(himlSmall, nFileInfo.hIcon);
   
 
    // Assign the image lists to the list view control.
    //ListView_SetImageList(hwndLV, himlLarge, LVSIL_NORMAL);

    ListView_SetImageList(hwndLV, himlSmall, LVSIL_SMALL);
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   static   CREATESTRUCT   *cs;
   static   HWND           hWndListView;
   static   HFONT          hFont;
   static   LV_ITEM        lv;
   static   Item           listitemm;
   static   Item*          ListItem;// = (Item* )LocalAlloc(LPTR, sizeof(Item));
   int i;
   HANDLE                  hFile;
   LV_COLUMN               lvC;
   RECT                    rect;
   char path[500];
   char temp[500];
   strcpy(path,directory);
   strcat(path,"*.*");
   WIN32_FIND_DATA data;
   
   LV_DISPINFO             *lvd;
   NMHDR                   *hdr;

   ListItem = &listitemm;

   switch (iMsg)
   {
   case WM_CREATE :
      cs = (CREATESTRUCT *)lParam;
      hWndListView = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW,
         "", WS_VISIBLE | WS_CHILD | LVS_SHOWSELALWAYS | LVS_LIST | LVS_ICON ,
         0, 0, 0, 0, hwnd,(HMENU)ID_LISTVIEW, cs->hInstance, NULL);

      ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT);


      lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
      lvC.fmt = LVCFMT_LEFT;
 
   hFile = FindFirstFile(path, &data);
  strcpy(ListItem->szItem, data.cFileName);
  strcpy(temp, directory);
  strcat(temp,data.cFileName);
  //InitListViewImageLists(hWndListView, temp);
//AdjustListView(hWndListView, &lv, 1);
  lvC.iSubItem = 0;
         lvC.cx = 100;
i=1;
      while(FindNextFile(hFile,&data)!=0)
  {
  strcpy(temp, directory);
  strcat(temp,data.cFileName);
  strcpy((ListItem+i)->szItem, data.cFileName);
      i++;
  InitListViewImageLists(hWndListView, temp);
  AdjustListView(hWndListView, &lv, i);
      }
FindClose(hFile);     
   
  break;

   case WM_SIZE :
      GetClientRect(hwnd, &rect);
      MoveWindow(hWndListView, 3, 3, rect.right - 6, rect.bottom - 6, 1);
      break;

   case WM_NOTIFY :
      switch(((LPNMHDR)lParam)->code)
      {
      case NM_DBLCLK :
         hdr = (NMHDR FAR*)lParam;
         if(hdr->hwndFrom == hWndListView)
         {
//            index = ListView_GetNextItem(hWndListView,-1,LVNI_SELECTED);
           /* if(index != -1)
            {
               MessageBox(hwnd, ListItem[index].szItem, "Doubleclicked on this item", MB_OK);
            }*/
         }
         break;

      case LVN_GETDISPINFO :
         lvd = (LV_DISPINFO FAR*)lParam;
         if((((LPNMHDR)lParam)->hwndFrom == hWndListView))
         {
           
if(lvd->item.iSubItem==0)
            {
               lvd->item.pszText = ListItem[lvd->item.iItem].szItem;
            }
             
            break;
         }
      }
      break;

   case WM_CLOSE :
      DestroyWindow(hwnd);
      break;

   case WM_DESTROY :
      DeleteObject(hFont);
      PostQuitMessage(0);
      break;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   HWND                    hwnd;
   MSG                     msg;
   WNDCLASS                wndclass;
   char                    szAppName[] = "ListviewApp";
   INITCOMMONCONTROLSEX    icex;

   wndclass.style         = 0;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(hInstance, szAppName);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   RegisterClass(&wndclass);

   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
   icex.dwICC = ICC_LISTVIEW_CLASSES;
   InitCommonControlsEx(&icex);

   hwnd = CreateWindow(szAppName, szAppName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 600, 250,
      NULL, NULL, hInstance, szCmdLine);

   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (int)msg.wParam;
}



les agradeceria cualquier sugerencia y muchas gracias a todos


En línea

escabe

Desconectado Desconectado

Mensajes: 34



Ver Perfil
Re: ayuda con programa en c y winapi que lista los directorios en un listview
« Respuesta #1 en: 7 Agosto 2011, 16:23 pm »

Siento responder tarde, estuve de vacaciones...
Te muestro alguna modificación:

Código:
#include <windows.h>

#include  <stdio.h>
#include  <commctrl.h>

#pragma comment(lib, "comctl32.lib")

#define  ID_LISTVIEW    1

char directory[MAX_PATH]="C:\\";


typedef struct
{
   char  szItem[MAX_PATH];
}Item;

int 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;
}

BOOL WINAPI InitListViewImageLists(HWND hwndLV)
{
   DWORD Flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
   SHFILEINFO fi = {0};
   int SystemSmallIcons = SHGetFileInfo("", 0, &fi, sizeof(SHFILEINFO), Flags);
   ListView_SetImageList(hwndLV, SystemSmallIcons, LVSIL_SMALL);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   static   CREATESTRUCT   *cs;
   static   HWND           hWndListView;
   static   HFONT          hFont;
   static   LV_ITEM        lv;
   static   Item           listitemm;
   static   Item*          ListItem;// = (Item* )LocalAlloc(LPTR, sizeof(Item));
   int i;
   HANDLE                  hFile;
   LV_COLUMN               lvC;
   RECT                    rect;
   char path[MAX_PATH];
   char temp[MAX_PATH];
   strcpy(path,directory);
   strcat(path, "*.*");
   WIN32_FIND_DATA data;

   LV_DISPINFO             *lvd;
   NMHDR                   *hdr;

   ListItem = &listitemm;

   switch (iMsg){
     case WM_CREATE :
       cs = (CREATESTRUCT *)lParam;
       hWndListView = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW,
          "", WS_VISIBLE | WS_CHILD | LVS_LIST,
          0, 0, 100, 100, hwnd,(HMENU)ID_LISTVIEW, cs->hInstance, NULL);

//       ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT);
       lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
       lvC.fmt = LVCFMT_LEFT;
       hFile = FindFirstFile(path, &data);
    strcpy(ListItem->szItem, data.cFileName);
     strcpy(temp, directory);
     strcat(temp,data.cFileName);
       lvC.iSubItem = 0;
       lvC.cx = 100;
   i=1;
     InitListViewImageLists(hWndListView);
       while(FindNextFile(hFile,&data)!=0){
         strcpy(temp, directory);
       strcat(temp, data.cFileName);
         strcpy(data.cFileName, temp);
         char *FileName = strrchr(data.cFileName, '\\');
         lv.mask = LVIF_TEXT | LVIF_IMAGE;
         lv.iSubItem = 0;
         lv.pszText = FileName+1;
         lv.iImage = AddIcon(data);
         ListView_InsertItem(hWndListView, &lv);
         i++;
      }
      FindClose(hFile);
  break;

   case WM_SIZE :
      GetClientRect(hwnd, &rect);
      MoveWindow(hWndListView, 3, 3, rect.right - 6, rect.bottom - 6, 1);
      break;

   case WM_NOTIFY :
      switch(((LPNMHDR)lParam)->code)
      {
      case NM_DBLCLK :
         hdr = (NMHDR FAR*)lParam;
         if(hdr->hwndFrom == hWndListView)
         {
//            index = ListView_GetNextItem(hWndListView,-1,LVNI_SELECTED);
           /* if(index != -1)
            {
               MessageBox(hwnd, ListItem[index].szItem, "Doubleclicked on this item", MB_OK);
            }*/
         }
         break;

      case LVN_GETDISPINFO :
         lvd = (LV_DISPINFO FAR*)lParam;
         if((((LPNMHDR)lParam)->hwndFrom == hWndListView))
         {
           
if(lvd->item.iSubItem==0)
            {
               lvd->item.pszText = ListItem[lvd->item.iItem].szItem;
            }
             
            break;
         }
      }
      break;

   case WM_CLOSE :
      DestroyWindow(hwnd);
      break;

   case WM_DESTROY :
      DeleteObject(hFont);
      PostQuitMessage(0);
      break;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
   HWND                    hwnd;
   MSG                     msg;
   WNDCLASS                wndclass;
   char                    szAppName[] = "ListviewApp";
   INITCOMMONCONTROLSEX    icex;

   wndclass.style         = 0;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(hInstance, szAppName);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   RegisterClass(&wndclass);

   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
   icex.dwICC = ICC_LISTVIEW_CLASSES;
   InitCommonControlsEx(&icex);

   hwnd = CreateWindow(szAppName, szAppName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 600, 250,
      NULL, NULL, hInstance, szCmdLine);

   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (int)msg.wParam;
}

Espero que te ayude en algo.

Saludos.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines