Guest User

Untitled

a guest
Apr 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;************************
  2. ; Edit Control Functions
  3. ;************************
  4. ;
  5. ; Standard parameters:
  6. ;   Control, WinTitle   If WinTitle is not specified, 'Control' may be the
  7. ;                       unique ID (hwnd) of the control.  If "A" is specified
  8. ;                       in Control, the control with input focus is used.
  9. ;
  10. ; Standard/default return value:
  11. ;   true on success, otherwise false.
  12.  
  13.  
  14. Edit_Standard_Params(ByRef Control, ByRef WinTitle) {  ; Helper function.
  15.     if (Control="A" && WinTitle="") { ; Control is "A", use focused control.
  16.         ControlGetFocus, Control, A
  17.         WinTitle = A
  18.     } else if (Control+0!="" && WinTitle="") {  ; Control is numeric, assume its a ahk_id.
  19.         WinTitle := "ahk_id " . Control
  20.         Control =
  21.     }
  22. }
  23.  
  24. ; Returns true if text is selected, otherwise false.
  25. ;
  26. Edit_TextIsSelected(Control="", WinTitle="")
  27. {
  28.     Edit_Standard_Params(Control, WinTitle)
  29.     return Edit_GetSelection(start, end, Control, WinTitle) and (start!=end)
  30. }
  31.  
  32. ; Gets the start and end offset of the current selection.
  33. ;
  34. Edit_GetSelection(ByRef start, ByRef end, Control="", WinTitle="")
  35. {
  36.     Edit_Standard_Params(Control, WinTitle)
  37.     VarSetCapacity(start, 4), VarSetCapacity(end, 4)
  38.     SendMessage, 0xB0, &start, &end, %Control%, %WinTitle%  ; EM_GETSEL
  39.     if (ErrorLevel="FAIL")
  40.         return false
  41.     start := NumGet(start), end := NumGet(end)
  42.     return true
  43. }
  44.  
  45. ; Selects text in a text box, given absolute character positions (starting at 0.)
  46. ;
  47. ; start:    Starting character offset, or -1 to deselect.
  48. ; end:      Ending character offset, or -1 for "end of text."
  49. ;
  50. Edit_Select(start=0, end=-1, Control="", WinTitle="")
  51. {
  52.     Edit_Standard_Params(Control, WinTitle)
  53.     SendMessage, 0xB1, start, end, %Control%, %WinTitle%  ; EM_SETSEL
  54.     return (ErrorLevel != "FAIL")
  55. }
  56.  
  57. ; Selects a line of text.
  58. ;
  59. ; line:             One-based line number, or 0 to select the current line.
  60. ; include_newline:  Whether to also select the line terminator (`r`n).
  61. ;
  62. Edit_SelectLine(line=0, include_newline=false, Control="", WinTitle="")
  63. {
  64.     Edit_Standard_Params(Control, WinTitle)
  65.    
  66.     ControlGet, hwnd, Hwnd,, %Control%, %WinTitle%
  67.     if (!WinExist("ahk_id " hwnd))
  68.         return false
  69.    
  70.     if (line<1)
  71.         ControlGet, line, CurrentLine
  72.    
  73.     SendMessage, 0xBB, line-1, 0  ; EM_LINEINDEX
  74.     offset := ErrorLevel
  75.  
  76.     SendMessage, 0xC1, offset, 0  ; EM_LINELENGTH
  77.     lineLen := ErrorLevel
  78.  
  79.     if (include_newline) {
  80.         WinGetClass, class
  81.         lineLen += (class="Edit") ? 2 : 1 ; `r`n : `n
  82.     }
  83.    
  84.     ; Select the line.
  85.     SendMessage, 0xB1, offset, offset+lineLen  ; EM_SETSEL
  86.     return (ErrorLevel != "FAIL")
  87. }
  88.  
  89. ; Deletes a line of text.
  90. ;
  91. ; line:     One-based line number, or 0 to delete current line.
  92. ;
  93. Edit_DeleteLine(line=0, Control="", WinTitle="")
  94. {
  95.     Edit_Standard_Params(Control, WinTitle)
  96.     ; Select the line.
  97.     if (Edit_SelectLine(line, true, Control, WinTitle))
  98.     {   ; Delete it.
  99.         ControlSend, %Control%, {Delete}, %WinTitle%
  100.         return true
  101.     }
  102.     return false
  103. }
Add Comment
Please, Sign In to add comment