Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active August 29, 2015 14:18
Show Gist options
  • Save tmplinshi/cd648cc37a8064c4680b to your computer and use it in GitHub Desktop.
Save tmplinshi/cd648cc37a8064c4680b to your computer and use it in GitHub Desktop.
; Menu_SetSysMenu.ahk
/*
__ __ __ __ __ __ __ _ __
/ /_ / /_/ /_____ _ / // /____ _/ /_ / /________________(_)___ / /_ ____ _______
/ __ \/ __/ __/ __ \(_) // // __ '/ __ \/ //_/ ___/ ___/ __/ / __ \/ __// __ \/ __/ _ \
/ / / / /_/ /_/ /_/ / / // // /_/ / / / / ,< (__ ) /__/ / / / /_/ / /__/ /_/ / / / // /
/_/ /_/\__/\__/ .___(_) // / \__,_/_/ /_/_/|_/____/\___/_/ /_/ .___/\__(_)____/_/ \__ /
/_/ /_//_/ /_/ (___/
Script : System Menu : Add custom and/or remove standard items
Author : SKAN ( arian.suresh@gmail.com ), Created: 19-Dec-2013
Topic : http://ahkscript.org/boards/viewtopic.php?p=7630#p7630
Modified by: tmplinshi
*/
/*
; 示例
Gui, +HwndHGUI ; 指定界面的句柄
SysMenuItems := "
(LTrim Comments
----------------------------- ; 菜单中的分隔符
窗口置顶, 2015, gui_AlwaysOnTop, Checked1
; 格式 ---> 菜单名称, 菜单ID, 点击菜单执行的标签或者函数, 勾选(可以为 Checked1 或者 Checked0)
)"
Menu_SetSysMenu( HGUI, SysMenuItems, "Reset" )
*/
Menu_SetSysMenu(p*) {
SystemMenu.Menu_SetSysMenu(p*)
}
Class SystemMenu {
static _ := OnMessage( 0x112, "SystemMenu.WM_SYSCOMMAND" )
static menu_func := {}
WM_SYSCOMMAND( lParam, Msg, hWnd ) { ; WM_SYSCOMMAND() goo.gl/kDL6uM
hSysMenu := SystemMenu.Menu_SetSysMenu( hWnd ) ; Get a Handle to SYSMENU
wParam := this
If SystemMenu.menu_func[wParam].checked
SystemMenu.Menu_CheckItem( hSysMenu, wParam, False, -1 ) ; Toggle Checkmark
If SystemMenu.menu_func.HasKey(wParam)
SetTimer, % SystemMenu.menu_func[wParam].fn, -1
}
Menu_CheckItem( hMenu, ItemRef=0, ByPos=1, CheckState=1 ) {
Static MF_BYCOMMAND := 0, MF_BYPOSITION := 0x400, MF_CHECKED := 0x8, MF_UNCHECKED := 0
Flag := ByPos ? MF_BYPOSITION : MF_BYCOMMAND,
ItemRef := ItemRef - ( ByPos ? 1 : 0 )
Flag |= ( !CheckState ) ? MF_UNCHECKED ; GetMenuState() goo.gl/SjVKK8
: ( CheckState > 0 ) ? MF_CHECKED
: ( DllCall( "GetMenuState", UInt,hMenu, UInt,ItemRef, UInt,Flag ) & MF_CHECKED
? MF_UNCHECKED : MF_CHECKED )
Return DllCall( "CheckMenuItem", UInt,hMenu, UInt,ItemRef, UInt,Flag ) ; goo.gl/L4FlQy
}
Menu_GetState( hMenu, ItemRef=0, ByPos=1, MF=0x8 ) {
Static MF_BYCOMMAND := 0, MF_BYPOSITION := 0x400, MF_POPUP := 0x10
Flag := ByPos ? MF_BYPOSITION : MF_BYCOMMAND
ItemRef := ItemRef - ( ByPos ? 1 : 0 )
R := DllCall( "GetMenuState", UInt,hMenu, UInt,ItemRef, UInt,Flag ) ; goo.gl/PdRLR9
Return ( MF=MF_POPUP ? ( R&16 ? R>>8 : 0 ) : MF>0 ? ( R & MF = MF ) : R )
}
Menu_SetSysMenu( hWnd, AddItems="", Options="" ) {
Static MF_MENUBARBREAK := 0x20, MF_SEPARATOR := 0x800, MF_STRING := 0 ; goo.gl/ggTuwF
Static SWP_Flag := 0x33 ; SWP_DRAWFRAME|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE goo.gl/sah2Dm
InStr( Options, "Reset" ) ? DllCall( "GetSystemMenu", UInt,hWnd, UInt,True )
hMenu := DllCall( "GetSystemMenu", UInt,hWnd, UInt,0 ) ; GetSystemMenu() goo.gl/cfW40p
InStr( Options, "-Close" ) ? DllCall( "RemoveMenu", UInt,hMenu, UInt,0xF060, UInt,0 )
InStr( Options, "-Move" ) ? DllCall( "RemoveMenu", UInt,hMenu, UInt,0xF010, UInt,0 )
; RemoveMenu() goo.gl/KzP0Yg
Loop, Parse, AddItems,`n, `r`t%A_Space%
{
Item := A_LoopField, F1 := "", F2 := 0
F3 := "", F4 := ""
Loop, Parse, Item, CSV, %A_Space%`t
F%A_Index% := A_LoopField
DllCall( "AppendMenu", UInt, hMenu ; AppendMenu() goo.gl/ggTuwF
, UInt, !F2 ? MF_SEPARATOR : F2>>16 ? MF_MENUBARBREAK : MF_STRING
, UInt, F2 & 0xFFFF
, UInt, F2 ? &F1 : F2 )
If RegExMatch(F4, "i)Checked\K\d", CheckState) {
this.Menu_CheckItem( hMenu, F2, False, CheckState )
this.menu_func[F2, "Checked"] := True
}
this.menu_func[F2, "fn"] := F3
}
If InStr( Options, "Redraw" ) ; SetWindowPos() goo.gl/sah2Dm
DllCall( "SetWindowPos", UInt,hWnd, Int,0, Int,0, Int,0, Int,0, Int,0, UInt,SWP_Flag )
Return hMenu, DllCall( "SetLastError", UInt,DllCall( "GetMenuItemCount", UInt,hMenu ) )
}
}
Gui +HwndHGUI
SysMenuItems := "
(LTrim
-----------------------------
item 1, 1234, test_function
item 2, 2222, test_lable
)"
Menu_SetSysMenu( HGUI, SysMenuItems, "Reset" )
Gui, Show, W400 h300
Return
test_function() {
MsgBox, % A_ThisFunc
}
test_lable:
MsgBox, % A_ThisLabel
Return
GuiClose:
ExitAPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment