• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Need Help On Enabling Disabled windows and or textboxes,labels,etc...

Need Help On Enabling Disabled windows and or textboxes,labels,etc...

I'm trying to write some code that will, when the mouse is pointed over, give me the objects handle so I can enable it.  Here is my code im using...

________________________________________________________

Dim P As POINTAPI, lRet As Long, hHandle As Long, aClass As String, aText As String, oldtext As String, oldhandle As Long
Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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 Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long

Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const WM_SETTEXT = &HC
Const WM_ENABLE = &HA






Private Sub Command1_Click()
'Enabling Code Goes Here!!!
End Sub

Private Sub Form_Unload(Cancel As Integer)
    End
End Sub

Private Sub Timer1_Timer()

   
    oldtext = aText
    lRet = GetCursorPos(P)
    oldhandle = WindowFromPoint(P.x, P.y)
   
    If oldhandle = Me.hwnd Then
    oldhandle = oldhandle
    Exit Sub
    End If
   
    If oldhandle = Command1.hwnd Then
    oldhandle = oldhandle
    Exit Sub
    End If
   
    If oldhandle = Text1.hwnd Then
    oldhandle = oldhandle
    Exit Sub
    End If
   
    hHandle = oldhandle
    aClass = Space$(128)
    lRet = GetClassName(hHandle, aClass, 128)
    aClass = Left$(aClass, lRet)
   
    lblHandle.Caption = hHandle
    lblClassName.Caption = aClass
    Dim lTextlen As Long
    lTextlen = SendMessage(hHandle, WM_GETTEXTLENGTH, 0, ByVal 0)
    If lTextlen Then
        If lTextlen > 1024 Then lTextlen = 1024
        lTextlen = lTextlen + 1
        aText = Space$(lTextlen)
        lRet = SendMessage(hHandle, WM_GETTEXT, lTextlen, ByVal aText)
        aText = Left$(aText, lRet)
    End If
 
     
   
    lblWText.Caption = aText
End Sub


Any help is greatly appreciated.

Nongravity.
0
nongravity
Asked:
nongravity
  • 4
  • 4
  • 3
  • +2
1 Solution
 
MicrosoftCommented:
disable a text box

text1.enable=false

that would disable and grey out text box 1


disable a command box

COMMAND1.ENABLE=FALSE

that would disable and grey out a command box

LABEL1.ENABLE = FALSE

that would disable a label caption


hope that helps
0
 
agriggsCommented:
Most controls in VB also have a MouseMove event that fires when the mouse is moved over that control.
0
 
MicrosoftCommented:
this is an example of how to get a value when the mouse is over an object maybe this might help you to get an idea.


Call this function, passing the list box's hwnd, x, and y, from the list box's mousemove event, and the item the mouse is over will be selected

Option Explicit
Private Const LB_SETCURSEL = &H186
Private Const LB_GETCURSEL = &H188
Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Declare Function LBItemFromPt Lib "COMCTL32.DLL" _
(ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, _
ByVal bAutoScroll 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 Long) As Long

Public Sub HighlightLBItem(ByVal LBHwnd As Long, _
ByVal X As Single, ByVal Y As Single)

Dim ItemIndex As Long
Dim AtThisPoint As POINTAPI
AtThisPoint.X = X \ Screen.TwipsPerPixelX
AtThisPoint.Y = Y \ Screen.TwipsPerPixelY
Call ClientToScreen(LBHwnd, AtThisPoint)
ItemIndex = LBItemFromPt(LBHwnd, AtThisPoint.X, _
   AtThisPoint.Y, False)
If ItemIndex <> SendMessage(LBHwnd, LB_GETCURSEL, 0, 0) Then
    Call SendMessage(LBHwnd, LB_SETCURSEL, ItemIndex, 0)
End If

End Sub

try that

cheers
andy

I could be just pissing in the wind, i don't know if any of what ive suggested but im trying to help as best as i can

if its not what you wont then never mind im only Human

cheers
Andy
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mpmaginCommented:
This may do what you want without using the API.  It will enable a disabled object on a form when the mouse pointer moves over it.  Hope this helps.


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim ctl As Control
       
    For Each ctl In Controls
        With ctl
            If (X >= .Left) And (X < .Left + .Width) And (Y >= .Top) And (Y < .Top + .Height) Then
                .Enabled = True
            End If
        End With
    Next ctl
   
End Sub
0
 
MicrosoftCommented:
you could try :-


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

   Dim ctl As Control
       
   For Each ctl In Controls
       With ctl
           If (X >= .Left) And (X < .Left + .Width) And (Y >= .Top) And (Y < .Top + .Height) Then
               .Enabled = True
           End If
       End With
   Next ctl
   
End Sub

:o)

he knows why#
0
 
amebaCommented:
Last 10 Grades Given: B A A A A B

"The WindowFromPoint function does not retrieve a handle to a hidden or disabled window, even if the point is within the window. An application should use the ChildWindowFromPoint function ..."

This is just a comment and cannot be 'accepted as answer' without my permission.
0
 
nongravityAuthor Commented:
Thanks for the reply's everybody !!!

ameba,
Is there a way to get the handle of a window,control,etc...
in VB Code if it is disabled. If so please let me know. I tried the ChildFromWindow function but It didn't give me the handle either. I do have a program that does this(Try before you buy). But I would like to make one of my own. (Tired of nag screens)  

0
 
nongravityAuthor Commented:
Thanks for the reply's everybody !!!

ameba,
Is there a way to get the handle of a window,control,etc...
in VB Code if it is disabled. If so please let me know. I tried the ChildFromWindow function but It didn't give me the handle either. I do have a program that does this(Try before you buy). But I would like to make one of my own. (Tired of nag screens)  

0
 
amebaCommented:
If you tried ChildWindowFromPoint and it didn't give you a handle, you are probably passing the (wrong) screen coordinates, i.e. coordinates you got from GetCursorPos - you must pass CLIENT coordinates instead.
To convert your point's coordinates, use ScreenToClient API.

Hope you are not using this to enable some 'disabled feature' in some shareware program.
0
 
nongravityAuthor Commented:
I'll try that.  Also, I'm not using to enable a disabled function or feature on shareware programs.  
0
 
amebaCommented:
OK, I modified your sample.  If this comment is not for A grade, do not accept it as answer - just ignore it.

' Form1, add one button and 3 labels (lblHandle, lblClassName, lblWText)
'     to test, add disabled textbox and disabled command button
'     or you can test it with disabled buttons in API Viewer
Option Explicit
Dim P As POINTAPI, lRet As Long, hHandle As Long, aClass As String, aText As String
Private Type POINTAPI
   x As Long
   y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd _
    As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal _
    yPoint As Long) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable _
    As Long) As Long
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const WM_SETTEXT = &HC
Const WM_ENABLE = &HA

Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal hWndParent As Long, _
    ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint _
    As POINTAPI) As Long

Private Sub Command1_Click()
    'Enabling Code Goes Here!!!
    If IsWindowEnabled(CLng(Command1.Tag)) Then
        Beep
    Else
        EnableWindow CLng(Command1.Tag), True
    End If
End Sub

Private Sub Timer1_Timer()
    Dim parenthwnd As Long, childhwnd As Long
    Dim pt As POINTAPI
   
    GetCursorPos pt
   
    parenthwnd = WindowFromPoint(pt.x, pt.y)
   
    If parenthwnd = Command1.hwnd Then
        Caption = ""
        Exit Sub
    End If
   
    ' test if there is any disabled window under the mouse
    ' convert mouse coords to client coords
    ScreenToClient parenthwnd, pt
    childhwnd = ChildWindowFromPoint(parenthwnd, pt.x, pt.y)
   
    If childhwnd = parenthwnd Then
        ' disabled control not detected
        Caption = "move mouse over disabled control"
        Command1.Enabled = False
        Command1.Tag = 0
    Else
        Caption = "click Command1 to enable control"
        Command1.Enabled = True
        Command1.Tag = childhwnd ' save hwnd for later use
    End If
   
    ' show some info
    hHandle = childhwnd
    aClass = Space$(128)
    lRet = GetClassName(hHandle, aClass, 128)
    aClass = Left$(aClass, lRet)
   
    lblHandle.Caption = hHandle
    lblClassName.Caption = aClass
    Dim lTextlen As Long
    lTextlen = SendMessage(hHandle, WM_GETTEXTLENGTH, 0, ByVal 0)
    If lTextlen Then
        If lTextlen > 1024 Then lTextlen = 1024
        lTextlen = lTextlen + 1
        aText = Space$(lTextlen)
        lRet = SendMessage(hHandle, WM_GETTEXT, lTextlen, ByVal aText)
        aText = Left$(aText, lRet)
    End If
   
    lblWText.Caption = aText
End Sub
0
 
nongravityAuthor Commented:
Works great! Thats what I was looking for.
0
 
amebaCommented:
Thanks for the points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now