VB窗体最小化
我想 把VB做出来的 程序点击最小化,窗体会最小化到桌面右下角。高手告诉我一下代码!!!!!!!!!!
我想 把VB做出来的 程序点击最小化,窗体会最小化到桌面右下角。高手告诉我一下代码!!!!!!!!!!
答案:一、首先需要一个模块,没有加的话添加一个,在模块的代码页中输入以下部分,注意看我的注释:'以下是做窗体在最小化不显示在任务栏而在通知栏的常量基函数声明
Public Const DefaultIconIndex = 1 '图标缺省索引
Public Const WM_LBUTTONDOWN = &H201 '按鼠标左键
Public Const WM_RBUTTONDOWN = &H204 '按鼠标右键
Public Const WM_LBUTTONDBLCLK = &H203 '双击鼠标左键Public Const NIM_ADD = 0 '添加图标
Public Const NIM_MODIFY = 1 '修改图标
Public Const NIM_DELETE = 2 '删除图标Public Const NIF_MESSAGE = 1 'message 有效
Public Const NIF_ICON = 2 '图标操作(添加、修改、删除)有效
Public Const NIF_TIP = 4 'ToolTip(提示)有效'
'API函数声明
'图标操作
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'判断窗口是否最小化
Public Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
'
'设置窗口位置和状态(position)的功能
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'定义类型
'通知栏图标状态
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'===========================================================================
二、在你要缩小的窗口代码编辑窗口里定义一个量。如下:
'用于标识窗口是否已经按过最小化按纽
Dim MinFlag As Boolean三、下面三个事件分别是判断最小化和任务栏图标右键菜单,双击等效果的。
===============================================================
rivate Sub Form_Resize()
'判断窗口是否最小化状态,并且是按最小化按纽后第一次发生Resize事件
If IsIconic(Me.hWnd) <> 0 And MinFlag = False Then
MinFlag = Not MinFlag
Me.Visible = False '隐藏窗口
'将窗口图标加入通知栏
Call Icon_Add(Me.hWnd, Me.Caption, Me.Icon, 0)
End If
End Sub'
Private Sub m_Menu_Click(Index As Integer)
Select Case Index
Case 0: '当单击"显示窗口"菜单时
Call Icon_Del(Formdl.hWnd, 0) '删除通知栏图标
Formdl.Show '调出窗口
Formdl.WindowState = 0
MinFlag = False
Case 1: '当单击"退出"菜单时
Call Icon_Del(Formdl.hWnd, 0) '删除通知栏图标
End '退出程序
End Select
End Sub'
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim L
L = X \ Screen.TwipsPerPixelX
'点击通知栏图标,用鼠标右键时调出弹出菜单
If L = WM_RBUTTONDOWN Then
Me.PopupMenu m_Main
ElseIf L = WM_LBUTTONDBLCLK Then
Call Icon_Del(Formdl.hWnd, 0) '删除通知栏图标
Formdl.Show '调出窗口
Formdl.WindowState = 0
MinFlag = False
End If
End Sub
四、我的程序运行最小化到任务栏后的图标是这个
右键点击该图标就有了右键菜单
祝你早日完成
要一个窗体,一个模块
窗体内输入
Private Sub Form_Load()
prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf WndProc
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hWnd = Me.hWnd
nid.uID = 9999
nid.uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE
nid.hIcon = Me.Icon
nid.szTip = Form1.Caption + Chr(0)
nid.uCallbackMessage = WM_USER + 100
Shell_NotifyIcon NIM_ADD, nid
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hWnd = Me.hWnd
nid.uID = 9999
Shell_NotifyIcon NIM_DELETE, nidEnd Sub
Private Sub Timer3_Timer()
If Form1.WindowState = 1 Then
Me.Hide
End If
End Sub模块部分输入模块部分输入模块部分输入模块部分输入模块部分输入模块部分输入模块部分输入模块部分输入
Option Explicit
Public Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONUP = &H202Public Const NIM_ADD = 0
Public Const NIM_MODIFY = 1
Public Const NIM_DELETE = 2Public Const NIF_MESSAGE = 1
Public Const NIF_ICON = 2
Public Const NIF_TIP = 4Type 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
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) 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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic prevWndProc As Long
Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_USER + 100 Then
If lParam = WM_LBUTTONDBLCLK Then
Form1.WindowState = vbNormal
Form1.ShowEnd If: End If
WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam)
End Function运行结果
代码很麻烦,加我,我发给你代码最小化到任务栏?楼上的答案还是可能实现的