Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005
    Posts
    203

    Unhappy Unanswered: Help using LVM_REDRAWITEMS

    could any one show me how i can send an external listview a refresh using LVM_REDRAWITEMS .Thanks

    I want to send refresh after setting the color


    visual basic code


    Code:
     lItemPos = 0& ' first item
        SendMessage hwnd, LVM_SETITEMSTATE, lItemPos, ByVal lxprocLVITEM
               
              'SendMessage hwnd, LVM_SETTEXTBKCOLOR, 0, hexdec(16711935)
               'SendMessage hwnd, LVM_SETTEXTBKCOLOR, 0, 16711935
               'SendMessage hwnd, LVM_SETTEXTBKCOLOR, 0&, 0&
              SendMessage hwnd, LVM_SETTEXTBKCOLOR, 0, 16711935
    
         => here i want to send a refresh to listview

  2. #2
    Join Date
    Jul 2006
    Posts
    8

    Are You For Real ?

    WHOW ! ... SERIOUSLY .. ARE YOU FOR REAL ?


    Code:
    VERSION 5.00
    Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   3645
       ClientLeft      =   60
       ClientTop       =   450
       ClientWidth     =   4620
       LinkTopic       =   "Form1"
       ScaleHeight     =   3645
       ScaleWidth      =   4620
       StartUpPosition =   3  'Windows Default
       Begin VB.CommandButton Command1 
          Caption         =   "Command1"
          Height          =   495
          Left            =   3240
          TabIndex        =   1
          Top             =   3000
          Width           =   1215
       End
       Begin MSComctlLib.ListView ListView1 
          Height          =   2775
          Left            =   120
          TabIndex        =   0
          Top             =   120
          Width           =   4335
          _ExtentX        =   7646
          _ExtentY        =   4895
          LabelWrap       =   -1  'True
          HideSelection   =   -1  'True
          _Version        =   393217
          ForeColor       =   -2147483640
          BackColor       =   -2147483643
          BorderStyle     =   1
          Appearance      =   1
          NumItems        =   0
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    ' *
    ' * I DID NOT UNDERSTOOD YOU VERY WELL
    ' *
    ' * YOUR ARE DEALING WITH A MEDIUM LEVEL CONCEPT OF WINAPI DEVELOPMENT
    ' * BUT YOU ASK FOR A SIMPLE ERROR IN MY PREVIOUS POST
    ' * ARE YOU AN BEGINNER, MEDIUM OR EXPERIENCED C++ OR VB PROGRAMMER ?
    ' *
    ' * ARE YOU ASKING FOR A GUIDE ?
    ' * OR YOU ARE ONLY CONCERNED TO RUN THIS ?
    ' * I WILL NO EXTEND IN MY COMMENTS OF THIS APPLICATION.
    ' * DO YOU UNDERSTAND THIS CODE ?, OR DONT CARE TO UNDERSTAND THIS ?, THAT IS OK FOR ME TOO.
    ' *
    
    Option Explicit
    
    Private Type LV_ITEM
        mask As Long
        iItem As Long
        iSubItem As Long
        state As Long
        stateMask As Long
        pszText As Long
        cchTextMax As Long
        iImage As Long
        lParam As Long
        iIndent As Long
        iGroupId As Long
        cColumns As Long
        puColumns As Long
    End Type
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
    Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    Private Declare Function IsBadWritePtr Lib "kernel32" (lp As Any, ByVal ucb As Long) As Long
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hwnd1 As Long, ByVal hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long
    
    Private Const PROCESS_VM_OPERATION = &H8&
    Private Const PROCESS_VM_READ = &H10&
    Private Const PROCESS_VM_WRITE = &H20&
    
    Private Const LVIF_STATE = &H8&
    Private Const LVIS_FOCUSED = &H1&
    Private Const LVIS_SELECTED = &H2&
    
    Private Const MEM_COMMIT = &H1000&
    Private Const MEM_RESERVE = &H2000&
    Private Const MEM_RELEASE = &H8000&
    
    Private Const PAGE_READWRITE = &H4&
    
    Private Const LVM_FIRST = &H1000&
    Private Const LVM_REDRAWITEMS = LVM_FIRST + 21
    Private Const LVM_SETTEXTBKCOLOR = LVM_FIRST + 38
    Private Const LVM_SETITEMSTATE = LVM_FIRST + 43
    
    Private Sub Command1_Click()
        Dim lThreadID As Long
        Dim lProcID As Long
        Dim hProc As Long
        Dim lxprocLVITEM As Long
    
        Dim hwnd1 As Long
        
        Dim LVITEM As LV_ITEM
        LVITEM.mask = LVIF_STATE
        LVITEM.state = LVIS_SELECTED Or LVIS_FOCUSED
        LVITEM.stateMask = LVIS_SELECTED Or LVIS_FOCUSED
        
        ' HERE IS WHERE I GET THE HWND ...
        ' PLEASE SEE THE FUNCTIONS AT THE END OF THIS CODE
        ' *STILL* DONT KNOW HOW DID YOU GET *YOUR* HWND.
        ' THAT *WAS* VERY IMPORTANT TO DEBUG THE APPLICATION
        ' TO KNOW WHICH KIND OF Window Handle YOUR WERE TAKING ABOUT
        ' I PROVIDED 3 ALTERNATIVES
        hwnd1 = MyFindWindow1()
        
        If hwnd1 <> 0 Then
        
            ' YOU ALREADY KNOW THIS CODE
            ' I JUST ADDED SOME ASSERTIONS
            lThreadID = GetWindowThreadProcessId(hwnd1, lProcID)
            If lThreadID <> 0 And lProcID <> 0 Then
                hProc = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, lProcID)
                If hProc <> 0 Then
                    lxprocLVITEM = VirtualAllocEx(hProc, ByVal 0, LenB(LVITEM), MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
                    If lxprocLVITEM <> 0 Then
                        If IsBadWritePtr(lxprocLVITEM, LenB(LVITEM)) = 0 Then
                            Dim lpWritten As Long
                            WriteProcessMemory hProc, ByVal lxprocLVITEM, LVITEM, LenB(LVITEM), lpWritten
                            If lpWritten = LenB(LVITEM) Then
                                Dim lItem As Long
                                lItem = 2&
                                Call SendMessage(hwnd1, LVM_SETITEMSTATE, ByVal lItem, ByVal lxprocLVITEM)
                            End If
                        End If
                        VirtualFreeEx hProc, ByVal lxprocLVITEM, LenB(LVITEM), MEM_RELEASE
                    End If
                    CloseHandle hProc
                End If
            End If
            
            ' THIS IS THE LAST PART
            Call SendMessage(hwnd1, LVM_SETTEXTBKCOLOR, 0, ByVal &HFF&)
            Call SendMessage(hwnd1, LVM_REDRAWITEMS, 0, ByVal 0&)
            UpdateWindow hwnd1
        End If
    
    End Sub
    
    Private Sub Form_Load()
        ' THIS IS WHERE YOU GET THE ERROR FROM THE PREVIOUS POST,
        ' I BELIEVED YOU KNOW HOW TO DEAL WITH
        ' THE MOST SIMPLEST ListView ActiveX OBJECT
        Me.ListView1.ListItems.Add , , "ONE"   ', 1 <--- ImageList Item in this form !
        Me.ListView1.ListItems.Add , , "TWO"   ', 2      In this version
        Me.ListView1.ListItems.Add , , "THREE" ', 3      I deleted it from the form
    End Sub
    
    ' *
    ' * A QUICK WAY TO PROVIDE MYSELF OF A HWND
    ' * I made a Visual Basic EXE application (OTHER .. NOT THIS),
    ' * I layout a listview ActiveX and add some items
    ' * and make the EXE of course, and run the application
    ' * outside of the Visual Basic Enviromnent,
    ' * then I used this function
    ' * to get the HWND,
    ' * WORKS FINE WITH THIS HWND
    ' *
    Public Function MyFindWindow1() As Long
        Dim hWnd As Long
        hWnd = FindWindow("ThunderRT6FormDC", "Test")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "ListView20WndClass", "")
        MyFindWindow1 = hWnd
    End Function
    
    ' *
    ' * A QUICK WAY TO PROVIDE MYSELF OF A HWND
    ' * I download the ControlSpy 6.0 from Microsoft (for debuggin MSCOMCLT32.DLL)
    ' * read the instruction and run ControlSpyV6.exe
    ' * and select ListView from the control list with default values
    ' * then I used this function
    ' * to get the HWND,
    ' * WORKS FINE WITH THIS HWND
    ' *
    Public Function MyFindWindow2() As Long
        Dim hWnd As Long
        hWnd = FindWindow("#32770", "Control Spy")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "CSPYContainer", "")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "SysListView32", "ListView")
        MyFindWindow2 = hWnd
    End Function
    
    ' *
    ' * A QUICK WAY TO PROVIDE MYSELF OF A HWND
    ' * I layout a ListView ActiveX Control in THIS form
    ' * with default values.
    ' *
    ' * then I used this function
    ' * to get the HWND,
    ' * WORKS FINE WITH THIS HWND
    ' *
    Public Function MyFindWindow3() As Long
        Dim hWnd As Long
        hWnd = Me.ListView1.hWnd
        MyFindWindow3 = hWnd
    End Function

  3. #3
    Join Date
    Apr 2005
    Posts
    203
    Hackman thank u for u nice code. But could u guide me how to make it run. what controles do i need and what refrences and where to place the caption and/or class name of the extrnal window that holds my listview. I be waiting untill i make your cod running.Thanks

    note: If possible upload the project in visual basic 6 .

  4. #4
    Join Date
    Jul 2006
    Posts
    8
    I was confused because you are dealing with a concept of Visual Basic programming which has an advanced level; I believed you had more experience in this matter. I will try to be more specific, without extend too far. I made an lzh compressed file and uploaded here, you can use WinZip to open it.

    1. Start Visual Basic.
    2. Make a new Project (File / New Project / Standard EXE)
    3. Make a reference to Microsoft Windows Common Controls 6.0 (Project / Components / Microsoft Windows Common Controls 6.0) we need this for testing purposes only.
    4. Now in the Toolbox window you have nine more controls, if you search (with the aid of the tool tips) you will find a control named ImageList, draw one in your default Form1, and one ListView and one commandButton.
    5. Customize the ImageList (Right Mouse Button / Properties) or (Properties Windows / Custom), in the General Tab select 32 x 32, in the Images Tab select the button "Insert Picture" and go to “C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Misc” there are some icons in there. Add three more icons to the Image List.
    6. Customize the ListView (Right Mouse Button / Properties) or (Properties Windows / Custom), in the Image Lists Tab select "ImageList1" in the ComboBox labeled Normal, leave Small and ColumnList with the <None> value.
    7. Save your project.
    8. Add the following code to the Form1 (Project Window / Form1 / Right Mouse Button / View Code)
    9. I haven’t your API declarations, so I get mine from the application “API Text Viewer” that comes with Visual Basic 6, check every line and look if there are some differences, it will be very important in the ByVal clause. Please use my code, complete, even when you have yours already, if you don’t do this, maybe it will not run. You can mix the old code after you have tested all and want to keep comments or anything, and checked those are equal.

    Code:
    Option Explicit
    
    Private Type LV_ITEM
        mask As Long
        iItem As Long
        iSubItem As Long
        state As Long
        stateMask As Long
        pszText As Long
        cchTextMax As Long
        iImage As Long
        lParam As Long
        iIndent As Long
        iGroupId As Long
        cColumns As Long
        puColumns As Long
    End Type
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
    Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    Private Declare Function IsBadWritePtr Lib "kernel32" (lp As Any, ByVal ucb As Long) As Long
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hwnd1 As Long, ByVal hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long
    
    Private Const PROCESS_VM_OPERATION = &H8&
    Private Const PROCESS_VM_READ = &H10&
    Private Const PROCESS_VM_WRITE = &H20&
    
    Private Const LVIF_STATE = &H8&
    Private Const LVIS_FOCUSED = &H1&
    Private Const LVIS_SELECTED = &H2&
    
    Private Const MEM_COMMIT = &H1000&
    Private Const MEM_RESERVE = &H2000&
    Private Const MEM_RELEASE = &H8000&
    
    Private Const PAGE_READWRITE = &H4&
    
    Private Const LVM_FIRST = &H1000&
    Private Const LVM_REDRAWITEMS = LVM_FIRST + 21
    Private Const LVM_SETTEXTBKCOLOR = LVM_FIRST + 38
    Private Const LVM_SETITEMSTATE = LVM_FIRST + 43
    
    Private Sub Command1_Click()
        Dim lThreadID As Long
        Dim lProcID As Long
        Dim hProc As Long
        Dim lxprocLVITEM As Long
    
        Dim hwnd1 As Long
        
        Dim LVITEM As LV_ITEM
        LVITEM.mask = LVIF_STATE
        LVITEM.state = LVIS_SELECTED Or LVIS_FOCUSED
        LVITEM.stateMask = LVIS_SELECTED Or LVIS_FOCUSED
        
        hwnd1 = MyFindWindow3()
        
        If hwnd1 <> 0 Then
        
            lThreadID = GetWindowThreadProcessId(hwnd1, lProcID)
            If lThreadID <> 0 And lProcID <> 0 Then
                hProc = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, lProcID)
                If hProc <> 0 Then
                    lxprocLVITEM = VirtualAllocEx(hProc, ByVal 0, LenB(LVITEM), MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
                    If lxprocLVITEM <> 0 Then
                        If IsBadWritePtr(lxprocLVITEM, LenB(LVITEM)) = 0 Then
                            Dim lpWritten As Long
                            WriteProcessMemory hProc, ByVal lxprocLVITEM, LVITEM, LenB(LVITEM), lpWritten
                            If lpWritten = LenB(LVITEM) Then
                                Dim lItem As Long
                                lItem = 2&
                                Call SendMessage(hwnd1, LVM_SETITEMSTATE, ByVal lItem, ByVal lxprocLVITEM)
                            End If
                        End If
                        VirtualFreeEx hProc, ByVal lxprocLVITEM, LenB(LVITEM), MEM_RELEASE
                    End If
                    CloseHandle hProc
                End If
            End If
            
            Call SendMessage(hwnd1, LVM_SETTEXTBKCOLOR, 0, ByVal &HFF&)
            Call SendMessage(hwnd1, LVM_REDRAWITEMS, 0, ByVal &HFFFF&) ' CORRECTED ByVal &FFFF&
            UpdateWindow hwnd1
        End If
    
    End Sub
    
    Private Sub Form_Load()
        Me.ListView1.ListItems.Add , , "ONE", 1
        Me.ListView1.ListItems.Add , , "TWO", 2
        Me.ListView1.ListItems.Add , , "THREE", 3
    End Sub
    
    Public Function MyFindWindow1() As Long
        Dim hWnd As Long
        hWnd = FindWindow("ThunderRT6FormDC", "Test")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "ListView20WndClass", "")
        MyFindWindow1 = hWnd
    End Function
    
    Public Function MyFindWindow2() As Long
        Dim hWnd As Long
        hWnd = FindWindow("#32770", "Control Spy")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "CSPYContainer", "")
        If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "SysListView32", "ListView")
        MyFindWindow2 = hWnd
    End Function
    
    Public Function MyFindWindow3() As Long
        Dim hWnd As Long
        hWnd = Me.ListView1.hWnd
        MyFindWindow3 = hWnd
    End Function
    10. MyFindWindow1() is the function that searches for an hWnd,

    hWnd = FindWindow("ThunderRT6FormDC", "Test")

    where “Test” is the caption” and “ThunderRT6FormDC” is the class name. But that is not the hWnd we need, the hWnd is obtained in the next statement:

    If hWnd <> 0 Then hWnd = FindWindowEx(hWnd, 0, "ListView20WndClass", "")

    Where "" is the caption of an internal window, and "ListView20WndClass" is the class name. Because the List View has its own frame, even when is not seen, the List View is a window by itself, inside of another window, a real window if you want to say so. The problem extends if you have a window, inside another window, and inside another window and so on. You have to know exactly your application class name, window title, inside window class name ... like MyFindWindow2(), You can use the application SPY++ to find it, which is distributed with Visual Basic 6.0 (some versions?). You can use MyFindWindow3() to get the hWnd of the ListView we draw in the step 6 (So you can test with the same Visual Basic Application without having to change to other application, only for testing)

    This is what I can explain to you about this application, if you need further explanation of the functions, you should search for the function name in the MSDN and read how it is used for, suppose you need more info about FindWindow(), go to MSDN and search in there, or ask in another thread how it is used, I could explain every function in here, but . . . I will rewrite what is in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •