VB编程托盘问题
程序已经弄好了,达到托盘效果,不过我想改动一下,程序有一个最小化到托盘的按钮控件,现在我想实现如下效果:打开程序,程序能在任务栏显示,并且托盘也显示。缩小到托盘之后程序在...
程序已经弄好了,达到托盘效果,不过我想改动一下,程序有一个最小化到托盘的按钮控件,现在我想实现如下效果:打开程序,程序能在任务栏显示,并且托盘也显示。缩小到托盘之后程序在任务栏消失,并且到托盘显示,请问有没高手的,请别复制代码,如果有代码举例子的请讲明每个代码的意思~
我原本的程序是这样的:打开程序,任务栏和托盘都没有显示,就是这里我改为打开程序之后,任务栏和托盘都显示,请问如何搞 展开
我原本的程序是这样的:打开程序,任务栏和托盘都没有显示,就是这里我改为打开程序之后,任务栏和托盘都显示,请问如何搞 展开
2个回答
展开全部
Public Type NOTIFYICONDATA
cbSize As Long '修改结构所占字节数
hwnd As Long '接受托盘图标消息的窗口句柄
uID As Long '为图标设置的ID
uFlags As Long '设置下面3项是否有效
uCallbackMessage As Long '消息的编号
hIcon As Long '托盘图标的句柄
szTip As String * 64 '鼠标指针到托盘图标时显示的提示字符串
End Type
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_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public nfIco As NOTIFYICONDATA
Public Sub TuoPan(frm As Form)
nfIco.hwnd = frm.hwnd
nfIco.uID = frm.Icon
nfIco.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
nfIco.uCallbackMessage = WM_MOUSEMOVE
nfIco.hIcon = frm.Icon.Handle
nfIco.szTip = "托盘示例" & vbNullChar '标题
nfIco.cbSize = Len(nfIco)
Call Shell_NotifyIcon(NIM_ADD, nfIco)
frm.Visible = False
End Sub
Private Sub Form_Load()
TuoPan Form1
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_LBUTTONUP
Me.Show
Case WM_RBUTTONUP
Case WM_MOUSEMOVE
Case WM_LBUTTONDOWN
Case WM_LBUTTONDBLCLK
Case WM_RBUTTONDOWN
Case WM_RBUTTONDBLCLK
Case Else
End Select
End Sub
cbSize As Long '修改结构所占字节数
hwnd As Long '接受托盘图标消息的窗口句柄
uID As Long '为图标设置的ID
uFlags As Long '设置下面3项是否有效
uCallbackMessage As Long '消息的编号
hIcon As Long '托盘图标的句柄
szTip As String * 64 '鼠标指针到托盘图标时显示的提示字符串
End Type
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_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public nfIco As NOTIFYICONDATA
Public Sub TuoPan(frm As Form)
nfIco.hwnd = frm.hwnd
nfIco.uID = frm.Icon
nfIco.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
nfIco.uCallbackMessage = WM_MOUSEMOVE
nfIco.hIcon = frm.Icon.Handle
nfIco.szTip = "托盘示例" & vbNullChar '标题
nfIco.cbSize = Len(nfIco)
Call Shell_NotifyIcon(NIM_ADD, nfIco)
frm.Visible = False
End Sub
Private Sub Form_Load()
TuoPan Form1
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_LBUTTONUP
Me.Show
Case WM_RBUTTONUP
Case WM_MOUSEMOVE
Case WM_LBUTTONDOWN
Case WM_LBUTTONDBLCLK
Case WM_RBUTTONDOWN
Case WM_RBUTTONDBLCLK
Case Else
End Select
End Sub
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
'这一部分API的目的是为了实现任务栏是否显示
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_EX_TOOLWINDOW = &H80
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cX As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOP = 0
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_HIDEWINDOW = &H80
Dim j As Integer '原窗体状态
Sub ShowTaskbarIcon(lHwnd As Long, b As Boolean)
lExStyle = GetWindowLong(lHwnd, GWL_EXSTYLE)
If b Then
lExStyle = lExStyle Or WS_EX_APPWINDOW
Else
lExStyle = lExStyle And Not WS_EX_APPWINDOW
End If
SetWindowPos hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_HIDEWINDOW
SetWindowLong lHwnd, GWL_EXSTYLE, lExStyle
SetWindowPos hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_SHOWWINDOW
End Sub
'这里面的代码应该放到你的托盘图标双击显示里面,我没有控件所以用一个按钮代替
Private Sub Command1_Click()
Me.WindowState = j '还原最小化前的窗体状态
'显示任务栏图标
ShowTaskbarIcon Me.hwnd, True
End Sub
Private Sub Form_Resize()
If Me.WindowState <> 1 Then j = Me.WindowState '记住非最小化的状态
'窗体最大化为 Me.WindowState = 2 ,0为普通窗口
'窗体最小化
If Me.WindowState = 1 Then
'隐藏任务栏图标
ShowTaskbarIcon Me.hwnd, False
'如果需要你在这里加上你的托盘代码
End If
End Sub
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_EX_TOOLWINDOW = &H80
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cX As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOP = 0
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_HIDEWINDOW = &H80
Dim j As Integer '原窗体状态
Sub ShowTaskbarIcon(lHwnd As Long, b As Boolean)
lExStyle = GetWindowLong(lHwnd, GWL_EXSTYLE)
If b Then
lExStyle = lExStyle Or WS_EX_APPWINDOW
Else
lExStyle = lExStyle And Not WS_EX_APPWINDOW
End If
SetWindowPos hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_HIDEWINDOW
SetWindowLong lHwnd, GWL_EXSTYLE, lExStyle
SetWindowPos hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_SHOWWINDOW
End Sub
'这里面的代码应该放到你的托盘图标双击显示里面,我没有控件所以用一个按钮代替
Private Sub Command1_Click()
Me.WindowState = j '还原最小化前的窗体状态
'显示任务栏图标
ShowTaskbarIcon Me.hwnd, True
End Sub
Private Sub Form_Resize()
If Me.WindowState <> 1 Then j = Me.WindowState '记住非最小化的状态
'窗体最大化为 Me.WindowState = 2 ,0为普通窗口
'窗体最小化
If Me.WindowState = 1 Then
'隐藏任务栏图标
ShowTaskbarIcon Me.hwnd, False
'如果需要你在这里加上你的托盘代码
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询