Autor
|
Tema: ayuda con programa en c y winapi que lista los directorios en un listview (Leído 3,174 veces)
|
suchil
Desconectado
Mensajes: 1
|
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 #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
Mensajes: 34
|
Siento responder tarde, estuve de vacaciones... Te muestro alguna modificación: #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
|
|
|
|
|
Mensajes similares |
|
Asunto |
Iniciado por |
Respuestas |
Vistas |
Último mensaje |
|
|
Ayuda con Listview (evitar duplicados en lista de facturación) plis!!!
Programación Visual Basic
|
GhostLT
|
3
|
6,971
|
29 Septiembre 2009, 05:40 am
por GhostLT
|
|
|
Equivalencia de función WINAPI unsigned int WINAPI myRun en Windows con Ubuntu
Programación C/C++
|
bertamax
|
2
|
2,860
|
7 Agosto 2010, 16:27 pm
por bertamax
|
|
|
[pregunta]copiar y reescribir directorios de forma recursiva winapi
Programación C/C++
|
daryo
|
0
|
1,452
|
15 Julio 2015, 21:13 pm
por daryo
|
|
|
nodejs | obtener lista de archivos y directorios
Desarrollo Web
|
patilanz
|
1
|
4,346
|
9 Diciembre 2017, 21:22 pm
por patilanz
|
|
|
Necesito una Lista de HexTypes | WinAPI
.NET (C#, VB.NET, ASP)
|
**Aincrad**
|
8
|
3,532
|
25 Marzo 2019, 23:59 pm
por ThunderCls
|
|