使用系统托盘图标

发布时间:2011年1月2日 作者:未知 查看次数:1628

使用系统托盘图标


 

;使用系统托盘图标,显示 Balloon Tooltip 的 MASM32 演示
;功能: 演示使用系统托盘图标,显示Balloon Tooltip
;作者: PurpleEndurer | 紫郢剑侠㊣ 于 广西河池
;日期 内容
;----------------------------------------------------------------------

 

以下是代码片段:

.386
.model flat, stdcall
option casemap: none

include \masm32\include\windows.inc

include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

;include \masm32\include\ole32.inc
;includelib \masm32\lib\ole32.lib

include \masm32\include\shell32.inc
includelib \masm32\lib\shell32.lib

include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

;include \masm32\include\advapi32.inc
;includelib \masm32\lib\advapi32.lib
;include \masm32\include\comdlg32.inc
;includelib \masm32\lib\comdlg32.lib

;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
; prototype
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
WinMain     PROTO :DWORD,:DWORD,:DWORD,:DWORD

;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
; const
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
WM_SHELLNOTIFY equ WM_USER+200
IDI_TRAY    equ 0

NOTIFYICONDATA50 struct
    cbSize  dword ?
    hwnd    HANDLE ?
    uID     dword ?
    uFlags  dword ?
    uCallbackMessage    dword ?
    hIcon   HANDLE ?
    szTip   db  128 dup(?)  ;Version 5.0为128个,以前为 64 个
    dwState dword ?     ;图标状态:NIS_HIDDEN:图标是隐藏的;NIS_SHAREDICON:图标是共享的
    dwStateMask dword ? ;指明dwState的哪些位可以被读取,如设置为NIS_HIDDEN则表示图标的隐藏状态可以被读取
    szInfo  byte 256 dup(?)     ;气泡提示内容
  union
      uTimeout  dd  ?  ;Union类型系统默认设置最短10秒,最长30秒
      uVersion dd ?
  ends
    szInfoTitle byte 64 dup(?)  ;气泡提示标题
    dwInfoFlags dword ?         ;指明是否在气泡提示上显示图标
NOTIFYICONDATA50 ends
 
NOTIFYICON_VERSION = 3
NOTIFYICON_OLDVERSION = 0

NIM_ADD     = 0
NIM_MODIFY  = 1
NIM_DELETE  = 2
NIM_SETFOCUS    = 3
NIM_SETVERSION  = 4

NIF_MESSAGE = 1 ;uCallbackMessage项包含了有效的信息
NIF_ICON    = 2 ;hIcon项包含了有效的信息
NIF_TIP     = 4 ;szTip项包含了有效的信息
NIF_STATE   = 8
NIF_INFO    = 010h
 
NIS_HIDDEN  = 1
NIS_SHAREDICON = 2

;/---气泡提示信息图标控制
NIIF_NONE   = 0 ;不显示图标
NIIF_INFO   = 1 ;“信息”图标(蓝色)
NIIF_WARNING= 2 ;“警告”图标(黄色)
NIIF_ERROR  = 3 ;“错误”图标(红色)
;---/

NIN_SELECT  =   WM_USER   +   0;  
NIN_BALLOONSHOW = WM_USER + 2   ;当气泡提示显示后外壳发送此消息
NIN_BALLOONHIDE = WM_USER + 3   ;当气泡提示消失时(比如通知栏图标被删除)外壳发送此消息,但气泡提示由于超时而消失不会产生此消息
NIN_BALLOONTIMEOUT = WM_USER + 4    ;当气泡提示由于超时而消失时外壳发送此消息
NIN_BALLOONUSERCLICK = WM_USER + 5  ;当用户点击鼠标时(点击气泡提示和通知栏图标均可)外壳发送此消息
NINF_KEY    =   1
NIN_KEYSELECT   =   NIN_SELECT or NINF_KEY

IDC_ckbShowTrayIcon equ 100
IDC_ckbShowBalloon  equ 110
c_AppQuit           equ 1b
c_IconShowed        equ 10b

;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
.data
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
g_szClassName label byte
g_szAppName     db "Balloon_Tooltip_Demo", 0
g_szButton      db "button", 0
g_szShowTrayIcon db "显示托盘图标", 0
g_szShowBalloon  db "显示气泡信息",
0

g_szAppQuit     db "程序退出", 0
g_szInfo        db "这是一个提示信息内容!", 0
g_szInfoTitle   db "这是一个提示信息标题!", 0
g_szClickTrayIcon db "你击中了系统托盘区的图标", 0
g_szClickBalloonOrIcon db "你击中了气泡或系统托盘区的图标", 0
g_szMessageHide db "信息隐藏了", 0
g_szShowMessageNow db "开始显示信息了", 0
g_szMessageTimeOut  db "信息显示时间到了", 0

g_dwFlag    dword 0

;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
.data?
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
g_hInstance  HINSTANCE ?
g_hWndMain   HWND ?
g_stNoteIcon NOTIFYICONDATA50 <>

.code
start:

invoke GetModuleHandle, NULL
mov g_hInstance, eax
invoke WinMain, g_hInstance, NULL, NULL, SW_SHOWDEFAULT
invoke ExitProcess, eax

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD
    LOCAL wc: WNDCLASSEX
    LOCAL msg: MSG
    LOCAL hwnd: HWND

    mov   wc.cbSize, SIZEOF WNDCLASSEX
    mov   wc.style, CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra, NULL
    mov   wc.cbWndExtra, NULL
    mov   eax, hInst
    mov   wc.hInstance, eax
    mov   wc.hbrBackground, COLOR_BTNSHADOW
    mov   wc.lpszMenuName, NULL
    mov   wc.lpszClassName, OFFSET g_szClassName
    invoke LoadIcon, NULL, IDI_APPLICATION
    mov   wc.hIcon, eax
    mov   wc.hIconSm, eax
    invoke LoadCursor, NULL, IDC_ARROW
    mov   wc.hCursor, eax
    invoke RegisterClassEx, addr wc
    invoke CreateWindowEx, NULL, addr g_szClassName, addr g_szAppName,\
               WS_OVERLAPPED + WS_CAPTION + WS_SYSMENU, 100, 100, 300, 100,\
               NULL, NULL, hInst, NULL;WS_VISIBLE
    mov   hwnd, eax
    invoke ShowWindow, hwnd, SW_SHOW

    .while TRUE
        invoke GetMessage, ADDR msg, NULL, 0, 0
        .BREAK .IF (!eax)
        invoke TranslateMessage, ADDR msg
        invoke DispatchMessage, ADDR msg
    .endw
    mov eax, msg.wParam
    ret
WinMain endp


WndProc proc hWndMain: DWORD, uMsg: DWORD, wParam: DWORD, lParam: DWORD
    .if uMsg == WM_DESTROY
        invoke Shell_NotifyIcon, NIM_DELETE, addr g_stNoteIcon
        invoke PostQuitMessage, NULL

    .elseif uMsg== WM_CLOSE
        test   g_dwFlag, c_IconShowed
        jz     @DestroyWin
        test   g_dwFlag, c_AppQuit
        .if    ZERO?
            mov    g_stNoteIcon.uTimeout, 20
            invoke lstrcpy, addr g_stNoteIcon.szInfo,addr g_szAppQuit
            invoke Shell_NotifyIcon, NIM_MODIFY, addr g_stNoteIcon
            or     g_dwFlag, c_AppQuit       
            invoke ShowWindow, g_hWndMain, SW_HIDE
        .else
@DestroyWin:
            invoke DestroyWindow, g_hWndMain
        .endif
    .elseif uMsg == WM_CREATE
        mov   eax, hWndMain
        mov   g_hWndMain, eax

        invoke RtlZeroMemory, addr g_stNoteIcon, sizeof g_stNoteIcon
        mov    eax, g_hWndMain
        mov    g_stNoteIcon.hwnd, eax
        mov    g_stNoteIcon.cbSize, sizeof g_stNoteIcon
        mov    g_stNoteIcon.uID, IDI_TRAY
        mov    g_stNoteIcon.uFlags, NIF_ICON + NIF_MESSAGE + NIF_TIP
        mov    g_stNoteIcon.uCallbackMessage, WM_SHELLNOTIFY
        invoke LoadIcon, NULL, IDI_WINLOGO
        mov    g_stNoteIcon.hIcon, eax
        invoke lstrcpy, addr g_stNoteIcon.szTip, addr g_szAppName

        invoke CreateWindowEx, NULL, addr g_szButton, addr g_szShowTrayIcon,\
            WS_BORDER + WS_CHILD + WS_VISIBLE + BS_AUTOCHECKBOX, 5, 5, 130, 20,\
            g_hWndMain, IDC_ckbShowTrayIcon, g_hInstance, NULL

        invoke CreateWindowEx, NULL, addr g_szButton, addr g_szShowBalloon,\
            WS_BORDER + WS_CHILD + WS_VISIBLE, 5, 30, 130, 20,\
            g_hWndMain, IDC_ckbShowBalloon, g_hInstance, NULL

    .elseif (uMsg == WM_COMMAND)
            mov eax, wParam
            mov edx, eax
            shr edx, 16
            .IF (dx == BN_CLICKED)
                .if (eax == IDC_ckbShowTrayIcon)
                    test g_dwFlag, c_IconShowed
                    .if ZERO?
                        mov eax, NIM_ADD
                        or  g_dwFlag, c_IconShowed
                    .else
                        mov eax, NIM_DELETE
                        and g_dwFlag, not c_IconShowed
                    .endif
                    invoke Shell_NotifyIcon, eax, addr g_stNoteIcon
                .elseif (eax==IDC_ckbShowBalloon)
                    xor    eax, eax
                    mov    g_stNoteIcon.dwState, eax
                    mov    g_stNoteIcon.dwStateMask, eax

                    invoke lstrcpy, addr g_stNoteIcon.szInfo, addr g_szInfo
                    mov    g_stNoteIcon.uTimeout, 200

                    invoke lstrcpy, addr g_stNoteIcon.szInfoTitle, addr g_szInfoTitle
                    mov    g_stNoteIcon.dwInfoFlags, NIIF_INFO

                    mov    g_stNoteIcon.uFlags, NIF_ICON + NIF_MESSAGE + NIF_TIP + NIF_INFO

                    mov eax, NIM_MODIFY
                    test   g_dwFlag, c_IconShowed
                    .if    ZERO?
                        or g_dwFlag, c_IconShowed
                        invoke SendDlgItemMessage, g_hWndMain, IDC_ckbShowTrayIcon, BM_SETCHECK, BST_CHECKED, 0
                        mov eax, NIM_ADD
                    .endif
                       
                    invoke Shell_NotifyIcon, eax, addr g_stNoteIcon
                .endif
            .ENDIF
    .elseif uMsg==WM_SHELLNOTIFY
        .if wParam==IDI_TRAY
            mov eax, lParam
            .if (eax==NIN_BALLOONSHOW);当气泡提示显示后外壳发送此消息
                invoke MessageBox, g_hWndMain, addr g_szShowMessageNow, addr g_szAppName, MB_ICONINFORMATION
            .else
                test   g_dwFlag, c_AppQuit
                .if    ZERO?
                    .if (eax==NIN_BALLOONHIDE)
                        invoke MessageBox, g_hWndMain, addr g_szMessageHide, addr g_szAppName, MB_ICONINFORMATION
                    .elseif (eax==WM_RBUTTONDOWN || eax==WM_LBUTTONDOWN)
                        invoke MessageBox, g_hWndMain, addr g_szClickTrayIcon, addr g_szAppName, MB_ICONINFORMATION
                    .elseif (eax==NIN_BALLOONTIMEOUT);当气泡提示由于超时而消失时外壳发送此消息
                        invoke MessageBox, g_hWndMain, addr g_szMessageTimeOut, addr g_szAppName, MB_ICONINFORMATION
                    .elseif (eax==NIN_BALLOONUSERCLICK)
                        invoke MessageBox, g_hWndMain, addr g_szClickBalloonOrIcon, addr g_szAppName, MB_ICONINFORMATION
                    .endif
                .else
                    invoke SendMessage, g_hWndMain, WM_CLOSE, 0, 0
    .endif
            .endif
        .endif
    .else
        invoke DefWindowProc, hWndMain, uMsg, wParam, lParam       
        ret
    .endif

    xor eax, eax
    ret
WndProc endp

end start

 

 



版权所有!www.sieye.cn
E.Mail:sieye@sohu.com QQ:66697110