🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: free.it.visual-basic
Article #2293

Re: VB.NET - WndProc di una Listview

#2293
From: Corrado Cavalli [MVP]
Date: Tue, 08 Jul 2003 18:03
145 lines
6288 bytes
L'informazione ti arriva attraverso il messaggio WM_NOTIFY,ecco un esempio:


Option Strict On

Imports System.Runtime.InteropServices

Public Class ListViewEx
  Inherits System.Windows.Forms.ListView

  ' Constants passed to the Scroll event
  Public Enum ScrollDirection
    Horizontal = 0
    Vertical
  End Enum

  ' Window messages we need to intercept
  Private Enum WindowMessages As Integer
    WM_HSCROLL = &H114
    WM_NOTIFY = &H4E
    WM_VSCROLL = &H115
  End Enum

  ' Notification messages for WM_NOTIFY
  Private Enum HeaderNotifications As Integer
    HDN_FIRST = -300&
    HDN_BEGINTRACK = (HDN_FIRST - 26)       ' Begin Column resize
    HDN_ENDTRACK = (HDN_FIRST - 27)         ' End Column resize
    HDN_DIVIDERDBLCLICK = (HDN_FIRST - 25)  ' Double-click on divider
    HDN_ITEMCHANGING = (HDN_FIRST - 20)     ' Column resizing
  End Enum

  ' Structure to hold extra information for WM_NOTIFY messages
  <StructLayout(LayoutKind.Sequential)> Private Structure NMHDR
    Friend hWndFrom As Integer
    Friend idfrom As Integer
    Friend code As Integer
  End Structure

  Protected mblnAllowColumnResize As Boolean = True

  Public Event ColumnsResized(ByVal sender As Object)
  Public Event Scroll(ByVal sender As Object, _
                      ByVal Direction As ScrollDirection)
  '***********************************************************************
  ' Public Sub New()
  ' Description: Class constructor
  '***********************************************************************
  Public Sub New()
    MyBase.New()
  End Sub
  '***********************************************************************
  ' Protected Overridable Sub OnColumnHeaderResized()
  ' Description: Performs actions and raises events when a ListView
  '              column is resized.
  '***********************************************************************
  Protected Overridable Sub OnColumnHeaderResized()
    RaiseEvent ColumnsResized(Me)
  End Sub
  '***********************************************************************
  ' Protected Overridable Sub OnScroll(ByVal Direction As
ScrollDirection)
  ' Description: Performs actions and raises events when the ListView
is
  '              scrolled.
  '***********************************************************************
  Protected Overridable Sub OnScroll(ByVal Direction As
ScrollDirection)
    RaiseEvent Scroll(Me, Direction)
  End Sub
  '***********************************************************************
  ' Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
  ' Description: Handles Windows messages
  '***********************************************************************
  Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)

    ' Execute parent WndProc.  While we're subclassing anyway, we'll
    ' encapsulate it in a Try block so that we can catch that annoying
    ' NullReferenceException that gets thrown occasionally during a
    ' LabelEdit.  NOTE: This may only be necessary in Beta 2.
    Try
      MyBase.WndProc(m)
    Catch
      Exit Sub
    End Try

    Select Case m.Msg

      ' Catch scroll messages and fire the Scroll event
      Case WindowMessages.WM_HSCROLL :
OnScroll(ScrollDirection.Horizontal)
      Case WindowMessages.WM_VSCROLL :
OnScroll(ScrollDirection.Vertical)

      ' Information about Column resizing comes throught WM_NOTIFY
      Case WindowMessages.WM_NOTIFY

        ' Read in the NMHDR structure from LParam
        With CType(m.GetLParam(GetType(NMHDR)), NMHDR)

          ' See what kind of notification we're dealing with...
          Select Case .code

            ' The user is starting to drag-resize -- abort if not
allowed
            Case HeaderNotifications.HDN_BEGINTRACK
              If mblnAllowColumnResize = False Then m.Result = New
IntPtr(1)

            ' A header is being resized -- fire the event, or abort
            Case HeaderNotifications.HDN_ITEMCHANGING
              Select Case mblnAllowColumnResize
                Case True : OnColumnHeaderResized() ' Fire the event
                Case False : m.Result = New IntPtr(1) ' Abort
              End Select
          End Select
        End With
    End Select
  End Sub
  '*********************************************************************
  ' Public Property AllowColumnResize() As Boolean
  ' Description: Returns/sets a value which determines whether
ListView
  '              columns can be resized, either manually or in code.
  ' NOTE: Setting this property to False prevents resizing Columns
using
  '       their Width property as well as manually.
  '*********************************************************************
  Public Property AllowColumnResize() As Boolean
    Get
      Return mblnAllowColumnResize
    End Get
    Set(ByVal Value As Boolean)
      mblnAllowColumnResize = Value
    End Set
  End Property
End Class

HTH

-- 
Corrado Cavalli [Microsoft .NET MVP-MCP]
UGIdotNET - http://www.ugidotnet.org
Technical Details
Message-ID: <O21OYuXRDHA.1976@TK2MSFTNGP11.phx.gbl>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!msrtrans1!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
References: <XpzOa.155797$Ny5.4435237@twister2.libero.it>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94ee,86e73fddcf9fc50a
X-Google-Attributes: gidf94ee,public
X-Google-Thread: 109528,86e73fddcf9fc50a
X-Google-Attributes: gid109528,public
X-Google-Thread: 10c104,86e73fddcf9fc50a
X-Google-Attributes: gid10c104,public
X-Google-Thread: 10b997,86e73fddcf9fc50a
X-Google-Attributes: gid10b997,public
X-Google-ArrivalTime: 2003-07-08 11:08:34 PST
From: "Corrado Cavalli [MVP]" <corradoNOSPAMPLEASE@mvps.org>
References: <XpzOa.155797$Ny5.4435237@twister2.libero.it>
Subject: Re: VB.NET - WndProc di una Listview
Date: Tue, 8 Jul 2003 20:03:17 +0200
Lines: 147
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <O21OYuXRDHA.1976@TK2MSFTNGP11.phx.gbl>
Newsgroups: free.it.visual-basic,it.comp.lang.visual-basic,microsoft.public.it.dotnet.vb,microsoft.public.it.dotnet.vstudio
NNTP-Posting-Host: host54-10.pool80180.interbusiness.it 80.180.10.54
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!msrtrans1!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl