vbAccelerator - Contents of code file: cClipboardViewer.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cClipboardViewer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' ===========================================================================
' Filename: cClipboardViewer
' Author:   Steve McMahon (steve@vbaccelerator.com)
' Date:     2 January 1999
'
' Description:
' Adds clipboard change notification handling to a VB form.  Requires
' SSUBTMR.DLL from vbAccelerator.
'
' ---------------------------------------------------------------------------
' vbAccelerator - free, advanced source code for VB programmers.
'     http://vbaccelerator.com
' ===========================================================================

Implements ISubclass

Private Declare Function SetClipboardViewer Lib "user32" (ByVal hwnd As Long)
 As Long
Private Declare Function ChangeClipboardChain Lib "user32" (ByVal hwnd As Long,
 ByVal hWndNext As Long) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA"
 (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As
 String) As Long
Private Declare Function SendMessageByLong Lib "user32" Alias "SendMessageA"
 (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As
 Long) 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 Const WM_CHANGECBCHAIN = &H30D
Private Const WM_DRAWCLIPBOARD = &H308

Private m_hWnd As Long
Private m_hWndNextViewer As Long
Private m_bInClipboardChangeNotification As Boolean
Private m_emr As EMsgResponse

Public Event ClipboardChanged()

Public Sub InitClipboardChangeNotification(hWndA As Long)
    StopClipboardChangeNotification
    m_hWnd = hWndA
    If (m_hWnd <> 0) Then
        ' Attach Clipboard 'viewer' notification messages
        AttachMessage Me, m_hWnd, WM_CHANGECBCHAIN
        AttachMessage Me, m_hWnd, WM_DRAWCLIPBOARD
        ' Place me in the clipboard viewer notification chain:
        m_hWndNextViewer = SetClipboardViewer(m_hWnd)
        m_bInClipboardChangeNotification = True
    End If
End Sub
Public Sub StopClipboardChangeNotification()
    If (m_bInClipboardChangeNotification) Then
        If (m_hWnd <> 0) Then
            ' Take myself out of the clipboard chain:
            ChangeClipboardChain m_hWnd, m_hWndNextViewer
            ' Stop subclassing for clipboard messages:
            DetachMessage Me, m_hWnd, WM_CHANGECBCHAIN
            DetachMessage Me, m_hWnd, WM_DRAWCLIPBOARD
        End If
    End If
    m_bInClipboardChangeNotification = False
End Sub
Private Sub Class_Terminate()
   StopClipboardChangeNotification
End Sub

Private Function ISubclass_WindowProc(ByVal hwnd As Long, _
                                      ByVal iMsg As Long, _
                                      ByVal wParam As Long, _
                                      ByVal lparam As Long) As Long
    Select Case iMsg
    Case WM_CHANGECBCHAIN
        If (wParam = m_hWndNextViewer) Then
            ' If the next viewer window is closing, repair the chain:
            m_hWndNextViewer = lparam
        ElseIf (m_hWndNextViewer <> 0) Then
            ' Otherwise if there is a next window, pass the message on:
            SendMessageByLong m_hWndNextViewer, iMsg, wParam, lparam
        End If
        ISubclass_WindowProc = 0
        
    Case WM_DRAWCLIPBOARD
        ' the content of the clipboard has changed.
        ' We raise a ClipboardChanged message and pass the message on:
        RaiseEvent ClipboardChanged
        SendMessageByLong m_hWndNextViewer, iMsg, wParam, lparam
        ISubclass_WindowProc = 0
        
    End Select
End Function

Private Property Get ISubclass_MsgResponse() As EMsgResponse
    ISubclass_MsgResponse = m_emr
End Property
Private Property Let ISubclass_MsgResponse(ByVal emrA As EMsgResponse)
    m_emr = emrA
End Property