给个示例参考
程序代码:
** #define WM_USER 0x400
** #define WM_SYSTEMTRAY WM_USER + 1001
** #define IMAGE_ICON 1
** #define LR_LOADFROMFILE 0x0010
** NIM_ADD equ 0h
** NIM_MODIFY equ 1h
** NIM_DELETE equ 2h
** NIF_MESSAGE equ 1h
** NIF_ICON equ 2h
** NIF_TIP equ 4h
** NIF_ICON | NIF_MESSAGE | NIF_TIP equ 7h
DECLARE LONG Shell_NotifyIcon IN Shell32 LONG, STRING@
DECLARE LONG LoadImage IN User32 LONG, STRING@, LONG, LONG, LONG, LONG
DECLARE LONG CreatePopupMenu IN User32
DECLARE LONG AppendMenu IN User32 LONG, LONG, LONG, STRING@
DECLARE LONG TrackPopupMenu IN User32 LONG, LONG, LONG, LONG, LONG, LONG, LONG
DECLARE LONG GetCursorPos IN User32 STRING@
DECLARE LONG PostMessage IN User32 LONG, LONG, LONG, LONG
DECLARE LONG SendMessage IN User32 LONG, LONG, LONG, LONG
DECLARE LONG SetForegroundWindow IN User32 LONG
_screen.Visible = .F.
of = CREATEOBJECT("form1")
of.show
READ EVENTS
CLEAR ALL
RETURN
DEFINE CLASS form1 as Form
ShowWindow = 2
ShowInTaskbar = .F.
AutoCenter = .T.
hLMenu = 0
hRMenu = 0 && 如果不是分配给一个窗口,调用DestroyMenu来释放菜单资源。
ADD OBJECT edit1 as editbox WITH top=10,left=10,width=this.Width-20,height=this.Height-50
PROCEDURE Unload
LOCAL stData
stData = BINTOC(88, "4RS");
+ BINTOC(this.hWnd, "4RS");
+ BINTOC(101, "4RS");
+ REPLICATE(0h00, 76)
Shell_NotifyIcon(2, @stData)
CLEAR EVENTS
ENDPROC
PROCEDURE Init
BINDEVENT(this.hWnd, 0x401, this, "myNotifyEvent") && 处理自定义消息(0x401)
BINDEVENT(this.hWnd, 0x111, this, "myCommand") && 处理系统消息(WM_COMMAND 0x111)
this.Show
PostMessage(this.hWnd, 0x111, 0x102, 0) && 这时用异步消息才可以隐藏窗口
myMenu(this) && 窗口菜单
this.hLMenu = CreatePopupMenu() && 托盘菜单
AppendMenu(this.hLMenu, 0, 0x101, "显示窗口-左")
AppendMenu(this.hLMenu, 0, 0x102, "隐藏窗口-左")
AppendMenu(this.hLMenu, 0, 0x103, "关闭窗口-左")
AppendMenu(this.hLMenu, 0, 0x104, "自己家的-左")
this.hRMenu = CreatePopupMenu()
AppendMenu(this.hRMenu, 0, 0x201, "显示窗口-右")
AppendMenu(this.hRMenu, 0, 0x202, "隐藏窗口-右")
AppendMenu(this.hRMenu, 0, 0x203, "关闭窗口-右")
AppendMenu(this.hRMenu, 0, 0x204, "自己家的-右")
LOCAL stData
stData = BINTOC(88, "4RS");
+ BINTOC(this.hWnd, "4RS");
+ BINTOC(101, "4RS");
+ BINTOC(7, "4RS");
+ BINTOC(0x401, "4RS");
+ BINTOC(LoadImage(0, "vfp.ico", 1, 0, 0, 0x10), "4RS");
+ "点击弹出菜单" + REPLICATE(0h00, 50)
Shell_NotifyIcon(0, @stData)
ENDPROC
PROCEDURE myNotifyEvent(hWnd, wMsg, wParam, lParam)
IF BETWEEN(lparam, 0x201, 0x204) && 左点击或右点击
LOCAL stPOINT, nX, nY
stPOINT = REPLICATE(0h00, 8)
GetCursorPos(@stPOINT)
nX = CTOBIN(LEFT(stPOINT, 4), "4RS")
nY = CTOBIN(RIGHT(stPOINT, 4), "4RS")
SetForegroundWindow(hWnd) && 设置前台窗口,这样点击其他地方时弹出菜单会自动消失。
TrackPopupMenu(IIF(lparam == 0x201, this.hLMenu, this.hRMenu), 2, nX, nY, 0, hWnd, 0)
ENDIF
ENDPROC
PROCEDURE myCommand(hWnd, wMsg, wParam, lParam)
DO CASE
CASE wParam == 0x101
this.Visible = .T.
CASE wParam == 0x102
this.Visible = .F.
CASE wParam == 0x103
this.Release
CASE wParam == 0x104
MESSAGEBOX("自己家的-左")
CASE wParam == 0x201
this.Visible = .T.
CASE wParam == 0x202
this.Visible = .F.
CASE wParam == 0x203
this.Release
CASE wParam == 0x204
MESSAGEBOX("自己家的-右")
OTHERWISE
SendMessage(hWnd, wMsg, wParam, lParam) && 消息反弹
ENDCASE
ENDPROC
ENDDEFINE
FUNCTION myMenu(oThis)
DEFINE MENU menu1 BAR AT LINE 0 IN (oThis.name)
DEFINE PAD pad1 OF menu1 PROMPT "pad1"
DEFINE PAD pad2 OF menu1 PROMPT "pad2"
ON PAD pad1 OF menu1 ACTIVATE POPUP pop1
ON PAD pad2 OF menu1 ACTIVATE POPUP pop2
DEFINE POPUP pop1
DEFINE BAR 1 OF pop1 PROMPT "pop1_bar1"
DEFINE BAR 2 OF pop1 PROMPT "pop1_bar2"
ON SELECTION POPUP pop1 selpop(POPUP(), PROMPT())
DEFINE POPUP pop2
DEFINE BAR 1 OF pop2 PROMPT "pop2_bar1"
DEFINE BAR 2 OF pop2 PROMPT "pop2_bar2"
ON SELECTION POPUP pop2 selpop(POPUP(), PROMPT())
ACTIVATE MENU menu1
ENDFUNC
FUNCTION selpop(cPopup, cPrompt)
MESSAGEBOX(cPopup + "---" + cPrompt)
ENDFUNC