用vb怎样实现点击程序的最小化按钮后把窗口变到右下角成为一个图标?

然后点击右下角的图标后又能还原窗口.
2024-11-24 03:00:13
推荐回答(2个)
回答1:

建立一个图象筐把它的名称改为imgicon1.
form1窗体的代码:

Private Sub Command1_Click()

Me.Hide
Timer2.Enabled = False
TrayIcon.cbSize = Len(TrayIcon)
' Handle of the window used to handle messages - which is the this form
TrayIcon.hwnd = Me.hwnd
' ID code of the icon
TrayIcon.uId = vbNull
' Flags
TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
' ID of the call back message
TrayIcon.ucallbackMessage = WM_MOUSEMOVE
' The icon - sets the icon that should be used
TrayIcon.hIcon = imgicon1.Picture
' The Tooltip for the icon - sets the Tooltip that will be displayed
TrayIcon.szTip = "备份管理器" & Chr$(0)
' Add icon to the tray by calling the Shell_NotifyIcon API
'NIM_ADD is a Constant - add icon to tray
Call Shell_NotifyIcon(NIM_ADD, TrayIcon)
' Don't let application appear in the Windows task list
App.TaskVisible = False
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)

Static Message As Long
Static RR As Boolean

'x is the current mouse location along the x-axis
Message = x / Screen.TwipsPerPixelX
If RR = False Then
RR = True
Select Case Message
' Left double click (This should bring up a dialog box)
Case WM_LBUTTONDBLCLK
Me.Show
Timer2.Enabled = True
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.hwnd = Me.hwnd
TrayIcon.uId = vbNull
'Remove icon for Tray
Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)

' Right button up (This should bring up a menu)
Case WM_RBUTTONUP
Me.PopupMenu mainmenu
End Select
RR = False
End If

End Sub

新建一个标准模块把代码复制进:
Option Explicit

'**Originally published by Ryan Heldt (rheldt@vb-online.com)
'**Modified by Donovan Parks (donopark@awinc.com)

'Win32 API declaration
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public 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

' Constants used to detect clicking on the icon
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONUP = &H205

' Constants used to control the icon
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIF_MESSAGE = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

' Used as the ID of the call back message
Public Const WM_MOUSEMOVE = &H200

' Used by Shell_NotifyIcon
Public 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

'create variable of type NOTIFYICONDATA
Public TrayIcon As NOTIFYICONDATA

回答2:

VB里有一个部件可以做到,具体叫什么我忘记了.我有压缩格式的VB工程,写上你的QQ或邮箱吧