Jump to content

Editable Label


SkellySoul
 Share

Recommended Posts

Hello :bye:

It has been quite awhile since I posted but thought I would share an interesting script that I am using in one of my projects.

This script allows a user to quickly edit a label control and and relock it.

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

Global $Data, $Label

GUICreate("Example", 500, 500)
$Data = GUICtrlCreateEdit("Write something in here and hit lock.", 0, 0, 500, 480)
$State = GUICtrlCreateButton("Lock", 0, 480, 500, 20)
GUISetState(@SW_SHOW)

$Set = 0

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $State
            If $Set = 0 Then
                $Label = GUICtrlCreateLabel(GUICtrlRead($Data), 0, 0, 500, 480)
                GUICtrlSetState($Label, $GUI_HIDE)
                GUICtrlDelete($Data)
                GUICtrlSetState($Label, $GUI_SHOW)
                GUICtrlSetData($State, "Unlock")
                $Set = 1
            ElseIf $Set = 1 Then
                $Data = GUICtrlCreateEdit(GUICtrlRead($Label), 0, 0, 500, 480)
                GUICtrlSetState($Data, $GUI_HIDE)
                GUICtrlDelete($Label)
                GUICtrlSetState($Data, $GUI_SHOW)
                GUICtrlSetData($State, "Lock")
                $Set = 0
            Else
            ;    _Error()
                Exit
            EndIf
    EndSwitch
WEnd
Link to comment
Share on other sites

My version:

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

_Example()

Func _Example()
    Local $iMsg, $iPseudoLabel, $iBtn_State, $iFlag = 0

    GUICreate("Edit as Label example.", 320, 240)

    $iPseudoLabel = GUICtrlCreateEdit("Write something in here and hit lock.", 10, 100, 300, 20, $ES_MULTILINE, $WS_EX_STATICEDGE)
    $State = GUICtrlCreateButton("Lock", 110, 200, 100, 25)

    GUISetState(@SW_SHOW)

    While 1
        $iMsg = GUIGetMsg()

        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                Exit
            Case $iMsg = $State And $iFlag
                GUICtrlSetStyle($iPseudoLabel, $ES_MULTILINE, $WS_EX_STATICEDGE)
                GUICtrlSetState($iPseudoLabel, $GUI_FOCUS)
                GUICtrlSetCursor($iPseudoLabel, 5)
                GUICtrlSetData($State, "Lock")
                $iFlag = 0
            Case $iMsg = $State And Not $iFlag
                GUICtrlSetStyle($iPseudoLabel, $ES_READONLY, $WS_EX_TRANSPARENT)
                GUICtrlSetCursor($iPseudoLabel, 2)
                GUICtrlSetData($State, "Unlock")
                $iFlag = 1
        EndSelect
    WEnd
EndFunc   ;==>_Example

Regards,

João Carlos.

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

I thought it would be something like this:

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

$hGUI = GUICreate("Example", 500, 500)

$iLabel = GUICtrlCreateLabel("Some data, right click to show the menu.", 20, 20, 460, 40)

$iMenu = GUICtrlCreateContextMenu($iLabel)
$iRename_MItem = GUICtrlCreateMenuItem("Rename", $iMenu)

GUISetState(@SW_SHOW)

While 1
    $iMsg = GUIGetMsg()

    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $iRename_MItem
            GUICtrlSetState($iLabel, $GUI_HIDE)

            $aCtrlPos = ControlGetPos($hGUI, '', $iLabel)

            $iEdit = GUICtrlCreateEdit(GUICtrlRead($iLabel), $aCtrlPos[0], $aCtrlPos[1],  $aCtrlPos[2]+5, $aCtrlPos[3]+5, BitOR($ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
            $hEdit = GUICtrlGetHandle($iEdit)

            While 1
                Sleep(10)

                $tPoint = _WinAPI_GetMousePos()

                If _WinAPI_WindowFromPoint($tPoint) <> $hEdit And (_IsPressed('01') Or _IsPressed('1B')) Then
                    ExitLoop
                EndIf
            WEnd

            GUICtrlSetData($iLabel, GUICtrlRead($iEdit))
            GUICtrlDelete($iEdit)
            GUICtrlSetState($iLabel, $GUI_SHOW)
    EndSwitch
WEnd
Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

In the UDF format to make life easier for noobs ...

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

$hGUI = GUICreate("Example", 500, 500)

$iLabel = GUICtrlCreateLabel("Some data, right click to show the menu.", 20, 20, 460, 40)

$iMenu = GUICtrlCreateContextMenu($iLabel)
$iRename_MItem = GUICtrlCreateMenuItem("Rename", $iMenu)

GUISetState(@SW_SHOW)

While 1
    $iMsg = GUIGetMsg()

    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $iRename_MItem
            _GUICtrlLabel_Edit($iLabel)
    EndSwitch
WEnd

; @MrCreatoR
Func _GUICtrlLabel_Edit($iCtrlID)
    Local $hCtrlID, $aCtrlPos, $hWndForm, $iEditID, $hEditID, $tPoint

    $hCtrlID = GUICtrlGetHandle($iCtrlID)
    If Not $iCtrlID Then Return SetError(0, 0, 0)

    $hWndForm = _WinAPI_GetParent($hCtrlID)
    If Not $iCtrlID Then Return SetError(0, 0, 0)

    GUICtrlSetState($iCtrlID, $GUI_HIDE)
    $aCtrlPos = ControlGetPos($hWndForm, '', $iCtrlID)
    If @error Then Return SetError(0, 0, 0)

    $iEditID = GUICtrlCreateEdit(GUICtrlRead($iCtrlID), $aCtrlPos[0], $aCtrlPos[1], $aCtrlPos[2] + 5, $aCtrlPos[3] + 5, _
            BitOR($ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
    $hEditID = GUICtrlGetHandle($iEditID)

    While 1
        Sleep(10)

        $tPoint = _WinAPI_GetMousePos()

        If _WinAPI_WindowFromPoint($tPoint) <> $hEditID And (_IsPressed('01') Or _IsPressed('1B')) Then
            ExitLoop
        EndIf
    WEnd

    GUICtrlSetData($iCtrlID, GUICtrlRead($iEditID))
    GUICtrlDelete($iEditID)
    GUICtrlSetState($iCtrlID, $GUI_SHOW)
EndFunc   ;==>_GUICtrlLabel_Edit

Regards,

João Carlos.

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Here another approach:

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

Global $Data, $Label, $sData, $iExStyle, $iStyle, $hGUI, $hData, $State, $Set

$hGUI = GUICreate("Example", 500, 500)
$Data = GUICtrlCreateEdit("Write something in here and hit lock.", 0, 0, 500, 480)
$hData = GUICtrlGetHandle($Data)
$State = GUICtrlCreateButton("Lock", 0, 480, 500, 20)
GUISetState(@SW_SHOW)

$iStyle = _WinAPI_GetWindowLong($hData, $GWL_STYLE )
$iExStyle = _WinAPI_GetWindowLong($hData, $GWL_EXSTYLE)
$Set = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $State
            If $Set = 0 Then
                _WinAPI_SetWindowLong($hData, $GWL_STYLE , BitXOR($iStyle, 0x3118C0)) ;this is unfortunately not working properly thus the GUIRegisterMsg() below
                GUICtrlSetData($State, "Unlock")
                _WinAPI_RedrawWindow($hGUI)
                $sData = GUICtrlRead($Data)
                GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')
                $Set = 1
            ElseIf $Set = 1 Then
                _WinAPI_SetWindowLong($hData, $GWL_STYLE ,  $iStyle)
                GUICtrlSetData($State, "Lock")
                _WinAPI_RedrawWindow($hGUI)
                GUIRegisterMsg($WM_COMMAND, '')
                $Set = 0
            Else
            ;    _Error()
                Exit
            EndIf
    EndSwitch
WEnd

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Switch BitAND($wParam, 0x0000FFFF)
        Case $Data
            GUICtrlSetData($Data, $sData)
    EndSwitch
    Return "GUI_RUNDEFMSG"
EndFunc

Or even easier:

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

Global $Data, $Label, $hGUI, $hData, $State, $Set

$hGUI = GUICreate("Example", 500, 500)
$Data = GUICtrlCreateEdit("Write something in here and hit lock.", 0, 0, 500, 480)
$State = GUICtrlCreateButton("Lock", 0, 480, 500, 20)
GUISetState(@SW_SHOW)
$Set = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $State
            If $Set = 0 Then
                GUICtrlSetState($Data, $GUI_DISABLE)
                GUICtrlSetData($State, "Unlock")
                $Set = 1
            ElseIf $Set = 1 Then
                GUICtrlSetState($Data, $GUI_ENABLE)
                GUICtrlSetData($State, "Lock")
                $Set = 0
            Else
            ;    _Error()
                Exit
            EndIf
    EndSwitch
WEnd

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

this is unfortunately not working properly thus the GUIRegisterMsg() below

:huh:

What wrong with GUICtrlSetStyle($Data, BitOR($iStyle, $ES_READONLY))?

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

$hGUI = GUICreate("Example", 500, 500)
$iData = GUICtrlCreateEdit("Write something in here and hit lock.", 0, 0, 500, 480)
$iState = GUICtrlCreateButton("Lock", 0, 480, 500, 20)

GUISetState(@SW_SHOW)

$iSet = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $iState
            Local $aData[2][2] = [[$ES_READONLY, 0], ["Unlock", "Lock"]]

            GUICtrlSetStyle($iData, BitOR($GUI_SS_DEFAULT_EDIT, $aData[0][$iSet]))
            GUICtrlSetData($iState, $aData[1][$iSet])

            $iSet = Number(Not $iSet)
    EndSwitch
WEnd
Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Thanks MrCreatoR, I searched for something like GUICtrlSetStyle() but found first _WinAPI_SetWindowLong()...

Here the code with GUICtrlSetStyle():

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

Global $Data, $Label, $sData, $iExStyle, $iStyle, $hGUI, $hData, $State, $Set

$hGUI = GUICreate("Example", 500, 500)
$Data = GUICtrlCreateEdit("Write something in here and hit lock.", 0, 0, 500, 480)
$hData = GUICtrlGetHandle($Data)
$State = GUICtrlCreateButton("Lock", 0, 480, 500, 20)
GUISetState(@SW_SHOW)

$iStyle = _WinAPI_GetWindowLong($hData, $GWL_STYLE )
$iExStyle = _WinAPI_GetWindowLong($hData, $GWL_EXSTYLE)
$Set = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $State
            If $Set = 0 Then
                GUICtrlSetStyle($Data, $iStyle + $ES_READONLY)
                GUICtrlSetData($State, "Unlock")
                $sData = GUICtrlRead($Data)
                $Set = 1
            ElseIf $Set = 1 Then
                GUICtrlSetStyle($Data, $iStyle)
                GUICtrlSetData($State, "Lock")
                $Set = 0
            Else
            ;    _Error()
                Exit
            EndIf
    EndSwitch
WEnd

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@skellysoul

Thank you for raising this subject it's been interesting and informative seeing everyones approach to the task.

@MrCreatoR

My favourite was your solution, in fact I think it is so good that it should be placed into the Snippets section.

EDIT: (To clarify which one, )

Thanks @all

Regards,

DeMo.

Edited by GerrOrneq

Quote of the week:"BASIC programmers never die, they GOSUB and don't RETURN." -- UnknownWisdom of the ages:

  

  • I'd be unstoppable... if not for law enforcement and physics.
  • Marriage, the number 1 cause of divorce.
  • Don't steal... the government hates competition.
  • Irish Government Motto: We’ve got what it takes to take what you’ve got.
  • Birthdays are good for you. Statistics show that the people who have the most live the longest.
  • Failure is not an option. It comes bundled with your Microsoft product.-- Ferenc Mantfeld
  • If you learn from your mistakes, then why ain't I a genius?! -- Anonymous
  • Remember, live every day as if it was your last day! one day you will be right.
  • How is it one careless match can start a forest fire, but it takes a whole box to start a campfire?
  • Sure my system is secure, it just locked up again.
  • I haven't lost my mind; I have a tape back-up somewhere.  ~Author Unknown
Link to comment
Share on other sites

@MrCreatoR

My favourite was your solution, in fact I think it is so good that it should be placed into the Snippets section.

EDIT: (To clarify which one, )

It's need more polishing, for example, while you select the editable text and drag the mouse outside the control borders the edit is done.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Here it's in UDF style:

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

Global $hEL_Wnd, $iEL_Label, $iEL_Edit

$hGUI = GUICreate("Example", 500, 100)

$aEditLabel = _EditableLabel_Create($hGUI, "Some data, left click (and wait few moments) to edit.", 20, 20, 460, 30)

Dim $aAccellKeys[1][2] = [['{ESC}', $aEditLabel[1]]]
GUISetAccelerators($aAccellKeys, $hGUI)

GUISetState(@SW_SHOW)

While 1
    $iMsg = GUIGetMsg()
    
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aEditLabel[0]
            _EditableLabel_Show($aEditLabel)
        Case $aEditLabel[1]
            _EditableLabel_Hide($aEditLabel, False)
    EndSwitch
WEnd

Func _EditableLabel_Create($hWnd, $sData, $iLeft, $iTop, $iWidth = -1, $iHeight = -1)
    Local $aRet[2]
    
    $aRet[0] = GUICtrlCreateLabel($sData, $iLeft, $iTop, $iWidth, $iHeight)
    $aRet[1] = GUICtrlCreateEdit($sData, $iLeft, $iTop, $iWidth + 5, $iHeight + 5, BitOR($ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
    GUICtrlSetState($aRet[1], $GUI_HIDE)
    
    $hEL_Wnd = $hWnd
    
    Return $aRet
EndFunc

Func _EditableLabel_Show($aEditLabel = 0)
    If @NumParams > 0 Then
        $iEL_Label = $aEditLabel[0]
        $iEL_Edit = $aEditLabel[1]
        AdlibRegister('_EditableLabel_Show', 800)
        Return
    EndIf
    
    AdlibUnRegister('_EditableLabel_Show')
    GUIRegisterMsg($WM_LBUTTONUP, '__EL_WM_LBUTTONUP')
    
    Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
    Local $aWinPos = WinGetClientSize($hEL_Wnd)
    
    If ($aPos[1] + ($aPos[3] - 10)) > $aWinPos[1] Then
        $aPos[3] = $aWinPos[1] - ($aPos[1] + 10)
    EndIf
    
    GUICtrlSetData($iEL_Edit, GUICtrlRead($iEL_Label))
    GUICtrlSetPos($iEL_Edit, $aPos[0], $aPos[1], $aPos[2] + 5, $aPos[3] + 5)
    
    GUICtrlSetState($iEL_Edit, $GUI_SHOW)
    GUICtrlSetState($iEL_Label, $GUI_HIDE)
EndFunc

Func _EditableLabel_Hide($aEditLabel, $bUpdate = True)
    GUIRegisterMsg($WM_LBUTTONUP, '')
    
    If IsArray($aEditLabel) Then
        $iEL_Label = $aEditLabel[0]
        $iEL_Edit = $aEditLabel[1]
    EndIf
    
    GUICtrlSetState($iEL_Edit, $GUI_HIDE)
    GUICtrlSetState($iEL_Label, $GUI_SHOW)
    
    If $bUpdate Then
        Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
        Local $sData = GUICtrlRead($iEL_Edit)
        
        StringReplace($sData, @CRLF, '')
        Local $iLines = @extended - 1
        
        GUICtrlSetData($iEL_Label, $sData)
        GUICtrlSetPos($iEL_Label, $aPos[0], $aPos[1], $aPos[2], $aPos[3] + (22 * $iLines))
    EndIf
EndFunc

Func __EL_WM_LBUTTONUP($hWnd, $iMsg, $wParam, $lParam)
    _EditableLabel_Hide(0, True)
EndFunc
Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 2 weeks later...

Very nice feature to edit labels.

I had some issues with the above example in UDF style though. When there is only one line on the label @extended returns 0 and so the label is made one line (22) smaller. For example by clicking on the text and then outside the label without changing anything. Also the label doesn't resize back when lines are removed. I also would like that style/exstyle parameters can be set on the label.

This code works for me (v3.3.8.1)

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

Global $hEL_Wnd, $iEL_Label, $iEL_Edit
$hGUI = GUICreate("Example", 500, 200)
$aEditLabel = _EditableLabel_Create($hGUI, "Some data, left click (and wait few moments) to edit.", 20, 20, 280, 22,-1,$WS_EX_DLGMODALFRAME)
Dim $aAccellKeys[1][2] = [['{ESC}', $aEditLabel[1]]]
GUISetAccelerators($aAccellKeys, $hGUI)
GUISetState(@SW_SHOW)
While 1
$iMsg = GUIGetMsg()
Switch $iMsg
     Case $GUI_EVENT_CLOSE
         Exit
     Case $aEditLabel[0]
         _EditableLabel_Show($aEditLabel)
     Case $aEditLabel[1]
         _EditableLabel_Hide($aEditLabel, False)
EndSwitch
WEnd
Func _EditableLabel_Create($hWnd, $sData, $iLeft, $iTop, $iWidth = -1, $iHeight = -1,$sStyle= -1, $sExStyle =-1)
Local $aRet[2]
$aRet[0] = GUICtrlCreateLabel($sData, $iLeft, $iTop, $iWidth, $iHeight, $sStyle, $sExStyle)
$aRet[1] = GUICtrlCreateEdit($sData, $iLeft, $iTop, $iWidth + 5, $iHeight + 5, BitOR($ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
GUICtrlSetState($aRet[1], $GUI_HIDE)
$hEL_Wnd = $hWnd
Return $aRet
EndFunc
Func _EditableLabel_Show($aEditLabel = 0)
If @NumParams > 0 Then
     $iEL_Label = $aEditLabel[0]
     $iEL_Edit = $aEditLabel[1]
     AdlibRegister('_EditableLabel_Show', 800)
     Return
EndIf
AdlibUnRegister('_EditableLabel_Show')
GUIRegisterMsg($WM_LBUTTONUP, '__EL_WM_LBUTTONUP')
Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
Local $aWinPos = WinGetClientSize($hEL_Wnd)
If ($aPos[1] + ($aPos[3] - 10)) > $aWinPos[1] Then
     $aPos[3] = $aWinPos[1] - ($aPos[1] + 10)
EndIf
GUICtrlSetData($iEL_Edit, GUICtrlRead($iEL_Label))
GUICtrlSetPos($iEL_Edit, $aPos[0], $aPos[1], $aPos[2], $aPos[3])
GUICtrlSetState($iEL_Edit, $GUI_SHOW)
GUICtrlSetState($iEL_Label, $GUI_HIDE)
EndFunc
Func _EditableLabel_Hide($aEditLabel, $bUpdate = True)
GUIRegisterMsg($WM_LBUTTONUP, '')
If IsArray($aEditLabel) Then
     $iEL_Label = $aEditLabel[0]
     $iEL_Edit = $aEditLabel[1]
EndIf
GUICtrlSetState($iEL_Edit, $GUI_HIDE)
GUICtrlSetState($iEL_Label, $GUI_SHOW)
If $bUpdate Then
     Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
     Local $sData = GUICtrlRead($iEL_Edit)
     StringReplace($sData, @CRLF, '')
     Local $iLines = @extended + 1
     GUICtrlSetData($iEL_Label, $sData)
     GUICtrlSetPos($iEL_Label, $aPos[0], $aPos[1], $aPos[2], 22 * $iLines)
EndIf
EndFunc
Func __EL_WM_LBUTTONUP($hWnd, $iMsg, $wParam, $lParam)
_EditableLabel_Hide(0, True)
EndFunc

I also mixed the UDF with an example of Melba23 so the label can be dragged as well:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
; Mouse coords relative to GUI client area
Opt("MouseCoordMode", 2)
Global $hEL_Wnd, $iEL_Label, $iEL_Edit,$fDblClk
; Create GUI
$hGUI = GUICreate("Left click to drag/Right click to edit", 300, 200)
$cLabel = _EditableLabel_Create($hGUI, "Flexible label", 10, 150, 100, 22)
GUICtrlSetBkColor(-1, 0xD4D0C8)
;Sets the accelerator table
Dim $aAccellKeys[1][2] = [['{ESC}', $cLabel[1]]]
GUISetAccelerators($aAccellKeys, $hGUI)
GUISetState()

While 1
Switch GUIGetMsg()
     Case $GUI_EVENT_CLOSE
         Exit
Case $GUI_EVENT_SECONDARYUP
         $cInfo = GUIGetCursorInfo($hGUI)
         $iControl = $cInfo[4]
         If $iControl = $cLabel[0] then _EditableLabel_Show($cLabel)
     Case $GUI_EVENT_PRIMARYDOWN
         $cInfo = GUIGetCursorInfo($hGUI)
         $iControl = $cInfo[4]
         Switch $iControl
             Case $cLabel[0]
                 $bPos = ControlGetPos($hGUI, "", $iControl)
                 $iSubtractX = $cInfo[0] - $bPos[0]
                 $iSubtractY = $cInfo[1] - $bPos[1]
                 ; Move the control until the mouse button is released
                 Do
                     $cInfo = GUIGetCursorInfo($hGUI)
                     ControlMove($hGUI, "", $iControl, $cInfo[0] - $iSubtractX, $cInfo[1] - $iSubtractY)
                 Until Not $cInfo[2]
EndSwitch
     Case $cLabel[1]
         _EditableLabel_Hide($cLabel, False)
EndSwitch
WEnd
Func _EditableLabel_Create($hWnd, $sData, $iLeft, $iTop, $iWidth = -1, $iHeight = -1,$sStyle= -1, $sExStyle =-1)
Local $aRet[2]
$aRet[0] = GUICtrlCreateLabel($sData, $iLeft, $iTop, $iWidth, $iHeight, $sStyle, $sExStyle)
$aRet[1] = GUICtrlCreateEdit($sData, $iLeft, $iTop, $iWidth + 5, $iHeight + 5, BitOR($ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
GUICtrlSetState($aRet[1], $GUI_HIDE)
$hEL_Wnd = $hWnd
Return $aRet
EndFunc
Func _EditableLabel_Show($aEditLabel = 0)
If @NumParams > 0 Then
     $iEL_Label = $aEditLabel[0]
     $iEL_Edit = $aEditLabel[1]
     AdlibRegister('_EditableLabel_Show', 800)
     Return
EndIf
AdlibUnRegister('_EditableLabel_Show')
GUIRegisterMsg($WM_LBUTTONUP, '__EL_WM_LBUTTONUP')
Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
Local $aWinPos = WinGetClientSize($hEL_Wnd)
If ($aPos[1] + ($aPos[3] - 10)) > $aWinPos[1] Then
     $aPos[3] = $aWinPos[1] - ($aPos[1] + 10)
EndIf
GUICtrlSetData($iEL_Edit, GUICtrlRead($iEL_Label))
GUICtrlSetPos($iEL_Edit, $aPos[0], $aPos[1], $aPos[2], $aPos[3])
GUICtrlSetState($iEL_Edit, $GUI_SHOW)
GUICtrlSetState($iEL_Label, $GUI_HIDE)
EndFunc
Func _EditableLabel_Hide($aEditLabel, $bUpdate = True)
GUIRegisterMsg($WM_LBUTTONUP, '')
If IsArray($aEditLabel) Then
     $iEL_Label = $aEditLabel[0]
     $iEL_Edit = $aEditLabel[1]
EndIf
GUICtrlSetState($iEL_Edit, $GUI_HIDE)
GUICtrlSetState($iEL_Label, $GUI_SHOW)
If $bUpdate Then
     Local $aPos = ControlGetPos($hEL_Wnd, '', $iEL_Label)
     Local $sData = GUICtrlRead($iEL_Edit)
     StringReplace($sData, @CRLF, '')
     Local $iLines = @extended + 1
     GUICtrlSetData($iEL_Label, $sData)
     GUICtrlSetPos($iEL_Label, $aPos[0], $aPos[1], $aPos[2], 22 * $iLines)
EndIf
EndFunc
Func __EL_WM_LBUTTONUP($hWnd, $iMsg, $wParam, $lParam)
_EditableLabel_Hide(0, True)
EndFunc

This works quite ok, though I think it would be nicer to just use GUICtrlCreateLabel and set the style to $SS_DRAGANDDROP and $SS_EDIT.

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...