Jump to content

GUI On Focus Event


Recommended Posts

I'm trying to make a textbox that is prefilled with instructional text in light gray font color, and then when the textbox gets focus the text will the disappear so the user can type in their text. I intended on using the GUICtrlCreateInput function, prefilling the textbox with the instructional text and then once the textbox gains focus to clear the contents of the textbox and to change the font color to black. However, I can not seem to find a value that GUIGetMsg() returns if a control gains focus. Does anyone have any ideas?

Below is my idea, where the commented out portion is the part I don't know how to handle, I just made up the onfocus() function as an example of what I want.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$txtName = GUICtrlCreateInput("", 64, 32, 121, 21)
$txtPhone = GUICtrlCreateInput("555-555-5555", 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
;~      Case onfocus($txtPhone)
;~          GUICtrlSetColor($txtPhone, 0x000000)
;~          ControlSetText('','',$txtPhone,'')
    EndSwitch
WEnd

post-6009-1240955790_thumb.gif

Link to comment
Share on other sites

Hopefully someone has a better way, but this is what I came up with:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>

$txtPhoneText = '555-555-5555'
#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$txtName = GUICtrlCreateInput("", 64, 32, 121, 21)
$txtPhone = GUICtrlCreateInput($txtPhoneText, 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If _onchange($frmExample,$txtPhone, $txtPhoneText) Then
        GUICtrlSetColor($txtPhone, 0x000000)
        ControlSetText('','',$txtPhone,'')
    EndIf
WEnd

Func _onchange($form, $controlID, $originalText)
    If ControlGetHandle('','',ControlGetFocus($form) ) = GUICtrlGetHandle($controlID) AND ControlGetText('','', $controlID) = $originalText Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc
Edited by cardguy1000
Link to comment
Share on other sites

This was easy...

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$txtName = GUICtrlCreateInput("", 64, 32, 121, 21)
$blank = GUICtrlCreateLabel("", 64, 64, 121, 21)
$txtPhone = GUICtrlCreateInput("555-555-5555", 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $blank
            GUICtrlDelete($blank)
            GUICtrlSetData($txtPhone, "")
    EndSwitch
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>

Opt('MustDeclareVars', 1)

Global $hGui, $cInput, $cInput2, $wProcOld

_Main()

Func _Main()
    Local $wProcNew, $DummyMenu

    $hGui = GUICreate("Focus!", 400, 200, -1, -1, $WS_THICKFRAME, -1)
    $cInput = GUICtrlCreateInput("Type something here", 20, 20, 360, 20)
    GUICtrlSetColor(-1, 0x808080)
    $cInput2 = GUICtrlCreateInput("Type something here", 20, 50, 360, 20)
    GUICtrlSetColor(-1, 0x808080)

    $wProcNew = DllCallbackRegister("_FocusWindowProc", "ptr", "hwnd;uint;long;ptr")
    $wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($cInput), $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
    _WinAPI_SetWindowLong(GUICtrlGetHandle($cInput2), $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))

    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>_Main

Func _FocusWindowProc($hWnd, $uiMsg, $wParam, $lParam)
    Switch $uiMsg
        Case $WM_SETFOCUS
            If _WinAPI_GetWindowText($hWnd) = "Type something here" Then
                _WinAPI_SetWindowText($hWnd, "")
                If $hWnd = GUICtrlGetHandle($cInput) Then
                    GUICtrlSetColor($cInput, 0x000000)
                Else
                    GUICtrlSetColor($cInput2, 0x000000)
                EndIf
            EndIf
        Case $WM_KILLFOCUS
            If _WinAPI_GetWindowText($hWnd) = "" Then
                _WinAPI_SetWindowText($hWnd, "Type something here")
                If $hWnd = GUICtrlGetHandle($cInput) Then
                    GUICtrlSetColor($cInput, 0x808080)
                Else
                    GUICtrlSetColor($cInput2, 0x808080)
                EndIf
            EndIf
    EndSwitch

    Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $uiMsg, $wParam, $lParam)
EndFunc   ;==>_FocusWindowProc

WBD

Link to comment
Share on other sites

@Firestorm... this does work!... or tell me why it doesn't!

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$blank1 = GUICtrlCreateLabel("", 64, 32, 121, 21)
$txtName = GUICtrlCreateInput("Type some text here", 64, 32, 121, 21)
GUICtrlSetColor($txtName, 0xC0C0C0)
$blank2 = GUICtrlCreateLabel("", 64, 64, 121, 21)
$txtPhone = GUICtrlCreateInput("555-555-5555", 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $blank1
            GUICtrlDelete($blank1)
            GUICtrlSetData($txtName, "")
            GUICtrlSetColor($txtName, "")
        Case $blank2
            GUICtrlDelete($blank2)
            GUICtrlSetData($txtPhone, "")
            GUICtrlSetColor($txtPhone, "")
    EndSwitch
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

@Firestorm... this does work!... or tell me why it doesn't!

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$blank1 = GUICtrlCreateLabel("", 64, 32, 121, 21)
$txtName = GUICtrlCreateInput("Type some text here", 64, 32, 121, 21)
GUICtrlSetColor($txtName, 0xC0C0C0)
$blank2 = GUICtrlCreateLabel("", 64, 64, 121, 21)
$txtPhone = GUICtrlCreateInput("555-555-5555", 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $blank1
            GUICtrlDelete($blank1)
            GUICtrlSetData($txtName, "")
            GUICtrlSetColor($txtName, "")
        Case $blank2
            GUICtrlDelete($blank2)
            GUICtrlSetData($txtPhone, "")
            GUICtrlSetColor($txtPhone, "")
    EndSwitch
WEnd

8)

Sorry, I removed my post. I was the one who misunderstood. I was thinking focus of the GUI.

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

@Firestorm... this does work!... or tell me why it doesn't!

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$frmExample = GUICreate("Example Form", 218, 135, 193, 125)
$lblName = GUICtrlCreateLabel("Name:", 16, 40, 35, 17)
$lblPhone = GUICtrlCreateLabel("Phone:", 16, 72, 38, 17)
$blank1 = GUICtrlCreateLabel("", 64, 32, 121, 21)
$txtName = GUICtrlCreateInput("Type some text here", 64, 32, 121, 21)
GUICtrlSetColor($txtName, 0xC0C0C0)
$blank2 = GUICtrlCreateLabel("", 64, 64, 121, 21)
$txtPhone = GUICtrlCreateInput("555-555-5555", 64, 64, 121, 21)
GUICtrlSetColor($txtPhone, 0xC0C0C0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $blank1
            GUICtrlDelete($blank1)
            GUICtrlSetData($txtName, "")
            GUICtrlSetColor($txtName, "")
        Case $blank2
            GUICtrlDelete($blank2)
            GUICtrlSetData($txtPhone, "")
            GUICtrlSetColor($txtPhone, "")
    EndSwitch
WEnd

8)

Valuater, that only works if the user clicks the control. If they tab to it then it will not work.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...