建立右键菜单 另一版.ahk

; 以记事本为示例生效
; https://www.autohotkey.com/boards/viewtopic.php?f=5&t=27236

F1::
if !仅第一次生成菜单 {
  Menu, EditMenu, Add, &My Item, MyItem
  Menu, EditMenu, Add ;------------------------------
  Menu, EditMenu, Add, &Undo, EditUndo
  Menu, EditMenu, Add ;------------------------------
  Menu, EditMenu, Add, Cu&t, EditCut
  Menu, EditMenu, Add, &Copy, EditCopy
  Menu, EditMenu, Add, &Paste, EditPaste
  Menu, EditMenu, Add, &Delete, EditDelete
  Menu, EditMenu, Add ;------------------------------
  Menu, EditMenu, Add, Select &All, EditSelectAll
  仅第一次生成菜单 := 1
}

WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
WinGetClass, vWinClass, % "ahk_id " hCtl
if !(vWinClass = "Edit") ; 注释这句可以继续往下走
  return

; Undo - check undo status (is undo available)
; Cut - check text selection > 0
; Copy - check text selection > 0
; Paste - check clipboard not empty
; Delete - check text selection > 0
; Select All - always available

SendMessage, 0xC6, 0, 0,, % "ahk_id " hCtl ;EM_CANUNDO := 0xC6
vOptU := ErrorLevel ? "En" : "Dis" ;1=undo available/0=undo not available
ControlGet, vText, Selected,,, % "ahk_id " hCtl
vOptT := StrLen(vText) ? "En" : "Dis"
vOptC := StrLen(Clipboard) ? "En" : "Dis"

Menu, EditMenu, % vOptU "able", &Undo, EditUndo
Menu, EditMenu, % vOptT "able", Cu&t, EditCut
Menu, EditMenu, % vOptT "able", &Copy, EditCopy
Menu, EditMenu, % vOptC "able", &Paste, EditPaste
Menu, EditMenu, % vOptT "able", &Delete, EditDelete

Menu, EditMenu, Show
return


MyItem:
MsgBox, % "hello world!"
return


EditUndo:
SendMessage, 0x304,,,, % "ahk_id " hCtl ;WM_UNDO := 0x304
return

EditCut:
SendMessage, 0x300,,,, % "ahk_id " hCtl ;WM_CUT := 0x300
return

EditCopy:
SendMessage, 0x301,,,, % "ahk_id " hCtl ;WM_COPY := 0x301
return

EditPaste:
SendMessage, 0x302,,,, % "ahk_id " hCtl ;WM_PASTE := 0x302
return

EditDelete:
SendMessage, 0x303,,,, % "ahk_id " hCtl ;WM_CLEAR := 0x303
return

EditSelectAll:
SendMessage, 0xB1, 0, -1,, % "ahk_id " hCtl ;EM_SETSEL := 0xB1
return

 

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