wie kann ich bei einer listview einträge hinzufügen?



  • VorhandeneListe ist ein gültiges Fensterhandle? Wie wird die Listenansicht erstellt?



  • mit

    VorhandeneListe = CreateWindowEx( WS_EX_CLIENTEDGE , WC_LISTVIEW , NULL , WS_VISIBLE | WS_CHILD | LVS_REPORT , 65 ,155 , 400 , 600 , hwnd , NULL , hInstance , NULL );
    


  • Schaut normal aus. Da bleibt Dir nur noch der Debugger.



  • Hier der ganze Code:

    /*++
    
    Copyright (c) 1998-2002  Microsoft Corporation
    All rights reserved.
    
    Module Name:ListView1
    
    --*/
    
    /**************************************************************
    This is the simple Hello program with three application-defined
    functions added. CreateListView creates a list-view control.
    OnGetDispInfo and OnEndLabelEdit handle notification messages.
    CreateListView creates a list view with three columns. The icon
    files used to create the image lists are not included.   
    **************************************************************/
    #include <windows.h> 
    #include <commctrl.h>
    #include <strsafe.h>
    #include "hello.h"
    
    //utility Macro
    #define ARRAYSIZE(x)                (sizeof(x)/sizeof(x[0]))
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    HWND CreateListView (HWND);
    VOID OnGetDispInfo(NMLVDISPINFO *);
    BOOL OnEndLabelEdit(NMLVDISPINFO *);
    
    HINSTANCE hInst;
    HWND hWndListView;
    
    // Used for subitems.
    PETINFO rgPetInfo [ ] = 
    {
    {TEXT("Dog"), TEXT("Poodle"), TEXT("$300.00")},
    {TEXT("Cat"), TEXT("Siamese"), TEXT("$100.00")},
    {TEXT("Fish"), TEXT("Angel Fish"), TEXT("$10.00")},
    };
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
         LPSTR lpszCmdParam, int nCmdShow)
    {
        static TCHAR    szAppName[ ] = TEXT("HelloWin");
        HWND           hwnd;
        MSG            msg;
        WNDCLASS       wndclass;
    
        if (!hPrevInstance)
        {
            wndclass.style          = CS_HREDRAW|CS_VREDRAW;
            wndclass.lpfnWndProc    = WndProc;
            wndclass.cbClsExtra     = 0;
            wndclass.cbWndExtra     = 0;
            wndclass.hInstance      = hInstance;
            wndclass.hIcon          = LoadIcon (NULL,
                                      IDI_APPLICATION);
            wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
            wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
            wndclass.lpszMenuName   = NULL;
            wndclass.lpszClassName  = szAppName;
    
            RegisterClass (&wndclass);
        }
    
        hInst = hInstance;
        hwnd = CreateWindow(szAppName,
                    TEXT("The List-View Program - ICON VIEW"),
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    NULL,
                    NULL,
                    hInstance,
                    NULL);
    
        ShowWindow (hwnd, nCmdShow);
        UpdateWindow (hwnd);
    
        while (GetMessage (&msg, NULL, 0, 0))
        {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
    {
    
    switch (message)
        {
        case WM_CREATE:
                    CreateListView (hwnd);
                    return 0;
    
        case WM_NOTIFY:
                    // Process notification messages.            
                    switch (((LPNMHDR) lParam)->code) 
                    { 
    
                    // Fill subitems.
                    case LVN_GETDISPINFO: 
                        OnGetDispInfo((NMLVDISPINFO *) lParam); 
                        break; 
    
                    // Change labels.
                    case LVN_ENDLABELEDIT: 
                        return OnEndLabelEdit( 
                        (NMLVDISPINFO *) lParam );
                        break;
                    }
                    return 0;
    
                case WM_DESTROY:
                    PostQuitMessage (0);
                    return 0;
        }
    
    return DefWindowProc (hwnd, message, wParam, lParam);
    }
    
    HWND CreateListView (HWND hWndParent)
    {
    RECT rcl;
    HICON hIcon;
    int index;
    HIMAGELIST hSmall, hLarge;
    LV_COLUMN lvC;
    TCHAR szText[50];
    LV_ITEM lvI;
    INITCOMMONCONTROLSEX icex;
    
    // Ensure that the common control DLL is loaded. 
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC  = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex); 
    GetClientRect(hWndParent, &rcl);
    
    // Create a list-view control in icon view.
    hWndListView = CreateWindowEx(0, WC_LISTVIEW, TEXT(""), WS_VISIBLE | WS_CHILD |
                              WS_BORDER 
    		//| LVS_ICON 
    //|LVS_REPORT
    		| LVS_EDITLABELS | WS_EX_CLIENTEDGE,
                              0, 0, rcl.right - rcl.left, rcl.bottom - rcl.top, hWndParent, 
                              (HMENU) ID_LISTVIEW, hInst, NULL);
    
    if(hWndListView == NULL)
      return NULL;
    
    // Create the image lists.
    hSmall = ImageList_Create(16, 16, FALSE, 3, 0);
    hLarge = ImageList_Create(32, 32, FALSE, 3, 0);
    for(index = 500; index < 503; index++)     // Index is based on IDs
    {                                          // of icons. These are
                                               // assigned in the header
                                               // file.
        hIcon = LoadIcon (hInst, MAKEINTRESOURCE(index));
            ImageList_AddIcon(hSmall, hIcon);
        ImageList_AddIcon(hLarge, hIcon);
    }
    
    // Check to be sure that the icons are in the image list.
    if(ImageList_GetImageCount(hSmall) < 3)
       return FALSE;
    if(ImageList_GetImageCount(hLarge) < 3)
       return FALSE;
    
    // Set the image lists.
    //ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL);
    ListView_SetImageList(hWndListView, hLarge, LVSIL_NORMAL);
    
    // Create columns.
    lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvC.fmt = LVCFMT_LEFT;
    lvC.cx = 75;
    lvC.pszText = szText;
    
    // Load the column labels from the resource file.
    for (index = 0; index < 3; index++)
    {
        lvC.iSubItem = index;
        LoadString(hInst, IDS_PETS + index, szText, ARRAYSIZE(szText));
        if(ListView_InsertColumn(hWndListView, index, &lvC) == -1)
            return NULL;
    }
    
    // Insert items.
    lvI.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
    lvI.state = 0;
    lvI.stateMask = 0;
    
    for (index = 0; index < 3; index++)
    {
        lvI.iItem = index;
        lvI.iImage = index;
        lvI.iSubItem = 0;
        lvI.pszText = LPSTR_TEXTCALLBACK;
    
    if(ListView_InsertItem(hWndListView, &lvI) == -1)
    return NULL;
    }
    
    return (hWndListView);
    }
    
    // OnGetDispInfo - processes the LVN_GETDISPINFO 
    // notification message. 
    
     VOID OnGetDispInfo(NMLVDISPINFO *plvdi) 
     {
      switch (plvdi->item.iSubItem)
        {
    
        case 0:
          plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szKind;
          break;
    
        case 1:
          plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szBreed;
          break;
    
        case 2:
          plvdi->item.pszText = rgPetInfo[plvdi->item.iItem].szPrice;
          break;
    
        default:
          break;
        }
     } 
    
     // OnEndLabelEdit - processes the LVN_ENDLABELEDIT 
     // notification message. 
     // Returns TRUE if the label is changed, or FALSE otherwise. 
    
     BOOL OnEndLabelEdit(NMLVDISPINFO *plvdi) 
     { 
        if (plvdi->item.pszText == NULL) 
            return FALSE; 
    
        // Copy the new label text to the application-defined structure.
        StringCchCopyN(rgPetInfo[plvdi->item.iItem].szKind, 
                ARRAYSIZE(rgPetInfo[plvdi->item.iItem].szKind),
                plvdi->item.pszText,
                plvdi->item.cchTextMax);
    
        return TRUE;
    
        // To make a more robust application you should send an EM_LIMITTEXT
        // message to the edit control to prevent the user from entering too
        // many characters in the field. 
    
     }
    

    HEADER:

    /*++
    
    Copyright (c) 1998-2002  Microsoft Corporation
    All rights reserved.
    
    Module Name:
    
    Abstract:
    
        Sample header
    
    --*/
    
    #define IDS_PETS 10
    #define IDS_BREED 11
    #define IDS_PRICE 12
    
    #define ID_LISTVIEW 400
    
    #define DOG_ICO   500
    
    #define CAT_ICO   501
    
    #define FISH_ICO  502
    
    #define IDS_COL1  600
    #define IDS_COL2  601
    #define IDS_COL3  602
    
    typedef struct tagPETINFO
    {
        TCHAR szKind[10];
        TCHAR szBreed[50];
        TCHAR szPrice[20];
    } PETINFO;
    

    brauchst noch eine Resource mit den ICONS
    😉



  • ok danke,
    aber ich krieg diesen fehler beim kompilieren:

    main.obj||error LNK2019: unresolved external symbol "long __stdcall WindowProcedure(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProcedure@@YGJPAUHWND__@@IIJ@Z) referenced in function _WinMain@16|
    


  • Das bedeutet, dass du irgendwo WindowProcedure() aufrufst, obwohl die Funktion gar nicht definiert ist.



  • ok danke fehler weg, aber bei mir werden nur die einträge ohnne die icons angezeigt... wenn ich den codeblock mit den icons hinzufüge dann ist gar kein eintrag mehr da und wenn ich das mit den icons weglasse dann geht alles wie gewohnt

    hier mal mein code für die icons

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC  = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);
    GetClientRect(hWndParent, &rcl);
    
    if(VorhandeneCharakter == NULL)
      return NULL;
    
    // Create the image lists.
    hSmall = ImageList_Create(16, 16, FALSE, 3, 0);
    hLarge = ImageList_Create(32, 32, FALSE, 3, 0);
    for(index = 500; index < 503; index++)     // Index is based on IDs
    {                                          // of icons. These are
                                               // assigned in the header
                                               // file.
        hIcon = LoadIcon (hInstance, "media/icon.bmp");
            ImageList_AddIcon(hSmall, hIcon);
        ImageList_AddIcon(hLarge, hIcon);
    }
    
    // Check to be sure that the icons are in the image list.
    if(ImageList_GetImageCount(hSmall) < 3)
       return FALSE;
    if(ImageList_GetImageCount(hLarge) < 3)
       return FALSE;
    
    // Set the image lists.
    //ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL);
    ListView_SetImageList(VorhandeneCharakter, hLarge, LVSIL_NORMAL);
    


  • ok hier mal ein funktionierednder Code, was an deinem falsch ist weiss ich net aber der hier geht!

    void CreateImagelist_LV(HWND hWnd, HINSTANCE hInstance){
    
    	HIMAGELIST		hImageList;
    	HICON			hIcon;
    	int			icon_anzahl;
    
    	hImageList = ImageList_Create(16, 16, ILC_COLOR16 | ILC_MASK, 3, 0);
    
    	hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICN_00));		//No.0
    	ImageList_AddIcon(hImageList, hIcon);
    	DestroyIcon(hIcon);
    
    	hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICN_01));		//No.1
    	ImageList_AddIcon(hImageList, hIcon);
    	DestroyIcon(hIcon);
    
    	hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICN_02));		//No.2
    	ImageList_AddIcon(hImageList, hIcon);
    	DestroyIcon(hIcon);
    
    	ListView_SetImageList(hWnd, hImageList, LVSIL_SMALL);
    }
    

    Vieleicht klappt dein Code nicht weil du ein Bitmap lädst. Versuchs halt mal mit Icons, sagt ja auch schon die Funktion "LoadIcon", net war?



  • ok danke, aber jetzt wir statt dem icon aus meinem ordner ein weißer fleck angezeigt... code ist der gleiche nur statt MAKEINTRESOURCE(ICN_00) hab ich "ordner/icon.ico" eingefügt



  • LoadIcon lädt Icons aus Moduldateien (.exe, .dll), aber nicht aus Icondateien. In diesem Fall wäre LoadImage die richtige Wahl:

    Doku: http://msdn.microsoft.com/en-us/library/ms648045.aspx

    Alternativ könntest Du die Icons auch als Ressource einbinden, dann geht LoadIcon auch wieder.



  • und wie muss ich das dann umschreiben *dummfrag*



  • oder kannst du mir sagen wie ich in codeblocks eine resource datei machen kann, damit ich beim code nich viel ändern muss


Anmelden zum Antworten