此函数用于获取AHKEditor或者SciTE编辑框里的选中内容

 

F1::MsgBox % GetSelectedTextScintilla()


GetSelectedTextScintilla(WinTitle="ahk_class AHKEditor") {
  ControlGetText, fullText, Scintilla1, %WinTitle%
  if ErrorLevel
    return
  ControlGet, ControlHWND, Hwnd, , Scintilla1, %WinTitle%

  fullText .= " "  ; 确保能获取最后一个字
  , byteStart := DllCall("SendMessage", "Ptr", ControlHWND, "UInt", SCI_GETSELECTIONSTART := 2143, "Ptr", 0, "Ptr", 0)
  , byteEnd := DllCall("SendMessage", "Ptr", ControlHWND, "UInt", SCI_GETSELECTIONEND := 2145, "Ptr", 0, "Ptr", 0)
  , VarSetCapacity(utf8Text, StrPut(fullText, "UTF-8"))
  , StrPut(fullText, &utf8Text, "UTF-8")
  , charPos := 1, bytePos := 0, charStart := 0, charEnd := 0

  Loop, Parse, fullText
  {
    if (!charStart && bytePos >= byteStart)
      charStart := charPos

    if (!charEnd && bytePos >= byteEnd) {
      charEnd := charPos
      break
    }
    bytePos += StrPut(A_LoopField, "UTF-8") - 1
    , charPos++
  }

  if (charStart && charEnd > charStart)
    return SubStr(fullText, charStart, charEnd - charStart)
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。