| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1116 人关注过本帖
标题:编一个托盘程序
只看楼主 加入收藏
随意魔
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2004-5-13
收藏
 问题点数:0 回复次数:4 
编一个托盘程序
本程序项目名为TRAY.vbp,它包括模块TRAY.bas和窗体TRAY.frm。

  1、TRAY.bas源代码

  Type NOTIFYICONDATA

   cbSize As Long

   hwnd As Long

   uID As Long

   uFlags As Long

   uCallbackMessage As Long

   hIcon As Long

   szTip As String * 64

  End Type

  Public a As Long

  '以下为 Shell_NotifyIcon将用到的常量

  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 NIM_MODIFY = &H1

  'Shell_NotifyIcon的函数声明

  Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _

  (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

  '处理消息将用到的结构、常量、API声明

  Type POINTAPI

   x As Long

   y As Long

  End Type

  Type Msg

   hwnd As Long

   message As Long

   wParam As Long

   lParam As Long

   time As Long

   pt As POINTAPI

  End Type

  Public Const WM_USER = &H400

  Public Const WM_RBUTTONDOWN = &H204

  Public Const WM_LBUTTONDOWN = &H201

  Public Const GWL_WNDPROC = -4

  Public trayflag As Boolean

  Global lpPrevWndProc As Long

  Global gHW As Long

  Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

  Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

  '以下过程为消息循环处理

  Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

  If hw = Form1.hwnd And uMsg = WM_USER+100 Then'检测到鼠标点动托盘图标

   Select Case lParam

   Case WM_RBUTTONDOWN '鼠标右键按下

   Form1.PopupMenu Form1.traymnu '弹出菜单

   Case WM_LBUTTONDOWN '鼠标左键按下

   Form1.PopupMenu Form1.mnutray2 '弹出菜单

   Case Else

   End Select

  Else '调用缺省窗口指针

   WindowProc = CallWindowProc(lpPrevWndProc, hw,uMsg, wParam, lParam)

  End If

  End Function

  2、TRAY.frm源代码

  Dim MyNot As NOTIFYICONDATA '定义一个托盘结构

  Private Sub Command1_Click() '鼠标按下删除按钮

   With MyNot

   .hIcon = Form1.Icon '托盘图标指针

   .hwnd = Form1.hwnd '窗体指针

   .szTip = "" '弹出提示字符串

   .uCallbackMessage = WM_USER+100 '对应程序定义的消息

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE '标志

   .uID = 1 '图标识别符

   .cbSize = Len(MyNot) '计算该结构所占字节数

   End With

  hh = Shell_NotifyIcon(NIM_DELETE, MyNot) '删除该图标

  trayflag = False '图标删除后trayflag为假

  End Sub

  Private Sub Command2_Click() '按下创建按钮

  Dim hh As Long

   With MyNot

   .hIcon = Form1.Icon

   .hwnd = Form1.hwnd

   .szTip = "托盘图标" & Chr(&H0)

   .uCallbackMessage = WM_USER+100

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE

   .uID = 1

   .cbSize = Len(MyNot)

   End With

  hh = Shell_NotifyIcon(NIM_ADD, MyNot) '添加一个托盘图标

  trayflag = True '图标添加后trayflag为真

  End Sub

  Private Sub Command3_Click() '修改托盘图标

  Dim hh As Long

  Set P = LoadPicture("c:\dos\bridge.ico") '导入一个新图标

   With MyNot

   .hIcon = P '将托盘图标改为新图标

   .hwnd = Form1.hwnd

   .szTip = "桥梁图标" & Chr(&H0) '更改提示信息

   .uCallbackMessage = WM_USER+100

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE

   .uID = 1

   .cbSize = Len(MyNot)

   End With

  hh = Shell_NotifyIcon(NIM_MODIFY, MyNot) '修改托盘的某些特征

  End Sub

  Private Sub Command4_Click() 'END按钮被按下

   Quit '调用退出函数Quit

  End Sub

  Private Sub Form_Load()

   gHW = Me.hwnd '取得本窗体指针

  '下一句调用钩子函数,将自制消息处理函数钩入Windows的消息循环

   hook

  End Sub

  Public Sub hook()

  '利用AddressOf取得消息处理函数WindowProc的指针,并将其传给SetWindowLong

  'lpPrevWndProc用来存储原窗口的指针

   lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOfWindowProc)

  End Sub

  Public Sub Unhook()

  '本子程序用原窗口的指针替换WindowProc函数的指针,即关闭子类、退出消息循环

   Dim temp As Long

   temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)

  End Sub

  Private Sub mnuhideForm_Click() '点中弹出菜单的‘隐藏’功能

   Form1.Hide '将窗体隐藏

  End Sub

  Private Sub mnumaxForm_Click() '点中弹出菜单的‘最大化’功能

   formstatus (2) '窗体最大化

  End Sub

  Private Sub mnuminForm_Click() '点中弹出菜单的‘最小化’功能

   formstatus (1) '窗体最小化

  End Sub

  Private Sub mnunorForm_Click() '点中弹出菜单的‘正常’功能

   formstatus (0) '窗体还原到正常

  End Sub

  Public Sub quit() '退出

   If trayflag = True Then Command1_Click '托盘图标仍在,模拟按下‘删除’按钮

   Unhook '退出消息循环

   Unload Me '卸载窗体

  End Sub

  Private Sub mnuQuit_Click() '点中弹出菜单的‘退出’功能

   quit

  End Sub

  Public Sub formstatus(ByVal wstates) '根据传递的参数变化窗体的状态

   Form1.WindowState = wstates '设置窗体的状态

   Form1.Show '显示窗体

  End Sub

搜索更多相关主题的帖子: Long 源代码 TRAY NIF 托盘 
2004-05-19 10:27
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
这些东西我已经把它封装在一个控件里了,可以很容易地调用,呵呵!

天津网站建设 http://www./
2004-05-19 14:02
随意魔
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2004-5-13
收藏
得分:0 
有更简短实用的源码可以回复给我:)

.-_-.曾经拥有.足以泪流.-_-.
2004-05-20 20:46
xingehappy
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2004-4-14
收藏
得分:0 
那個控件能不能給我.省的每次都要寫.懶啊!!!

我又回頭去飛,去追.就算我追到最后只剩冰雪.天都為我傷悲.冷的愛快枯萎.任漫天風雪覆蓋我的心碎........
2004-06-03 19:02
随意魔
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2004-5-13
收藏
得分:0 
源代码在上面,复制一分到你计算机不就OK了?

.-_-.曾经拥有.足以泪流.-_-.
2004-06-04 09:56
快速回复:编一个托盘程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042872 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved