• Welcome to PlanetSquires Forums.
 

Listview Demo help

Started by Frank Bruebach, March 18, 2024, 08:56:53 AM

Previous topic - Next topic

Frank Bruebach

Hello all

Need a little Help to fix this Listview example. Help is Welcome

Thanks in advance, Frank


' freebasic cWindow listview demo test
'
#define UNICODE
#define _WIN32_WINNT &h0602
#INCLUDE ONCE "Afx/CWindow.inc"
#INCLUDE ONCE "Afx/AfxGdiPlus.inc"
#INCLUDE ONCE "Afx/AfxMenu.inc"
USING Afx
' // Menu identifiers
ENUM
    IDM_UNDO = 5000   ' Undo
    IDM_REDO          ' Redo
    IDM_HOME          ' Home
    IDM_SAVE          ' Save
    IDM_EXIT          ' Exit
END ENUM
' Constants for controls
ENUM
    IDC_LISTVIEW = 1000
END ENUM

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG
   END WinMain(GetModuleHandleW(""), NULL, COMMAND(), SW_NORMAL)
' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
' ========================================================================================
' Main window callback procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
   DIM pNmh  AS NMHDR PTR            ' // Pointer to a NMHDR structure
   DIM pLvNm AS NMLISTVIEW PTR       ' // Pointer to a NMLISTVIEW structure
   DIM pLvCd AS NMLVCUSTOMDRAW PTR   ' // Pointer to a NMLVCUSTOMDRAW structure
     SELECT CASE uMsg
         CASE WM_CREATE
         EXIT FUNCTION
         CASE WM_COMMAND
             IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                 SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
                     CASE IDCANCEL
                         ' // If ESC key pressed, close the application sending an WM_CLOSE message
                         SendMessageW hwnd, WM_CLOSE, 0, 0
                         EXIT FUNCTION                       
                     CASE IDM_UNDO
                         MessageBox hwnd, "Undo option clicked", "Menu", MB_OK
                         EXIT FUNCTION
                     CASE IDM_REDO
                         MessageBox hwnd, "Redo option clicked", "Menu", MB_OK
                         EXIT FUNCTION
                     CASE IDM_HOME
                         MessageBox hwnd, "Home option clicked", "Menu", MB_OK
                         EXIT FUNCTION
                     CASE IDM_SAVE
                         MessageBox hwnd, "Save option clicked", "Menu", MB_OK
                         EXIT FUNCTION
                     CASE IDM_EXIT
                         SendMessageW hwnd, WM_CLOSE, 0, 0
                         EXIT FUNCTION     
                 END SELECT
             END IF
         CASE WM_SIZE
         ' // Resize the ListView control and its header
         IF wParam <> SIZE_MINIMIZED THEN
            DIM hListView AS HWND, pWindow AS CWindow PTR
            pWindow = CAST(CWindow PTR, GetPropW(hwnd, "CWINDOWPTR"))
            hListView = GetDlgItem(hwnd, IDC_LISTVIEW)
            '----------------- problem zone ---------------------------- //
            ' pWindow->MoveWindow hListView, 5, 5, pWindow->ClientWidth - 10, pWindow->ClientHeight - 10, TRUE
            '----------------- problem zone ---------------------------- //
         END IF
        CASE WM_DESTROY
         ' // End the application by sending an WM_QUIT message
         PostQuitMessage(0)
         EXIT FUNCTION
    END SELECT
    ' // Default processing of Windows messages
    FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG
   ' // Set process DPI aware
   AfxSetProcessDPIAware
  ' Initialize common controls
      InitCommonControls()
   DIM pWindow AS CWindow
   pWindow.Create(NULL, " Demo ListView WinFBE", @WndProc)
   pWindow.ClassStyle = CS_DBLCLKS   ' // Change the window style to avoid flicker
   pWindow.SetClientSize(565, 420)
   pWindow.Center

' // Add a button -> but I needed here some Radiobuttons too ;)
   pWindow.AddControl("Button", , IDCANCEL, "&Close", 280, 280, 75, 23)
   
   ' // Adds a listview
   DIM hListView AS HWND
   hListView = pWindow.AddControl("ListView", pWindow.hWindow, IDC_LISTVIEW, "") ' correct ?

   ' // Add some extended styles
   DIM dwExStyle AS DWORD
   dwExStyle = ListView_GetExtendedListViewStyle(hListView)
   dwExStyle = dwExStyle OR LVS_EX_FULLROWSELECT OR LVS_EX_GRIDLINES
   ListView_SetExtendedListViewStyle(hListView, dwExStyle)

   ' // Add the header's column names
   DIM i AS LONG, lvc AS LVCOLUMNW, wszText AS WSTRING * 260
   lvc.mask = LVCF_FMT OR LVCF_WIDTH OR LVCF_TEXT OR LVCF_SUBITEM
   FOR i = 0 TO 4
      wszText = "Column " & STR(i)
      lvc.pszText = @wszText
      lvc.cx = pWindow.ScaleX(110)
      lvc.iSubItem = i
      SendMessageW(hListView, LVM_INSERTCOLUMNW, i, CAST(LPARAM, @lvc))
   NEXT

   ' // Populate the ListView with some data
   DIM x AS LONG
   DIM lvi AS LVITEMW
   lvi.mask = LVIF_TEXT
   FOR i = 0 to 29
      lvi.iItem = i
      lvi.iSubItem = 0
      wszText = "Column 0 Row" + STR(i)
      lvi.pszText = @wszText
      ListView_InsertItem(hListView, @lvi)
      FOR x = 1 TO 4
         lvi.iSubItem = x
         wszText = "Column " & STR(x) & " Row" + STR(i)
         lvi.pszText = @wszText
         SendMessageW hListView, LVM_SETITEMTEXTW, i, CAST(LPARAM, @lvi)
      NEXT
   NEXT
   ' // Select the fist item
   ListView_SetItemState(hListView, 0, LVIS_FOCUSED OR LVIS_SELECTED, &H000F)
   ' // Set the focus in the ListView
   SetFocus hListView
   FUNCTION = pWindow.DoEvents(nCmdShow)
END FUNCTION
' ========================================================================================

PS where U can find Infos about Control wrapper functions for Afx?

Paul Squires

How about this....

            '----------------- problem zone ---------------------------- //
            pWindow = AfxCWindowPtr(hwnd)
             if pWindow then
                pWindow->MoveWindow hListView, 5, 5, pWindow->ClientWidth - 10, pWindow->ClientHeight - 10, TRUE
             end if  
            '----------------- problem zone ---------------------------- //

QuotePS where U can find Infos about Control wrapper functions for Afx?

Check out all of the *.inc files in the \inc\Afx folder. Specifically, for ListView, look at AfxCtl.inc.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

#2
Hi Frank,

Please use updated code, not code posted in the forum 8 or 9 years ago when I was writing the Afx framework and doing changes everyday.

If you had used the template available from the WinFBE editor, you wouldn't have had that problem.

This is the updated ListView example available from WinFBE:

' ########################################################################################
' Microsoft Windows
' File: CW_COMMCTRL_ListView.fbtpl
' Contents: CWindow with a ListView
' Compiler: Free Basic
' Copyright (c) 2016 José Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

#define IDC_LISTVIEW 1001

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
   AfxSetProcessDPIAware

   ' // Create the main window
   DIM pWindow AS CWindow
   pWindow.Create(NULL, "CWindow with a ListView", @WndProc)
   pWindow.ClassStyle = CS_DBLCLKS   ' // Change the window style to avoid flicker
   pWindow.SetClientSize(565, 320)
   pWindow.Center

   ' // Adds a listview
   DIM hListView AS HWND
   hListView = pWindow.AddControl("ListView", , IDC_LISTVIEW)

   ' // Add some extended styles
   DIM dwExStyle AS DWORD
   dwExStyle = ListView_GetExtendedListViewStyle(hListView)
   dwExStyle = dwExStyle OR LVS_EX_FULLROWSELECT OR LVS_EX_GRIDLINES
   ListView_SetExtendedListViewStyle(hListView, dwExStyle)

   ' // Add the header's column names
   DIM lvc AS LVCOLUMNW, wszText AS WSTRING * 260
   lvc.mask = LVCF_FMT OR LVCF_WIDTH OR LVCF_TEXT OR LVCF_SUBITEM
   FOR i AS LONG = 0 TO 4
      wszText = "Column " & STR(i)
      lvc.pszText = @wszText
      lvc.cx = pWindow.ScaleX(110)
      lvc.iSubItem = i
      ListView_InsertColumn(hListView, i, @lvc)
   NEXT

   ' // Populate the ListView with some data
   DIM lvi AS LVITEMW
   lvi.mask = LVIF_TEXT
   FOR i AS LONG = 0 to 29
      lvi.iItem = i
      lvi.iSubItem = 0
      wszText = "Column 0 Row" + WSTR(i)
      lvi.pszText = @wszText
      ListView_InsertItem(hListView, @lvi)
      FOR x AS LONG = 1 TO 4
         wszText = "Column " & WSTR(x) & " Row" + WSTR(i)
         ListView_SetItemText(hListView, i, x, @wszText)
      NEXT
   NEXT

   ' // Select the fist item (ListView items are zero based)
   ListView_SelectItem(hListView, 0)
   ' // Set the focus in the ListView
   SetFocus hListView

   

   ' // Dispatch Windows messages
   FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main window callback procedure
' ================================================================e========================
FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

   SELECT CASE uMsg

      CASE WM_COMMAND
         SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
            ' // If ESC key pressed, close the application sending an WM_CLOSE message
            CASE IDCANCEL
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE WM_SIZE
         ' // Resize the ListView control and its header
         IF wParam <> SIZE_MINIMIZED THEN
            ' // Retrieve the handle of the ListView control
            DIM hListView AS HWND = GetDlgItem(hwnd, IDC_LISTVIEW)
            ' // Retrieve a pointer to the CWindow class
            DIM pWindow AS CWindow PTR = AfxCWindowPtr(hwnd)
            ' // Move the ListView control
            IF pWindow THEN pWindow->MoveWindow hListView, 5, 5, pWindow->ClientWidth - 10, pWindow->ClientHeight - 10, CTRUE
         END IF

    CASE WM_DESTROY
          ' // End the application
         PostQuitMessage(0)
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to DefWindowProc
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Frank Bruebach

#3
Hi jose...

I would have liked to do that too, but I couldn't find a current Listview example (using Afx) other than the treeview example in the examples sample projects of WinFBE Editor

I have downloaded WinFBE suite and your winfbx master files

Thanks for your Update

Regards Frank

José Roca

In the examples folder are the examples that use resources. The templates are available directly from the editor clicking the button with an arrow in the top ledt corner.


Frank Bruebach

#5
Thank you for the Tip good to know that!

I have checked a Lot of example already very good Work Jose and Paul..

Only the "notepad.bas" (visual Designer Projects) its Not running...

AfxRichEdit.inc  Line 1166 Here Error passing different Pointer types...

Thx, Frank