Results 1 to 18 of 18

Thread: Right Click Mouse

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Right Click Mouse

    Hi Currently i'm have Activex Control made by some one else (textbox with some advanced properties and event added). From it documentation, i found it was made based on VB standard Textbox. The problem is, it's mousedown and mouseup event doesn't work (not fire at all, the event does exist). Since i like it and using it a lot in my program, so i want to add event my own mousedown and my own mouseup event by made User Control based on this activex.
    My question is how to add event on user control and how to detect mousedown and mouseup using API or whatever it take . Sample attached will be very helpfull.

    Thanks.

    BA

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    Sorry to "bump" this question up.
    Perhaps anyone on different timezone is now already up and online can give me a hands.

    Thanks

  3. #3
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    Create the events yourself. Use the following:

    VB Code:
    1. Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.  
    3. Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    You can then intercept the messages WM_LBUTTONDOWN and WM_LBUTTONUP and use the RaiseEvent() statement to tell you application to fire the event.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    Thanks Jenova

    After dig the net for while i got almost same with your suggestion, but i still don't know when exact time and how to intercept WM_LBUTTONDOWN and WM_LBUTTONUP on my current user control?

    FYI, i'm still novice in VB programming.
    Last edited by barianto; Feb 5th, 2007 at 05:06 AM.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Right Click Mouse

    Do you have the source code for the ActiveX control? If so, just make sure it exposes the textbox's MouseDown event (as Jenova showed you). If they aren't exposed your program won't see them, so you'll have to subclass the ActiveX control and intercept the messages for any MouseDown or MouseUp on the control.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    You can SubClass the control by using the CallWindowProc() and SetWindowLong() API Statements. Then write your own windowprocedure that intercepts the messages and processes them the way you want to.

    Check out the AllApi link in my sig for a definition of these API's and examples on how to use them.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    Al42:
    I don't have source code.

    Jenova:
    I don't know how to subclass it. Sorry if i'm just to whine but sample from you guys (if you got time) will be very helpful.

    Thanks

  8. #8
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    Here you go, this is how subclassing a text box control would happen, you may need to modify the code slightly, if you don't have the code then we cant ammend it for you. Hope this is of some help:

    VB Code:
    1. '
    2. ' You will need a project with
    3. ' 1 x Form
    4. ' 1 x Text Box
    5. ' 1 x Module
    6.  
    7. ' *** IN A MODULE ***
    8.  
    9. Option Explicit
    10.  
    11. Private Declare Function CallWindowProc _
    12.     Lib "user32" _
    13.     Alias "CallWindowProcA" ( _
    14.         ByVal lpPrevWndFunc As Long, _
    15.         ByVal hwnd As Long, _
    16.         ByVal Msg As Long, _
    17.         ByVal wParam As Long, _
    18.         ByVal lParam As Long) _
    19.         As Long
    20.        
    21. Private Declare Sub CopyMemory _
    22.     Lib "kernel32" _
    23.     Alias "RtlMoveMemory" ( _
    24.         Destination As Any, _
    25.         Source As Any, _
    26.         ByVal Length As Long)
    27.        
    28. Private Declare Function SetWindowLong _
    29.     Lib "user32" _
    30.     Alias "SetWindowLongA" ( _
    31.         ByVal hwnd As Long, _
    32.         ByVal nIndex As Long, _
    33.         ByVal dwNewLong As Long) _
    34.         As Long
    35.  
    36. Private Const GWL_WNDPROC = (-4)
    37.  
    38. Private Const WM_RBUTTONDOWN    As Long = &H204
    39. Private Const WM_RBUTTONUP      As Long = &H205
    40.  
    41. Private OldWindowProc           As Long
    42.  
    43. Public Sub HookWindow(txtTextBox As TextBox)
    44.     ' Set the new window procedure to monitor the text boxes message queue. When our
    45.     ' message is found it is picked out of the queue and proccess by our window
    46.     ' procedure, any other messages are left alone and are processed by the original
    47.     ' window procedure.
    48.     OldWindowProc = SetWindowLong(txtTextBox.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
    49. End Sub
    50.  
    51. Public Sub UnhookWindow(txtTextBox As TextBox)
    52.     ' Restore the old window procedure and cease monitoring the
    53.     ' the message queue.
    54.     SetWindowLong txtTextBox.hwnd, GWL_WNDPROC, OldWindowProc
    55.     If OldWindowProc Then
    56.         OldWindowProc = 0
    57.     End If
    58. End Sub
    59.  
    60. Private Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, _
    61.                                                 ByVal wParam As Long, ByVal lParam As Long) As Long
    62.  
    63.     Select Case Msg
    64.        
    65.         ' Intercept the right mouse button being released
    66.         Case WM_RBUTTONUP
    67.            
    68.             ' Process the message the way we want. If you comment the code in the form and run
    69.             ' the application, right click on the text box, you will see the default text box
    70.             ' menu. However now that we have intercepted the message we are altering it to do
    71.             ' WE want it to do, in this instance, we are generating a message box.
    72.             MsgBox "Put the code that you want here when the user right clicks", vbInformation
    73.            
    74.             ' MSDN tells us to return 0 when ever we procces a message
    75.             NewWindowProc = 0
    76.            
    77.         Case Else
    78.            
    79.             ' Restore the default handling for other messages. Remember that the text box
    80.             ' will be recieving other messages to process. We are not interested in these
    81.             ' so we let the default window procedure process them, not ours.
    82.             NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
    83.    
    84.     End Select
    85.  
    86. End Function
    87.  
    88. ' *** IN A FORM ***
    89. Option Explicit
    90.  
    91. Private Sub Form_Load()
    92.     ' Hook the textbox
    93.     HookWindow Text1
    94. End Sub
    95.  
    96. Private Sub Form_Unload(Cancel As Integer)
    97.     ' Unhook the textbox
    98.     UnhookWindow Text1
    99. End Sub
    Last edited by Jenova; Feb 6th, 2007 at 06:00 AM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    I will try it.

    Will let you know later jenova.

    PS: How to put your code above if i made user control based on this control (which doesn't have mousedown and mouseup event). User control doesn't have form, where should i put HookWindow procedure?

  10. #10
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    I'm not sure, i have never made a User Control before. I would imagine that you would still need to add it to the form that your control is on, either that, i think user controls have 'Initialize' or 'Activate' events. just hook the window from that point in the control.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    Jenova, it doesnt work. No error produce but it never met this condition
    VB Code:
    1. select Case Msg
    2.          ' Intercept the right mouse button being released
    3.         Case WM_RBUTTONUP, WM_RBUTTONDOWN

  12. #12
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    Quote Originally Posted by barianto
    Jenova, it doesnt work. No error produce but it never met this condition
    VB Code:
    1. select Case Msg
    2.          ' Intercept the right mouse button being released
    3.         Case WM_RBUTTONUP, WM_RBUTTONDOWN
    If your using it with the control you have then i am ot sure how to get around it. But it should be

    VB Code:
    1. Select Case Msg
    2.          ' Intercept the right mouse button being released
    3.         Case WM_RBUTTONUP
    4.  
    5.             ' Process message here
    6.  
    7. End Select

    Have you tried searching Google for Subclassing ActiveX Controls in Visual Basic 6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    I will try searching on goole then

    I add "WM_RBUTTONDOWN" just in case it will hit, but still doesn't wor.

  14. #14
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Right Click Mouse

    Use Spy++/Classy/WinID to get the handle of the textbox and then subclass it (Classy can actually write code for you).

    As it is created with VB, the textbox's class will be ThunderTextBox or ThunderRT6TextBox.

    Once you get the handle you can easily subclass it by following previous examples.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  15. #15
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    He doesn't need to use Spy++ or any other application for that matter, the text box has a .hWnd property.

  16. #16
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Right Click Mouse

    If I understood correctly, the Custom-TextBox-Control has a VB6 textbox inside it.

    In that case the hWnd exposed by the ActiveX control is NOT the handle of the VB6TextBox inside it.
    So, directly hooking the VB6Textbox MAY work.

    I think, the parent hierarchy is something like this,
    Code:
    Form (ThunderFormDC)
    |__ UserControl (ThunderUserControlDC)
             |__TextBox (ThunderTextBox)
    (That is what looks like when you create your own usercontrol with a textbox and place it on a form. For an 'external' ActiveX control it should be similar.)

    I'm not sure if it will work. But as directly hooking the control isn't working, there is no harm trying this.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: Right Click Mouse

    thanks, jenova, iPrank
    will try it (iPrank suggestion)

  18. #18
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Right Click Mouse

    For the record. This was actually easier than i thought . I'm not sure if you have done this but here you go this is how you subclass the textbox on the ActiveX control. FindWindow was not necesary for the control.


    VB Code:
    1. '
    2. ' For this application to work you will need
    3. ' 1 x Form
    4. ' 1 x Module
    5. ' 1 x User Control w/ Text Box Control on it
    6. '
    7.  
    8. ' Module Code
    9. Option Explicit
    10.  
    11. Private Declare Function CallWindowProc _
    12.     Lib "user32" _
    13.     Alias "CallWindowProcA" ( _
    14.         ByVal lpPrevWndFunc As Long, _
    15.         ByVal hwnd As Long, _
    16.         ByVal Msg As Long, _
    17.         ByVal wParam As Long, _
    18.         ByVal lParam As Long) _
    19.         As Long
    20.        
    21. Private Declare Sub CopyMemory _
    22.     Lib "kernel32" _
    23.     Alias "RtlMoveMemory" ( _
    24.         Destination As Any, _
    25.         Source As Any, _
    26.         ByVal Length As Long)
    27.        
    28. Private Declare Function SetWindowLong _
    29.     Lib "user32" _
    30.     Alias "SetWindowLongA" ( _
    31.         ByVal hwnd As Long, _
    32.         ByVal nIndex As Long, _
    33.         ByVal dwNewLong As Long) _
    34.         As Long
    35.  
    36. Private Const GWL_WNDPROC = (-4)
    37.  
    38. Private Const WM_RBUTTONUP      As Long = &H205
    39.  
    40. Private OldWindowProc           As Long
    41.  
    42. Public Sub HookWindow(txtTextBox As TextBox)
    43.     ' Set the new window procedure to monitor the text boxes message queue. When our
    44.     ' message is found it is picked out of the queue and proccess by our window
    45.     ' procedure, any other messages are left alone and are processed by the original
    46.     ' window procedure.
    47.     OldWindowProc = SetWindowLong(txtTextBox.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
    48. End Sub
    49.  
    50. Public Sub UnhookWindow(txtTextBox As TextBox)
    51.     ' Restore the old window procedure and cease monitoring the
    52.     ' the message queue.
    53.     SetWindowLong txtTextBox.hwnd, GWL_WNDPROC, OldWindowProc
    54.     If OldWindowProc Then
    55.         OldWindowProc = 0
    56.     End If
    57. End Sub
    58.  
    59. Private Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, _
    60.                                                 ByVal wParam As Long, ByVal lParam As Long) As Long
    61.  
    62.     Select Case Msg
    63.        
    64.         ' Intercept the right mouse button being released
    65.         Case WM_RBUTTONUP
    66.            
    67.             ' Process the message the way we want. If you comment the code in the form and run
    68.             ' the application, right click on the text box, you will see the default text box
    69.             ' menu. However now that we have intercepted the message we are altering it to do
    70.             ' WE want it to do, in this instance, we are generating a message box.
    71.             MsgBox "Put the code that you want here when the user right clicks", vbInformation
    72.            
    73.             ' MSDN tells us to return 0 when ever we procces a message
    74.             NewWindowProc = 0
    75.            
    76.         Case Else
    77.            
    78.             ' Restore the default handling for other messages. Remember that the text box
    79.             ' will be recieving other messages to process. We are not interested in these
    80.             ' so we let the default window procedure process them, not ours.
    81.             NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
    82.    
    83.     End Select
    84.  
    85. End Function
    86.  
    87. ' Form  Code
    88.  
    89. Option Explicit
    90.  
    91. Private Sub UserControl_Initialize()
    92.     HookWindow Text1
    93. End Sub
    94.  
    95. Private Sub UserControl_Resize()
    96.     With Text1
    97.         .Height = UserControl.Height
    98.         .Width = UserControl.Width
    99.     End With
    100. End Sub
    101.  
    102. Private Sub UserControl_Terminate()
    103.     UnhookWindow Text1
    104. End Sub

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