Results 1 to 3 of 3

Thread: How can I detect if a label has been klicked on another form?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Location
    Bergen, Norway
    Posts
    143

    Question

    How can I detect if a labael has been klicked on another form?

    I think the code should go somthing like this, but anything helps.

    Code:
    If frmForm2.lblLabel.click then 'this is the part im having problem with
    goto 1
    else
    do nothing
    end if
    Thanks in advance


    -Lumin

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    Ok, I have given this a quick test, and this works,
    however, it can only work if you can get hwnd of the object
    that you would like to have respond. Basically what you
    need to do is subclass the form that you wish to get a
    response from. Now since the Label control does not have
    an hwnd, you should instead us the Textbox control. You
    can lock the text, change the background color, and remove
    the boarder and make it look just like a label. Anyway,
    here you go.


    Code:
    '********************************************************
    ''This code goes in a module
    
    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
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
    Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
    Public Const GWL_WNDPROC = -4
    Public Const WM_DESTROY = &H2
    Public Const WM_QUIT = &H12
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    
    Public Const MB_OK = &H0&
    Public Const MB_ICONEXCLAMATION = &H30&
    
    Public Const IDC_ARROW = 32512&
    
    Public Const IDI_APPLICATION = 32512&
    Private lpPrevWndProc As Long
    Private gLabel As Long
    
    
    Public Function HookWindow(ByVal hWnd As Long, ByVal labelhwnd As Long)
    
        lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
        gLabel = GetWindowLong(labelhwnd, GWL_WNDPROC)
        Call SetWindowLong(labelhwnd, GWL_WNDPROC, GetAddress(AddressOf LabelWndProc))
        End Function
    
    Public Sub UnHookWindow(ByVal hWnd As Long)
    
        Call SetWindowLong(hWnd, GWL_WNDPROC, lpPrevWndProc)
    End Sub
    
    Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    'This is the code that you would if you would like to 
    'have the window respond to some interaction.  Here I am 
    'just calling the quit command when the window is clicked.
    
        Select Case uMsg&
           Case WM_DESTROY:
              Call PostQuitMessage(0&)
        End Select
        
        WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
    End Function
    
    Public Function LabelWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
        Select Case uMsg&
           Case WM_LBUTTONUP:
              Call MessageBox(gHwnd&, "You clicked myTextbox!", App.Title, MB_OK Or MB_ICONEXCLAMATION)
        End Select
        
    
      LabelWndProc = CallWindowProc(gLabel, hWnd&, uMsg&, wParam&, lParam&)
       
    End Function
    
    Public Function GetAddress(ByVal lngAddr As Long) As Long
    
    
        GetAddress = lngAddr&
        
    End Function
    
    
    ''End module
    '********************************************************


    And this is the code that you would put into the form that has the textbox that you want to know when is clicked.


    Code:
    Private Sub Form_Load()
    Call HookWindow(Form2.hWnd, Text1.hWnd)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    'Make sure you unhook the window, otherwise you will
    'cause many errors, and will loss much work.
    'Make sure you save often.
    
    Call UnHookWindow(Form2.hWnd)
    
    End Sub
    And that should do it. Now my test project had two forms: Form1 and Form2. When form1 loaded, form2 also loaded. Since I don't know exactly what you are doing, I can only guess as to how it might look.

    Hope this is helpful.

    (Using VB6 SP4)


  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Here's an easier way:
    Code:
    'Form1 code
    Private WithEvents LabelInForm2 As Label
    
    Private Form_Load()
        Set LabelInForm2 = frmForm2.lblLabel
        'other code goes here
    End Sub
    
    Private Sub LabelInForm2_Click()
        MsgBox "You have just clicked on the Label in frmForm2"
    End Sub
    Good luck!

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