Results 1 to 6 of 6

Thread: Very Hard Question (RESOLVED)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271

    Very Hard Question (RESOLVED)

    Im creating custom controls and I need to use my own windows procedure.
    I know I can replace the winproc using something like

    SetWindowLong(m_hwnd, GWL_WNDPROC, AddressOf CtrlHandler)
    But I can only get it to work with the Ctrlhandler function in a module.
    How can I get this to work so I can have my message handler in the class?? Is it possible?
    Last edited by packetVB; May 17th, 2004 at 06:55 AM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    NeverMind,

    After much thought and trial and error, I figured it out.

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Could you please show the board how you resolved this, as i'm sure this will help others.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    Sure,

    Ill go with an example textbox window
    VB Code:
    1. 'vbTextBox custom class
    2. Private m_parentHwnd As Long
    3. Private m_hwnd As Long
    4. Private m_ControlID As Long
    5. Private OldWndProc As Long
    6. Public ObjectPtr As Long
    7.  
    8. Private Sub Class_Initialize()
    9.     m_hwnd = 0
    10.     'copy this class object to ObjectPtr, will use it later
    11.     CopyMemory ObjectPtr, Me, 4
    12. End Sub
    13.  
    14. 'Message Handling for this class window
    15. Public Function MessageHandler(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    16.  
    17.     If uMsg = WM_LBUTTONDOWN Then
    18.         Debug.Print "ButtonDown"
    19.     End If
    20.     MessageHandler = CallWindowProc(OldWndProc, hWnd, uMsg, wParam, lParam)
    21. End Function
    22.  
    23. Public Function CreateTextBox(pLeft As Long, pTop As Long, pWidth As Long, pHeight As Long, pHwnd As Long, pText As String, CtrlID As Long, ExtraStyle As Long) As Long
    24.     m_parentHwnd =pHwnd
    25.     ReturnValue = CreateWindowEx(ExStyle, "EDIT", pText, WS_VISIBLE Or WS_CHILD Or ES_LEFT Or WS_TABSTOP Or ExtraStyle, pLeft, pTop, pWidth, pHeight, pHwnd, CtrlID, App.hInstance, ByVal 0)
    26.  
    27.     if Returnvalue=0 then exit function
    28.     m_hwnd = ReturnValue
    29.     'now that we created the window put the ObjectPtr (which is pointer to this class object)
    30.         'into the GWL_USERDATA of the window
    31.         SetWindowLong m_hwnd, GWL_USERDATA, ObjectPtr
    32.  
    33.         'the CtrlHandler is responsible for sending message to correct
    34.         'class object
    35.         OldWndProc = SetWindowLong(m_hwnd, GWL_WNDPROC, AddressOf CtrlHandler)
    36.         'ShowWindow m_hwnd, SW_SHOWNORMAL
    37.         CreateTextBox = m_hwnd
    38.  
    39. End Function

    Now we just created a window and replaced the windows procedure with one called CtrlHandler, that will be public in a module like so.
    VB Code:
    1. 'Public Functions module
    2. Public OO as Object
    3.  
    4. Public Function CtrlHandler(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    5. Dim ObjectPointer As Long
    6.     'get the pointer that we put into Userdata when we create the window in the class    
    7.     ObjectPointer = GetWindowLong(hWnd, GWL_USERDATA)
    8.     'if its not 0
    9.     If ObjectPointer <> 0 Then
    10.         'copy memory to the tmp object OO
    11.         'the tmp object must be PUBLIC declared in the module
    12.         CopyMemory OO, ObjectPointer, 4
    13.         'Call the Class function
    14.         CtrlHandler = OO.MessageHandler(hWnd, Msg, wParam, lParam)
    15.     Else
    16.         'do the default (for forms)
    17.         CtrlHandler = DefWindowProc(hWnd, Msg, wParam, lParam)
    18.     End If
    19. End Function
    Not sure how well this will work, seems to be working pretty good for now.

    Of course GetWindowLong,CopyMemory,DefWindowProc are API's, check all APINET for how to declare and for the Const.

    Cheers,
    packetvb
    Last edited by packetVB; May 17th, 2004 at 07:33 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    yo admins

    Can you move this to the code bank.
    Dont want to double post.


    packetvb

  6. #6

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