#include <windows.h>
#define IDR_PAUSE 12
#define IDR_START 13
LPCTSTR szAppClassName = TEXT("服务程序")
LPCTSTR szAppWindowName = TEXT("服务程序")
HMENU hmenu//菜单句柄
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
NOTIFYICONDATA nid
UINT WM_TASKBARCREATED
POINT pt//用于接收鼠标坐标
int xx//用于接收菜单选项返回值
// 不要修改TaskbarCreated,这是系统任务栏自定义的消息
WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"))
switch (message)
{
case WM_CREATE://窗口创建时候的消雀氏息.
nid.cbSize = sizeof(nid)
nid.hWnd = hwnd
nid.uID = 0
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP
nid.uCallbackMessage = WM_USER
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION)
lstrcpy(nid.szTip, szAppClassName)
Shell_NotifyIcon(NIM_ADD, &nid)
hmenu=CreatePopupMenu()//生成菜单
AppendMenu(hmenu,MF_STRING,IDR_PAUSE,L"暂停服务")//为菜单添加两个选项
AppendMenu(hmenu,MF_STRING,IDR_START,L"关于")
break
case WM_USER://连续使用该程序时候的消息.
if (lParam == WM_LBUTTONDOWN)
MessageBox(hwnd, TEXT("Win32 API 实现系统托盘程序,双击托盘可以退出!"), szAppClassName, MB_OK)
if (lParam == WM_LBUTTONDBLCLK)//双击托盘的消息,退出.
SendMessage(hwnd, WM_CLOSE, wParam, lParam)
if (lParam == WM_RBUTTONDOWN)
{
GetCursorPos(&pt)//取鼠标坐标
::SetForegroundWindow(hwnd)//解决在菜单外单击左键菜单不消失的问题
EnableMenuItem(hmenu,IDR_PAUSE,MF_GRAYED)//让菜单中的某一项变灰
颂芹 xx=TrackPopupMenu(hmenu,TPM_RETURNCMD,pt.x,pt.y,NULL,hwnd,NULL)//显示菜单并获取选项ID
if(xx==IDR_PAUSE) MessageBox(hwnd, TEXT("111"), szAppClassName, MB_OK)
if(xx==IDR_START) MessageBox(hwnd, TEXT("222"), szAppClassName, MB_OK)
if(xx==0) PostMessage(hwnd,WM_LBUTTONDOWN,NULL,NULL)
//MessageBox(hwnd, TEXT("右键"), szAppName, MB_OK)
}
break
case WM_DESTROY://窗口销毁时候的消息.
Shell_NotifyIcon(NIM_DELETE, &nid)
PostQuitMessage(0)
break
default:
/*
* 防止当Explorer.exe 崩溃以后,程序在系统系统托盘中的图标就消失
*
* 原理:Explorer.exe 重新载入后会重建系统任务栏。当系统任务栏建立的时候会向系统内所有
* 注册接收TaskbarCreated 消息的顶级窗口发送一条消息,我们只需要捕捉这个消息,并重建系
* 统托盘的图标即顷樱散可。
*/
if (message == WM_TASKBARCREATED)
SendMessage(hwnd, WM_CREATE, wParam, lParam)
break
}
return DefWindowProc(hwnd, message, wParam, lParam)
}
int main()
{
HWND hwnd
MSG msg
WNDCLASS wndclass
HWND handle = FindWindow(NULL, szAppWindowName)
if (handle != NULL)
{
MessageBox(NULL, TEXT("Application is already running"), szAppClassName, MB_ICONERROR)
return 0
}
wndclass.style = CS_HREDRAW | CS_VREDRAW
wndclass.lpfnWndProc = WndProc
wndclass.cbClsExtra = 0
wndclass.cbWndExtra = 0
wndclass.hInstance = NULL
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION)
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW)
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH)
wndclass.lpszMenuName = NULL
wndclass.lpszClassName = szAppClassName
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppClassName, MB_ICONERROR)
return 0
}
// 此处使用WS_EX_TOOLWINDOW 属性来隐藏显示在任务栏上的窗口程序按钮
hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
szAppClassName, szAppWindowName,
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, NULL, NULL, NULL)
ShowWindow(hwnd, 0)
UpdateWindow(hwnd)
//消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg)
DispatchMessage(&msg)
}
return 0
}
Windows 95以及后来的Windows版本允许你将程序图标放入系统托盘。所谓系统托盘,通常指的是屏幕右下方显示时间,音量等图标的那个区域。这个区域主要用于显示状态信息或者当你运行的程序不可见时,允许你方便地访问程序的主要特性。这个区域还可以用于显示小程序的图标,以便用户容让氏早易访问主程序,或者在预定的时间加载主程序。有些系统托盘图标可以变化用以指示程序状态,例如,浏览器的坦雀系统托盘图标当modem接收和发送数据时显示的是不同的图标。把鼠标移到托盘图标上停留一下常常会显示一个提示,根据程序的状态,它可能也会变化。在托盘图标上单击鼠标右键常常显示一个程序菜单,而双击鼠标左键常常可以启动主窗口或应用程序。
访问系统托盘的方法是通过Shell_NotifyIcon函数和NOTIFYICONDATA结构实现的。
typedef struct _NOTIFYICONDATA {
DWORD cbSize
HWND hWnd
UINT uID
UINT uFlags
UINT uCallbackMessage
HICON hIcon
TCHAR szTip[64]
DWORD dwState //Version 5.0
DWORD dwStateMask //Version 5.0
TCHAR szInfo[256] //Version 5.0
UINT uTimeout //Version 5.0
TCHAR szInfoTitle[64] //Version 5.0
DWORD dwInfoFlags //Version 5.0
} NOTIFYICONDATA, *PNOTIFYICONDATA
为了要核没在系统托盘中显示图标,用NIM_ADD标志调用Shell_NotifyIcon函数。
#define ID_TASKBARICON 100
#define WM_ICONNOTIFY (WM_USER + 101)
NOTIFYICONDATA nid
// 初始化系统托盘图标
m_tnid.cbSize=sizeof(NOTIFYICONDATA)
m_tnid.hWnd=this->m_hWnd
m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP
m_tnid.uCallbackMessage=MYWM_NOTIFYICON
CString szToolTip
szToolTip=_T("托盘实例")
_tcscpy(m_tnid.szTip,szToolTip)
m_tnid.uID=IDR_MAINFRAME
m_tnid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME)
::Shell_NotifyIcon(NIM_ADD,&m_tnid)
cbSize成员是结构的大小(使用它主要是为了支持将来这个结构大小增加)。
hWnd是窗口句柄。当图标发生某事件时(如单击、双击等),Windows将向窗口发送uCallbackMessage成员指定的消息。uID成员指定与图标关联的ID。它不是很重要,除非你需要显示并跟踪几个图标。
uFlag成员告诉Windows应该读取哪个成员。当添加一个图标时,应该包含这个结构的大多数成员。当更新图标时,如只是需要改变图标时,你只要设置相应
的标志就可以了。
hIcon成员是你想显示的图标。 最后,szTip成员是提示文本。设置好这些结构成员后,调用Shell_NotifyIcon函数。
当与图标关联的事件发生时,Windows将发送uCallbackMessage成员指定的消息。IParam包含发送的消息。当获得WM_LBUTTONDBLCLK消息时显示主窗口 在这里我们可以定义一个WM_MYNOTIFYICON
#define WM_MYNOTIFYICON WM_USER+101或者启动主程序。当获得WM_RBUTTONUP消息时显示菜单。
注意:如果在系统托盘中单击鼠标右键,有时会有一个d出式(上下文菜单)菜单显示/消失的怪现象,可以参考下列代码加以解决。
switch(message)
{
case MYWM_NOTIFYICON:
if(lParam==WM_LBUTTONDBLCLK)
{
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW)
CenterWindow()
}
else if(lParam==WM_RBUTTONDOWN)
{
CMenu menu
menu.LoadMenuW(IDM_RIGHT_MENU)
CMenu *pMenu
pMenu=menu.GetSubMenu(0)
POINT point
GetCursorPos(&point)
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd())
}
break
case WM_SYSCOMMAND:
if(wParam==SC_MINIMIZE||wParam==SC_CLOSE)
{
AfxGetApp()->m_pMainWnd->ShowWindow(SW_HIDE)
return 0//请问这里为什么要加个return 0 呢???
}
break
} 不论什么时候,你都可以用 NIM_MODIFY 调用 Shell_NotifyIcon。程序终止之前,用 NIM_DELETE 调用 Shell_NotifyIcon从托盘中清除图标。
Shell_NotifyIcon(NIM_DELETE, &nid)
另外打开程序时直接最小托盘
CMyEyeProtectApp::InitInstance()函数中
m_pMainWnd->ShowWindow(SW_HIDE)
这样就可以在开始的时候直接进入托盘
另外在程序开始之前出现一个几秒钟的启动画面
CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数中
CSplashDlg *dlg=new CSplashDlg(this)
dlg->Create(CSplashDlg::IDD,this)
dlg->ShowWindow(SW_SHOW)
dlg->CenterWindow(this)
dlg->UpdateWindow()
Sleep(2000)
dlg->DestroyWindow()
把那对话框的属性贴上你想显示的图片,然后去掉边框,再按上面方法处理就可以了
在Form中制作一个菜单,在窗体上点右键-菜单编辑器,标题随便,名称“F00”(不含引号,下同),去掉“可见”前的勾,“下一个”,标题“打开”,名称“F01”,点“右箭头”,“下一个”,标题退出,名称“F02”,点“右箭头”,“确定”以下是代码,效果是程序运行时不显示在托盘中图标,点最小化后显示在托盘图标并且不在任务栏中显示窗口,双击图标打开窗口,右键点图标d出菜单
'-----------------------窗体中--------------------------
Dim WindowTop, WindowLeft
Function WindowStyle()
'以下把程序放入System Tray====================================System Tray Begin
With nfIconData
.hWnd = Me.hWnd
.uID = Me.Icon
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle
'定义鼠标移动到托盘上时显示的Tip
.szTip = "已最小化到托盘" &vbNullChar
.cbSize = Len(nfIconData)
End With
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
'=============================================================System Tray End
Me.Hide
End Function
Private Sub Form_Resize()
WindowTop = Me.Top
WindowLeft = Me.Left
If Me.WindowState = 1 Then
WindowStyle
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMsg As Single
lMsg = X / Screen.TwipsPerPixelX
Select Case lMsg
Case WM_LBUTTONDBLCLK '双击左键显示窗体,要改成其他的看模块里的定义
ShowWindow Me.hWnd, SW_RESTORE
Me.Top = WindowTop
Me.Left = WindowLeft
'Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
Me.SetFocus
Case WM_RBUTTONUP '在托盘图标上点右键显示菜单
PopupMenu F00 '菜单名称为F00,做菜单时你可以改成别的,这里也得改成相应的
End Select
End Sub
Private Sub F01_Click()
ShowWindow Me.hWnd, SW_RESTORE
Me.Top = WindowTop
Me.Left = WindowLeft
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End Sub
Private Sub F02_Click()
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'退出程序时删除托盘图标
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End Sub
'-----------------------模块中--------------------------
'在系统托盘显示图标的模块
Option Explicit
Public Const MAX_TOOLTIP As Integer = 64
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
Public Const SW_RESTORE = 9
Public Const SW_HIDE = 0
Public nfIconData As NOTIFYICONDATA
Public Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * MAX_TOOLTIP
End Type
Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)