[C con Clase] ayuda con programa c y winapi que muestra archivos y documentos en un listview

jesus gutierrez enigma_profunda en hotmail.com
Lun Jul 4 01:42:50 CEST 2011


les pido su ayuda con este programa que estoy realizando ya que teoricamente el programa deberia de listar las carpetas y los archivos en un control listview, los nombres de los archivos y de las carpetas si se listan en forma correcta, el problema esta con los iconos, los cuales se muestra un solo icono para todos los archivos en este caso el icono del ultimo archivo de c:
esta realizado en vc++ y el codigo es el siguiente:

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



disculpen cualquier molestia y aclaro que soy novato en esto de la programacion con apis y tambien en c ;) muchas gracias a todos de antemano
 		 	   		  
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20110703/466b6bd2/attachment.html>


Más información sobre la lista de distribución Cconclase