Jump to content

Reading GUI date control instantly


Go to solution Solved by Melba23,

Recommended Posts

Posted

Hi,

Anyone can show me thru the example below how can I enable real time reading of the value of the date? What I want is to check the date value as soon as the user click on the next date entry instead of waiting for him to click on the submit button. Any help is appreciated.

Click on date1, when user leave the date1 gui control, read value of date1

Click on date 2, when user leave the date2 gui control, read value of date2

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $date, $msg

    GUICreate("My GUI get date", 200, 200, 800, 200)
    $date1 = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20)
    $date2 = GUICtrlCreateDate("1953/04/25", 10, 50, 185, 20)
    $button1 = GUICtrlCreateButton("Submit", 10, 100, 50, 50, -1, -1)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    GUIDelete()
EndFunc   ;==>Example
Posted (edited)

@FireFox

I think about this but this is not what he need (I think so)

In your example you must change date, but i think, OP want to check date after changing focus

Maybe this:

#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $date, $msg
    Local $date, $msg

    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate1 = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20)
    Local $idDate2 = GUICtrlCreateDate("1953/04/25", 10, 50, 185, 20)
    Local $button1 = GUICtrlCreateButton("Submit", 10, 100, 50, 50, -1, -1)
    GUISetState(@SW_SHOW)

    Local $idLastFocus = 0
    ; Local $idNewFocus = 0
    ; Loop until the user exits.
    $idLastFocus = ControlGetFocus($hGui)
    While 1
        ; ConsoleWrite('ControlGetFocus($hGui) = ' & ControlGetFocus($hGui) & @CRLF)
        If $idLastFocus <> ControlGetFocus($hGui) Then
            If $idLastFocus = 'SysDateTimePick321' Then
                ConsoleWrite('Date1 = ' & GUICtrlRead($idDate1) & @CRLF)
            EndIf
            If $idLastFocus = 'SysDateTimePick322' Then
                ConsoleWrite('Date2 = ' & GUICtrlRead($idDate2) & @CRLF)
            EndIf
            $idLastFocus = ControlGetFocus($hGui)
        EndIf
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idDate1
                ConsoleWrite(GUICtrlRead($idDate1) & @CRLF)

            Case $idDate2
                ConsoleWrite(GUICtrlRead($idDate2) & @CRLF)

            Case $button1


        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

EDIT:

commented  Local $idNewFocus = 0

because not needed

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

Instead of keep checking the focus, use WM_COMMAND. You will surely find a $ ..._KILLFOCUS.

Edited by FireFox
Posted (edited)
For the past hour I studied WM_KILLFOCUS message on MSDN 
 
And I was wondering how to use WM_KILLFOCUS for control, I thought that this message applies only to the entire window, not a single element of this window.
 
If I'm wrong, please give me some tips.
 
 
EDIT:
I found some further example
 
But still looking for the way to understanding how it works. 
Probably a little more time passed and the magic dissipate, and in its place will be knowledge ;)
Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Moderators
  • Solution
Posted

lolipop,

 

  Quote

how can I enable real time reading of the value of the date?

This allows you to detect changes to the date control instantly:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 200, 200, 800, 200)
$cDate1 = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20)
$cDate2 = GUICtrlCreateDate("1953/04/25", 10, 50, 185, 20)
$cButton = GUICtrlCreateButton("Submit", 10, 100, 50, 50, -1, -1)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop

    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Switch _WinAPI_LoWord($wParam) ; ControlID of the control
        Case $cDate1, $cDate2
            Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
            If DllStructGetData($tStruct, "Code") = $DTN_DATETIMECHANGE Then
                ConsoleWrite("1: " & GUICtrlRead($cDate1) & " - 2: " & GUICtrlRead($cDate2) & @CRLF)
            EndIf
    EndSwitch

EndFunc
Any use? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

  On 3/13/2014 at 1:48 AM, Melba23 said:

lolipop,

 

This allows you to detect changes to the date control instantly:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 200, 200, 800, 200)
$cDate1 = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20)
$cDate2 = GUICtrlCreateDate("1953/04/25", 10, 50, 185, 20)
$cButton = GUICtrlCreateButton("Submit", 10, 100, 50, 50, -1, -1)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop

    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Switch _WinAPI_LoWord($wParam) ; ControlID of the control
        Case $cDate1, $cDate2
            Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
            If DllStructGetData($tStruct, "Code") = $DTN_DATETIMECHANGE Then
                ConsoleWrite("1: " & GUICtrlRead($cDate1) & " - 2: " & GUICtrlRead($cDate2) & @CRLF)
            EndIf
    EndSwitch

EndFunc
Any use? :)

M23

 

Hi Melba,

Sorry for the late reply. Thanks for the solution. It works perfectly.

I have try intergating your solution with a sample code I find in the forum (I have a listview and I wish to use this sample code to custom draw my listview item)  but I can't seem to made it work. The sample code is basically meant to do a custom draw for the listview item color. With my limited knowledge, I can't make it work. You think you can help? Sample below.

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
   Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    ; Local $tBuffer
   $hWndListView = $Timelog_form_Listview
   If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($Timelog_form_Listview)

   $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
   $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
   $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
   $iCode = DllStructGetData($tNMHDR, "Code")

   Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
               Case $NM_CUSTOMDRAW
                  If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG ; Not in details mode

                     Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $iColor1, $iColor2, $iColor3

                     $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                     $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')

                     Switch $iDrawStage
                        Case $CDDS_PREPAINT
                           Return $CDRF_NOTIFYITEMDRAW
                        Case $CDDS_ITEMPREPAINT
                           Return $CDRF_NOTIFYSUBITEMDRAW
                        Case $CDDS_ITEMPOSTPAINT
                           ; Not handled
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                           $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                           $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')

                           If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then ; Item to draw is selected
                              $hDC = _WinAPI_GetDC($hWndFrom)
                              $tRect = DllStructCreate($tagRECT)

                              ; We draw the background when we draw the first item.
                              If $iSubitem = 0 Then
                                 ; We must send the message as we want to use the struct. _GUICtrlListView_GetSubItemRect returns an array.
                                 _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))

                                 DllStructSetData($tRect, "Left", 2)
                                 _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_GetStockObject($GRAY_BRUSH)) ; Change the bush here. You can use GDI+ to make your own.
                              EndIf

                              DllStructSetData($tRect, "Left", 2)
                              DllStructSetData($tRect, "Top", $iSubitem)
                              _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))

                              Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
                              _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; It uses the background drawn for the first item.

                              ; Select the font we want to use
                              _WinAPI_SelectObject($hDC, _SendMessage($hWndFrom, $WM_GETFONT))

                              If $iSubitem = 0 Then
                                 DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 2)
                              Else
                                 DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 6)
                              EndIf
                              _WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))

                              _WinAPI_ReleaseDC($hWndFrom, $hDC)

                              Return $CDRF_SKIPDEFAULT ; Don't do default processing
                           EndIf

                            Return $CDRF_NEWFONT ; Let the system do the drawing for non-selected items
                        Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
                            ; Not handled
                      EndSwitch
                  Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                  Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                  Local $iItem = DllStructGetData($tInfo, "Item")
                  Global $vValidTimeIn = _GUICtrlListView_GetItemText($TimeListview, $iItem, 1)
                  Global $vSelectedUsername = _GUICtrlListView_GetItemText($Time_Listview, $iItem, 0)
                  If $vValidTimeIn == "" Then
                     GUICtrlSetState($Time_button1, $GUI_HIDE)
                     GUICtrlSetState($Time_button2, $GUI_SHOW)
                     GUICtrlSetState($Time_input1,$GUI_FOCUS)
                  Else
                     GUICtrlSetState($Time_button1, $GUI_SHOW)
                     GUICtrlSetState($Time_button2, $GUI_HIDE)
                     GUICtrlSetState($Time_input1,$GUI_FOCUS)
                  EndIf
            EndSwitch
   EndSwitch
   Return $GUI_RUNDEFMSG
EndFunc

Func RGB2BGR($iColor)
   Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc

Btw, thank you Firefox and mLipok for their solution too. :)

Edited by lolipop
  • Moderators
Posted

lolipop,

You just need to have both sets of code inside the handler: :)

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)

    ; Start by looking for the Date control
    Switch _WinAPI_LoWord($wParam) ; ControlID of the control
        Case $cDate1, $cDate2
            Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
            If DllStructGetData($tStruct, "Code") = $DTN_DATETIMECHANGE Then
                ConsoleWrite("1: " & GUICtrlRead($cDate1) & " - 2: " & GUICtrlRead($cDate2) & @CRLF)
            EndIf
    EndSwitch

    ; And then see if the ListView needs attention
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    ; Local $tBuffer
    $hWndListView = $Timelog_form_Listview
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($Timelog_form_Listview)
    ; etc, etc
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • 5 years later...
Posted
  On 3/12/2014 at 8:09 PM, mLipok said:

And I was wondering how to use WM_KILLFOCUS for control

Expand  

Example below seems to work (just copy-paste from here)

All credits for argumentum and Melba23, both referenced in the code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 200, 200, 800, 200)
$cDate1 = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20)
$cDate2 = GUICtrlCreateDate("1953/04/25", 10, 50, 185, 20)
$cButton = GUICtrlCreateButton("Submit", 10, 100, 50, 50, -1, -1)

;argumentum, Nov 2017
;https://www.autoitscript.com/forum/topic/191058-datetime-pick-coloring/
Global $g_hDTP1 = GUICtrlGetHandle($cDate1)
Global $g_hDTP2 = GUICtrlGetHandle($cDate2)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
   Switch GUIGetMsg()
     Case $GUI_EVENT_CLOSE
         ExitLoop

   EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
   ;Melba23, March 2014
   ;https://www.autoitscript.com/forum/topic/159764-reading-gui-date-control-instantly/?do=findComment&comment=1161074
    ; Start by looking for the Date control
    Switch _WinAPI_LoWord($wParam) ; ControlID of the control
      Case $cDate1, $cDate2
         Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
         If DllStructGetData($tStruct, "Code") = $DTN_DATETIMECHANGE Then
            ConsoleWrite("1: " & GUICtrlRead($cDate1) & " - 2: " & GUICtrlRead($cDate2) & @CRLF)
         EndIf
    EndSwitch

   ;Melba23, March 2014
   ;https://www.autoitscript.com/forum/topic/159764-reading-gui-date-control-instantly/?do=findComment&comment=1161074
    ; And then see if the ListView needs attention
;~     Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
;~     ; Local $tBuffer
;~     $hWndListView = $Timelog_form_Listview
;~     If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($Timelog_form_Listview)
    ; etc, etc

   ;argumentum, Nov 2017
   ;https://www.autoitscript.com/forum/topic/191058-datetime-pick-coloring/
   Local $hWndFrom, $iCode, $tNMHDR, $iIDFrom
   $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
   $hWndFrom = HWnd(DllStructGetData($tNMHDR, "HwndFrom"))
   $iCode = DllStructGetData($tNMHDR, "Code")
   $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")

   Switch $hWndFrom
        Case $g_hDTP1, $g_hDTP2
            Switch $iCode
                Case $NM_SETFOCUS
                    ConsoleWrite("$NM_SETFOCUS" & @CRLF & "-->hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF)
                Case $NM_KILLFOCUS
                    ConsoleWrite("$NM_KILLFOCUS" & @CRLF & "-->hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF)
                    Consolewrite("+-> GUICtrlRead:" & @TAB & GUICtrlRead($cDate1) & @CRLF)

            EndSwitch
   EndSwitch

EndFunc

 

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
  • Recently Browsing   0 members

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