Jump to content

Recommended Posts

Posted (edited)

Hello! I think the thread title is obvious enough :D, Here is my code:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 290, 139, 192, 124)
$Edit1 = GUICtrlCreateEdit("Test", 8, 8, 273, 121, $ES_READONLY)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

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

    EndSwitch
WEnd

 

Thanks in advance, TD :D

Edited by TheDcoder
Marked as "Sloved"

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

Override the event handler for entering text. Simple. Oh and copying a simple Koda GUI example isn't really trying anything.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 1/30/2016 at 12:22 PM, guinness said:

Override the event handler for entering text. Simple.

Expand  

How???

  On 1/30/2016 at 12:22 PM, guinness said:

copying a simple Koda GUI example isn't really trying anything.

Expand  

I tried in my original code... the code I provided only serves as an example :P

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

How?? Help file maybe? Also the code is a terrible example to write GUI code, where is GUIDelete()?

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

; A clean example taken partially from the help file

; Global cache the edit control handle
Global $g_hEdit = 0
Global $g_sPreviousContents = ''

Example()

Func Example()
    Local $hGUI = GUICreate('', 290, 140)
    Local $iEdit = GUICtrlCreateEdit('Example text', 8, 8, 273, 121)
    $g_hEdit = GUICtrlGetHandle($iEdit)
    $g_sPreviousContents = GUICtrlRead($iEdit)

    GUIRegisterMsg($WM_COMMAND, WM_COMMAND)
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Tidy GUI resources
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $iCode = _WinAPI_HiWord($wParam)
    Switch $lParam
        Case $g_hEdit
            Switch $iCode
                ; Sent when the user has taken an action that may have altered text in an edit control
                Case $EN_CHANGE
                    GUICtrlSetData(_WinAPI_GetDlgCtrlID($g_hEdit), $g_sPreviousContents)
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_COMMAND

 

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

With an API-like interface

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

; A clean example taken partially from the help file

; Enumeration for the accessing specific global elements in the global API. See => https://www.autoitscript.com/forum/topic/173803-oop-like-approach-in-autoit/
Global Enum $API_EDIT_HANDLE, $API_EDIT_CONTENTS, $API_MAX

; Global API for caching global variables and uses a single global variable
Global $g_aAPI[$API_MAX]

Example()

Func Example()
    Local $hGUI = GUICreate('', 290, 140)
    Local $iEdit = GUICtrlCreateEdit('Example text', 8, 8, 273, 121)
    SetEdit(GUICtrlGetHandle($iEdit))
    SetEditContents(GUICtrlRead($iEdit))

    GUIRegisterMsg($WM_COMMAND, WM_COMMAND)
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Tidy GUI resources
    GUIDelete($hGUI)
EndFunc   ;==>Example

; Private API

Func GetEdit()
    Return $g_aAPI[$API_EDIT_HANDLE]
EndFunc   ;==>GetEdit

Func GetEditContents()
    Return $g_aAPI[$API_EDIT_CONTENTS]
EndFunc   ;==>GetEditContents

Func SetEdit($hEdit)
    $g_aAPI[$API_EDIT_HANDLE] = $hEdit
EndFunc   ;==>SetEdit

Func SetEditContents($sData)
    $g_aAPI[$API_EDIT_CONTENTS] = $sData
EndFunc   ;==>SetEditContents

; Handlers

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $iCode = _WinAPI_HiWord($wParam)
    Local $hEdit = GetEdit()
    Switch $lParam
        Case $hEdit
            Switch $iCode
                ; Sent when the user has taken an action that may have altered text in an edit control
                Case $EN_CHANGE
                    GUICtrlSetData(_WinAPI_GetDlgCtrlID($hEdit), GetEditContents())
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_COMMAND

 

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Or create a small event handler that is easy to expand. See I use exactly the same amount of global variables, but now I have 2 edit controls. If I expand to 3 or 4 or even 99, the global variable amount will be the same, which is one, as the rest are constants and for readability only.

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

; A clean example taken partially from the help file

; Enumeration for the custom event handler array
Global Enum $EVENT_CONTROL_ID, $EVENT_CODE, $EVENT_MAX

; Enumeration for the accessing specific global elements in the global API. See => https://www.autoitscript.com/forum/topic/173803-oop-like-approach-in-autoit/
Global Enum $API_HANDLER, $API_MAX

; Global API for caching global variables and uses a single global variable
Global $g_aAPI[$API_MAX]

; Note: Use PreExpand by guinness to remove constants and enumeration values, if "Globals" are not your thing in production code

Example()

Func Example()
    Local $hGUI = GUICreate('', 290, 400)
    Local $iEditOne = GUICtrlCreateEdit('Example text one', 5, 5, 280, 200)
    Local $iEditTwo = GUICtrlCreateEdit('Example text two', 5, 205, 280, 200)
    Local $sEditContentsOne = GUICtrlRead($iEditOne)

    ; This is intentional!
    Local $sEditContentsTwo = GUICtrlRead($iEditOne)

    RegisterHandler()

    GUIRegisterMsg($WM_COMMAND, WM_COMMAND)
    GUISetState(@SW_SHOW, $hGUI)

    Local $aEvent = Null, _
            $iMsg = 0
    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case Else
                ; Check if an event occurred within the GUI
                If $iMsg > 0 Then

                    ; Read the event handler message to get the control id and code
                    $aEvent = ReadHandler($iMsg)
                    Switch $aEvent[$EVENT_CONTROL_ID]
                        Case $iEditOne
                            Switch $aEvent[$EVENT_CODE]
                                ; Sent when the user has taken an action that may have altered text in an edit control
                                Case $EN_CHANGE
                                    GUICtrlSetData($iEditOne, $sEditContentsOne)
                            EndSwitch

                        Case $iEditTwo
                            Switch $aEvent[$EVENT_CODE]
                                ; Sent when the user has taken an action that may have altered text in an edit control
                                Case $EN_CHANGE
                                    GUICtrlSetData($iEditTwo, $sEditContentsTwo)
                            EndSwitch
                    EndSwitch

                EndIf


        EndSwitch
    WEnd

    ; Tidy GUI resources
    GUIDelete($hGUI)
EndFunc   ;==>Example

; Private API

; Get the registered event handler
Func GetHandler()
    Return $g_aAPI[$API_HANDLER]
EndFunc   ;==>GetHandler

; Read the event handler message, that was passed to the dummy control i.e. custom event handler
Func ReadHandler($iMsg)
    Local $iLong = GUICtrlRead($iMsg)

    Local $aMsg[$EVENT_MAX]
    $aMsg[$EVENT_CONTROL_ID] = _WinAPI_LoWord($iLong)
    $aMsg[$EVENT_CODE] = _WinAPI_HiWord($iLong)

    Return $aMsg
EndFunc   ;==>ReadHandler

; Register an event handler
Func RegisterHandler()
    Local $iHandler = GUICtrlCreateDummy()
    $g_aAPI[$API_HANDLER] = $iHandler

    Return $iHandler
EndFunc   ;==>RegisterHandler

; Handlers

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $iCode = _WinAPI_HiWord($wParam)
    Local $iControl = _WinAPI_GetDlgCtrlID($lParam)

    ; Generate a long value with the control id and code
    Local $iMakeLong = _WinAPI_MakeLong($iControl, $iCode)

    ; Pass to the custom handler by sending a message to the event handler
    GUICtrlSendToDummy(GetHandler(), $iMakeLong)
EndFunc   ;==>WM_COMMAND

 

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

@guinness Wow! That is a lot of work! :D... But over kill for my solution :P, I found a easier (clean too!) solution:

; A cleaned example taken from the crappy code in the question :P

#Region Includes
#include <ColorConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#EndRegion

#Region GUI
Global $g_hGUI = GUICreate("How to disable a Edit control without graying it out", 290, 139, 192, 124)
Global $g_idEditControl = GUICtrlCreateEdit("Its magic! :D", 8, 8, 273, 121, $ES_READONLY)

GUICtrlSetBkColor($g_idEditControl, $COLOR_WHITE) ; $COLOR_WHITE = 0xFFFFFF

GUISetState(@SW_SHOW)

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

    EndSwitch
WEnd
#EndRegion

 

Thanks again! TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

OK, didn't think of that little hack.

Please do study my examples though, as I still feel you have a tremendous amount to learn on application design. As for exit in the loop, that's a terrible design choice.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)
  On 1/31/2016 at 9:41 AM, guinness said:

I still feel you have a tremendous amount to learn on application design

Expand  

Yeah, my project is a big mess :P

  On 1/31/2016 at 9:41 AM, guinness said:

As for exit in the loop, that's a terrible design choice.

Expand  

:shocked: Isn't that everyone does (from a long time) to exit from the GUI?

 

P.S We can talk about this in the Chatbox if you want to...

Edited by TheDcoder
P.S

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted
  On 1/31/2016 at 10:03 AM, TheDcoder said:

.S We can talk about this in the Chatbox if you want to...

Expand  

If you want to use the chat box, then ask MVPs there why my code proposition is a good thing for medium/large projects.

And no, that's not the "correct approach". What if you exit the application after closing, minimizing, moving etc... where would you place your code to tidy up the resources? After the loops seems like an obvious choice, hence why ExitLoop is preferred.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 1/31/2016 at 1:50 PM, guinness said:

And no, that's not the "correct approach". What if you exit the application after closing, minimizing, moving etc... where would you place your code to tidy up the resources? After the loops seems like an obvious choice, hence why ExitLoop is preferred.

Expand  

I can use a custom On Exit function... Also, you can't quit in my program while it is minimized :P... Sometimes the GUI isn't even created!

 

  On 1/31/2016 at 1:50 PM, guinness said:

If you want to use the chat box, then ask MVPs there

Expand  

The only regular MVP is @TheSaint, But @water comes and goes (though, he does not prefer to use CB!)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Posted

I think this is what @guinness mean:

; A cleaned example taken from the crappy code in the question :P

#Region Includes
#include <ColorConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#EndRegion

#Region GUI
Global $g_hGUI = GUICreate("How to disable a Edit control without graying it out", 290, 139, 192, 124)
Global $g_idEditControl = GUICtrlCreateEdit("Its magic! :D", 8, 8, 273, 121, $ES_READONLY)

GUICtrlSetBkColor($g_idEditControl, $COLOR_WHITE) ; $COLOR_WHITE = 0xFFFFFF

GUISetState(@SW_SHOW)

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

GUIDelete($g_hGUI)
#EndRegion

Exit

 

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

Correct, like I have done in this thread.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
×
×
  • Create New...