Results 1 to 7 of 7

Thread: Subclassing a ComboBox Paste Event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Question Subclassing a ComboBox Paste Event

    What I'm doing is subclassing a ComboBox and trying to disable pasting to it by blocking the WM_PASTE message. However, this doesn't work for some reason. So next I tried listing off all of the messages sent to the ComboBox to see what was being sent to it exactly. What I got confused me even more! Whenever I did a paste (by pressing either <SHIFT>+<INSERT> or <CTRL>+<V> ) these are the mesages that were intercepted:
    • WM_COMMAND (&H111)
      - wParam = 0400 03E9 (EN_UPDATE + identifier of control)
      - lParam = 6D8 (the control handle)
    • WM_CTLCOLOREDIT (&H133)
      - wParam = 5EE (varies a control Device Context handle)
      - lParam = 6D8 (the control handle)
    • WM_COMMAND (&H11)
      - wParam = 0300 03E9 (EN_CHANGE + identifier of control)
      - lParam = 6D8 (the control handle)


    OK, so that's all fine and dandy, but how the hell does the Edit portion of the ComboBox receive the/a paste message? I tried it repeatedly and I always got this combination of messages. The odd thing is that I can send (and intercept) a WM_PASTE message to the ComboBox and it works fine!


    Can anybody explain to me what is going on?
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Unhappy

    Doesn't anybody know an answer to this?

    I really need to know this!
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    The problem is that the Combobox's Hwnd isn't the Handle receiving the Message, it the "Edit" Class it uses, so you need to Subclass this Window Handle instead, ie.

    In a Module:
    Code:
    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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const GWL_WNDPROC = (-4)
    Private Const WM_PASTE = &H302
    
    Private lPrevWndProc As Long
    
    Private Function CallBackProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If Msg <> WM_PASTE Then CallBackProc = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, ByVal lParam)
    End Function
    
    Public Sub SubClassComboEdit(ByRef oCombo As ComboBox)
        Dim lHwnd As Long
        If lPrevWndProc = 0 Then
            lHwnd = FindWindowEx(oCombo.hWnd, 0, "Edit", vbNullString)
            lPrevWndProc = SetWindowLong(lHwnd, GWL_WNDPROC, AddressOf CallBackProc)
        End If
    End Sub
    
    Public Sub RemoveSubClass(ByRef oCombo As ComboBox)
        Dim lHwnd As Long
        If lPrevWndProc Then
            lHwnd = FindWindowEx(oCombo.hWnd, 0, "Edit", vbNullString)
            Call SetWindowLong(lHwnd, GWL_WNDPROC, lPrevWndProc)
        End If
    End Sub
    In the Form with the Combobox:
    Code:
    Private Sub Form_Load()
        SubClassComboEdit Combo1
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        RemoveSubClass Combo1
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Smile

    Thank you! Thank you! Thank you! Thank you!!!!!!


    I had figured that all components of the ComboBox would have their messages filtered by the exposed handle.
    I mean you can send a WM_PASTE message to the CB handle and it will still perform the paste, weird.
    Oh well, once again,

    Thanks
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  5. #5
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Re: Subclassing a ComboBox Paste Event

    Aaron Young, I need to block the copy and paste on my combobox and I tried this code of yours, but on the line between ''''

    Public Sub SubClassComboEdit(ByRef oCombo As ComboBox)
    Dim lHwnd As Long
    If lPrevWndProc = 0 Then
    ''''''lHwnd = FindWindowEx(oCombo.hWnd, 0, "Edit", vbNullString)''''''
    lPrevWndProc = SetWindowLong(lHwnd, GWL_WNDPROC, AddressOf CallBackProc)
    End If
    End Sub

    It's coming an error 438.. How can I solve this problem? If u can see it for me, I uploaded it on it: http://rapidshare.com/files/418537411/teste_login.xls

    Thanks a lot
    PS: Some notes are in portuguese..

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Subclassing a ComboBox Paste Event

    You do realise that this thread is over 10 years old don't you?

    Error 438 means that the object does not support this property or method. In your case, a ComboBox in XL does not have a hwnd property.

  7. #7
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Re: Subclassing a ComboBox Paste Event

    hahahaha

    No man.. I did'nt realised, because i'm a junior on VBA..
    What can I do, you know?
    I really need to block the paste and copy on the combobox, I tried many codes and variations till now, but I do not understand it as much it needs to work right :P

Posting Permissions

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



Click Here to Expand Forum to Full Width