代码之家  ›  专栏  ›  技术社区  ›  DDiVita

ListView CustomDraw不在64位平台上工作

  •  0
  • DDiVita  · 技术社区  · 16 年前

    我不确定是否有人能帮助解决这个问题,但是我继承了这个代码,我不知道这个问题是什么,也不熟悉WNDPROC方法。未运行的区域是“检查自定义绘图消息”部分。我需要它来运行processListCustomDraw(m)方法,以便生成一些缩略图。我相信最初的应用是W.NET 1.1,所以我可能是SOL。任何想法或想法都将非常感激。

    <SecurityPermission(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _
        Protected Overrides Sub WndProc(ByRef m As Message)
            Try
    
                If m.Msg = Win32.Consts.WM_NCPAINT Then
                    ' always repaint background if get wm_ncpaint
                    _backgroundDirty = True
                End If
    
                If m.Msg = Win32.Consts.WM_ERASEBKGND Then
                    ' see if should repaint the background or not
                    If ProcessBackground() = False Then Return
                End If
    
                ' look for messages to owner draw the listview items
                If m.Msg = Win32.Consts.OCM_NOTIFY Then
                    ' get the notification info
                    Dim notifyHeader As Win32.NMHDR = _
                     CType(m.GetLParam(GetType(Win32.NMHDR)), Win32.NMHDR)
    
                    ' turn off background painting when get the item changed message
                    If notifyHeader.code = Win32.Consts.LVN_ITEMCHANGED Then
                        _paintBackground = False
                    End If
    
                    ' check for custom draw message
                    If notifyHeader.hwndFrom.Equals(Me.Handle) And _
                     notifyHeader.code = Win32.Consts.NM_CUSTOMDRAW Then
                        _paintBackground = True
    
                        ' process the message, returns true if performed custom
                        ' drawing, otherwise false
                        If ProcessListCustomDraw(m) Then
                            Return
                        End If
                    End If
                End If
    
                MyBase.WndProc(m)
    
            Catch ex As Exception
    
                Log.WriteLog(Log.SZ_LOG_EXCEPTION & MethodBase.GetCurrentMethod().Name & " " & ex.Message)
    
            End Try
    
        End Sub
    
    ' one step closer to detecting if a listview item should be drawn
        ' return true if the listview item was drawn
        Private Function ProcessListCustomDraw(ByRef m As Message) As Boolean
            Try
                ' return true if performed custom drawing
                Dim drawSelf As Boolean = False
    
                ' get custom draw information
                Dim customDraw As Win32.NMCUSTOMDRAW = _
                 CType(m.GetLParam(GetType(Win32.NMCUSTOMDRAW)), Win32.NMCUSTOMDRAW)
    
                ' return different values in the message depending on the draw stage
                Select Case customDraw.dwDrawStage
                    Case Win32.Consts.CDDS_PREPAINT
                        m.Result = New System.IntPtr(Win32.Consts.CDRF_NOTIFYITEMDRAW)
    
                    Case Win32.Consts.CDDS_ITEMPREPAINT
                        m.Result = New System.IntPtr(Win32.Consts.CDRF_SKIPDEFAULT)
    
                        If IsItemVisible(customDraw.dwItemSpec) Then
                            ' finally, draw the listview item
                            Dim g As Graphics = Graphics.FromHdc(customDraw.hdc)
                            Try
                                DrawItemEg(g, CInt(customDraw.dwItemSpec))
                                drawSelf = True
                            Finally
                                g.Dispose()
                            End Try
                        Else
                            drawSelf = True
                        End If
    
                    Case Else
                        m.Result = New System.IntPtr(Win32.Consts.CDRF_DODEFAULT)
                End Select
    
    
                Return drawSelf
    
            Catch ex As Exception
    
                Log.WriteLog(Log.SZ_LOG_EXCEPTION & MethodBase.GetCurrentMethod().Name & " " & ex.Message)
    
            End Try
    
        End Function
    

    更新:这里是win32类:

    ' win32 values
    Private Class Win32
        Public Enum Consts
            ' messages
            WM_NCPAINT = &H85
            WM_ERASEBKGND = &H14
            WM_NOTIFY = &H4E
            OCM_BASE = &H2000
            OCM_NOTIFY = OCM_BASE + WM_NOTIFY
            NM_CUSTOMDRAW = -12
            NM_SETFOCUS = -7
            LVN_ITEMCHANGED = -101
    
            ' custom draw return flags
            CDRF_DODEFAULT = &H0
            CDRF_SKIPDEFAULT = &H4
            CDRF_NOTIFYITEMDRAW = &H20
    
            ' custom draw state flags
            CDDS_PREPAINT = &H1
            CDDS_ITEM = &H10000
            CDDS_ITEMPREPAINT = CDDS_ITEM Or CDDS_PREPAINT
        End Enum
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure NMHDR
            Public hwndFrom As IntPtr
            Public idFrom As Integer
            Public code As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RECT
            Public left As Integer
            Public top As Integer
            Public right As Integer
            Public bottom As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure NMCUSTOMDRAW
            Public hdr As NMHDR
            Public dwDrawStage As Integer
            Public hdc As IntPtr
            Public rc As RECT
            Public dwItemSpec As Integer
            Public uItemState As Integer
            Public lItemlParam As IntPtr
        End Structure
    End Class
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Hans Passant    16 年前

    如果它在x86上工作,那么您的p/invoke声明可能是错误的。在需要intptr的地方使用integer,类似的。你没有张贴,我说不出来。访问pinvoke.net以获得正确的答案。


    在看到您的编辑:是的,您的nmhdr声明是错误的。一定是这样的:

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure NMHDR
        Public hwndFrom As IntPtr
        Public idFrom As IntPtr              ' NOT INTEGER!!!
        Public code As Integer
    End Structure