Powerbasic语言写的托盘图标程序代码

'==============================================================================
'
' System Tray Example for PowerBASIC for Windows
' Copyright (c) 1998-2008 PowerBASIC, Inc.
' All Rights Reserved.
'
' Shows how to create and change an icon in the Windows system tray.
'
'==============================================================================

#COMPILER PBWIN 9
#COMPILE EXE
#RESOURCE "TRAY.PBR"
'--------------------------------------------------------------------
%USEMACROS = 1

'--------------------------------------------------------------------
#INCLUDE "Win32API.inc"

'--------------------------------------------------------------------
%WM_TRAYICON = %WM_USER + 400

'--------------------------------------------------------------------
GLOBAL g_hInst AS DWORD

 

'==============================================================================
FUNCTION DialogProc (BYVAL hDlg AS DWORD, BYVAL wMsg AS DWORD, _
BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
'------------------------------------------------------------------------------
' Main dialog procedure
'--------------------------------------------------------------------------

LOCAL p AS POINTAPI

STATIC hMenu AS DWORD
STATIC ti AS NOTIFYICONDATA

SELECT CASE AS LONG wMsg
CASE %WM_INITDIALOG ' sent right before main dialog is shown

' Select first Option control
SendDlgItemMessage hDlg, 101, %BM_SETCHECK, %BST_CHECKED, 0

' Get Menu Handle
hMenu = GetSubMenu(LoadMenu(g_hInst, "POPUPMENU"), 0)

' Add tray icon
ti.cbSize = SIZEOF(ti)
ti.hWnd = hDlg
ti.uID = g_hInst
ti.uFlags = %NIF_ICON OR %NIF_MESSAGE OR %NIF_TIP
ti.uCallbackMessage = %WM_TRAYICON
ti.hIcon = LoadIcon(g_hInst, "FACE1")
ti.szTip = "Task Tray Example"
Shell_NotifyIcon %NIM_ADD, ti
DestroyIcon ti.hIcon

FUNCTION = 1


CASE %WM_TRAYICON

SELECT CASE AS LONG LOWRD(lParam)

' Left button press
CASE %WM_LBUTTONDOWN
IF IsWindowVisible(hDlg) = %FALSE THEN
ShowWindow hDlg, %SW_SHOW
END IF

' Right button press
CASE %WM_RBUTTONDOWN
IF IsWindowVisible(hDlg) = %FALSE THEN
SetForegroundWindow hDlg
GetCursorPos p
TrackPopupMenu hMenu, 0, p.x, p.y, 0, hDlg, BYVAL %NULL
Postmessage hDlg, %WM_NULL, 0, 0
END IF

END SELECT


CASE %WM_DESTROY
' Remove the tray icon if the application is closed
Shell_NotifyIcon %NIM_DELETE, ti

CASE %WM_COMMAND
SELECT CASE LOWRD(wParam)

CASE 101 TO 105
' Change tray icon
ti.hIcon = LoadIcon(g_hInst, "FACE" + FORMAT$(LOWRD(wParam) - 100))
Shell_NotifyIcon %NIM_MODIFY, ti
DestroyIcon ti.hIcon

CASE %IDOK
' Display the about box from resource - see Tray.rc
DialogBox g_hInst, BYVAL 101&, %HWND_DESKTOP, CODEPTR(AboutProc)

CASE %IDCANCEL
' Make sure they want to exit
IF MessageBox(hDlg, _
"Close Tray Example and remove its icon from the task tray?", _
"Tray Example", _
%MB_ICONEXCLAMATION OR %MB_OKCANCEL) = %IDOK THEN
EndDialog hDlg, 0
FUNCTION = 1
END IF

END SELECT


CASE %WM_SYSCOMMAND
' If either the minimize or close buttons are pressed, hide the
' window so it doesn't appear in the task bar.
SELECT CASE (wParam AND &H0FFF0)
CASE %SC_MINIMIZE
ShowWindow hDlg, %SW_HIDE
FUNCTION = 1
EXIT FUNCTION

CASE %SC_CLOSE
ShowWindow hDlg, %SW_HIDE
FUNCTION = 1
EXIT FUNCTION
END SELECT

END SELECT

END FUNCTION ' End DialogProc

 

'==============================================================================
FUNCTION AboutProc (BYVAL hDlg AS DWORD, BYVAL wMsg AS DWORD, _
BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
'------------------------------------------------------------------------------
' About dialog procedure
'--------------------------------------------------------------------------

SELECT CASE AS LONG wMsg

CASE %WM_INITDIALOG ' sent right before the About dialog is shown
FUNCTION = 1

CASE %WM_COMMAND
IF LOWRD(wParam) = %IDOK THEN
EndDialog hDlg, 0
FUNCTION = 1
END IF

END SELECT

END FUNCTION

 

'==============================================================================
FUNCTION WINMAIN (BYVAL CurInst AS DWORD, _
BYVAL PrvInst AS DWORD, _
BYVAL CmdLine AS ASCIIZ PTR, _
BYVAL CmdShow AS LONG) AS LONG
'------------------------------------------------------------------------------
' Program entry point
'--------------------------------------------------------------------------

' Initialize the main dialog from resource - see Tray.rc
' We tell the dialog to use the DialogProc for messages
g_hInst = CurInst
DialogBox g_hInst, BYVAL 100&, %HWND_DESKTOP, CODEPTR(DialogProc)

END FUNCTION

 托盘程序源码收藏,很有用的代码。 

posted on 2016-09-23 17:10  云开不败  阅读(689)  评论(0)    收藏  举报

导航