Solved

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

Posted on 2001-06-06
13
218 Views
Last Modified: 2012-06-27
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
Comment
Question by:nongravity
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6160487
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
 
LVL 2

Expert Comment

by:agriggs
ID: 6160870
Most controls in VB also have a MouseMove event that fires when the mouse is moved over that control.
0
 
LVL 2

Expert Comment

by:Microsoft
ID: 6160957
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
 

Expert Comment

by:mpmagin
ID: 6161086
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
 
LVL 2

Expert Comment

by:Microsoft
ID: 6161314
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
 
LVL 15

Expert Comment

by:ameba
ID: 6162029
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:nongravity
ID: 6164330
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
 

Author Comment

by:nongravity
ID: 6164335
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
 
LVL 15

Expert Comment

by:ameba
ID: 6164448
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
 

Author Comment

by:nongravity
ID: 6164766
I'll try that.  Also, I'm not using to enable a disabled function or feature on shareware programs.  
0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 6165830
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
 

Author Comment

by:nongravity
ID: 6167671
Works great! Thats what I was looking for.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6167802
Thanks for the points!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now