vb窗体最小化问题

我要的效果是从窗体登陆后,点击最小化按钮,让窗体最小化到系统托盘,可是现在的却只能点开的时候最小化可以,然后也可以恢复,当登陆后就只能最小化到系统托盘,却不能回复窗口。希... 我要的效果是从窗体登陆后,点击最小化按钮,让窗体最小化到系统托盘,可是现在的却只能点开的时候最小化可以,然后也可以恢复,当登陆后就只能最小化到系统托盘,却不能回复窗口。
希望高手帮忙...

代码如下

Const MAX_TOOLTIP As Integer = 64
Const NIF_ICON = &H2
Const NIF_MESSAGE = &H1
Const NIF_TIP = &H4
Const NIM_ADD = &H0
Const NIM_DELETE = &H2
Const WM_MOUSEMOVE = &H200
Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202
Const WM_LBUTTONDBLCLK = &H203
Const WM_RBUTTONDOWN = &H204
Const WM_RBUTTONUP = &H205
Const WM_RBUTTONDBLCLK = &H206
Const SW_RESTORE = 9
Const SW_HIDE = 0
Private 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

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private nfIconData As NOTIFYICONDATA

Private Sub Form_Load()
WebBrowser1.Navigate "http://www.kf.yingwl.cn/login1.asp?web=no"
End Sub

Private Sub pic_Click()
Unload Me
End Sub

Private Sub Timer1_Timer()
Me.Caption = WebBrowser1.LocationName
If InStr(Me.Caption, "赢客服-管理端") > 0 Then
pic.Visible = False
yingkf.Width = 11700
yingkf.Height = 9100
WebBrowser1.Width = 11700
WebBrowser1.Height = 9100
WebBrowser1.ToolBar = 0
Me.Left = (Screen.Width - yingkf.Width) / 2
Me.Top = (Screen.Height - yingkf.Height) / 2
Timer1.Enabled = False
End If
End Sub

Private Sub Form_Resize()

If Me.WindowState = 1 Then '如程序为最小化则——
Me.Visible = False '让程序界面不可见
nfIconData.hwnd = Me.hwnd
nfIconData.uID = Me.Icon
nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
nfIconData.uCallbackMessage = WM_MOUSEMOVE
nfIconData.hIcon = Me.Icon.Handle
nfIconData.szTip = "mifengkeji" & vbNullChar
nfIconData.cbSize = Len(nfIconData)
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
ShowWindow Me.hwnd, SW_HIDE

End If
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
lMsg = x / Screen.TwipsPerPixelX
If lMsg = WM_LBUTTONUP Then
yingkf.WindowState = 0 '程序回复到Normal状态
Me.Visible = True '从任务栏中清除图标
Call Shell_NotifyIcon(NIM_DELETE, nfIconData) '退出图标
End If
End Sub
根据http://zhidao.baidu.com/question/27621068.html,我已经找到了问题的所在位置,可是不知道怎么解决,我的窗口大小不能太小,所以。。。希望高手继续帮忙。
展开
 我来答
katar1024
2008-10-25 · TA获得超过942个赞
知道小有建树答主
回答量:511
采纳率:0%
帮助的人:529万
展开全部
给你一个类模块吧,呵呵,我用了好久的,能最小化到托盘,还能弹出气球提示,这几天我冲分类达人,回答得好再加些分吧,感激不尽!
再把下面的代码用记事本保存成frmTray.frm
添加到工程里
==========Form1===============
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 1560
TabIndex = 3
Top = 1680
Width = 1215
End
Begin VB.PictureBox Picture1
Height = 495
Left = 1560
ScaleHeight = 435
ScaleWidth = 1155
TabIndex = 2
Top = 360
Width = 1215
End
Begin VB.TextBox Text2
Height = 270
Left = 1560
TabIndex = 1
Text = "Text2"
Top = 1320
Width = 1215
End
Begin VB.TextBox Text1
Height = 270
Left = 1560
TabIndex = 0
Text = "Text1"
Top = 960
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Const WM_LBUTTONDOWN = &H201

Dim Tray As New TrayClass

Private Sub Command1_Click()
Tray.PopupBallon Text1, Text2, NIIF_INFO
End Sub

Private Sub Form_Load()
Tray.AddTray Icon, Picture1.hWnd, , "托盘示例"
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case X / 15
Case WM_LBUTTONDOWN
If Me.Visible Then Tray.SendFormToTray Icon, Picture1.hWnd, Me.hWnd, , "托盘示例" Else Tray.RestoreFormFromTray False
End Select
End Sub

再把下面的代码用记事本保存成TrayClass.cls
添加到工程里
==============TrayClass.cls================
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "TrayClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private Declare Function DrawAnimatedRects Lib "user32" (ByVal hWnd As Long, ByVal idAni As Long, lprcFrom As RECT, lprcTo As RECT) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) 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 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 Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2
Private Const WM_USER = &H400
Private Const NIF_ICON As Long = &H2
Private Const NIF_INFO As Long = &H10
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_STATE As Long = &H8
Private Const NIF_TIP As Long = &H4
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const NIM_SETVERSION As Long = &H4
Private Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_MBUTTONDOWN = &H207
Private Const WM_MBUTTONUP = &H208
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_MBUTTONDBLCLK = &H209
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_SHOWWINDOW = &H18
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_MINIMIZE = &H20000000
Private Const WS_MAXIMIZE = &H1000000
Private Const CS_NOCLOSE = &H200
Private Const GWL_WNDPROC = (-4)
Private Const GWL_STYLE = (-16)

Public Enum BallIIcon
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
End Enum

Public Enum TrayCallBackConstants
TRAY_CALLBACK = (WM_USER + 1001&)
WM_MOUSEMOVE = &H200
End Enum

Private Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 128
dwState As Long
dwStateMask As Long
szInfo As String * 256
uTimeoutOrVersion As Long
szInfoTitle As String * 64
dwInfoFlags As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Dim RCWnd As RECT, RCTray As RECT, NIMData As NOTIFYICONDATA, Trayed As Boolean, OWP As Long, frmOwner As Form
Dim m_hWndMessageReceive As Long, m_hWndOwner As Long, m_TrayTip As String, m_TrayIcon As Long, m_CallBackType As Long

Private Function RebuildRect()
Dim hWndTarget As Long
hWndTarget = FindWindow("Shell_TrayWnd", vbNullString)
hWndTarget = FindWindowEx(hWndTarget, 0, "TrayNotifyWnd", vbNullString)
GetWindowRect m_hWndOwner, RCWnd
GetWindowRect hWndTarget, RCTray
End Function

Private Function fAction(ToTray As Boolean)
RebuildRect
If ToTray Then
DrawAnimatedRects m_hWndOwner, 3, RCWnd, RCTray
Else
DrawAnimatedRects m_hWndOwner, 3, RCTray, RCWnd
End If
End Function

Private Function InitTray()
Dim X As Long, T As Long
NIMData.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
If Not Trayed Then
Do
NIMData.uId = CLng(Rnd * 32768)
X = Shell_NotifyIcon(NIM_ADD, NIMData)
T = T + 1
If T > 50 Then Exit Function
Loop Until X <> 0
Trayed = True
Else
Shell_NotifyIcon NIM_MODIFY, NIMData
End If
End Function

Public Function TestFunc()
TestFunc = FindWindow("Shell_TrayWnd", vbNullString)
TestFunc = FindWindowEx(TestFunc, 0, "TrayNotifyWnd", "TrayNotifyWnd")
End Function

Public Function SendFormToTray(IconHandel As Long, hWndMessageReceive As Long, hWndOwner As Long, Optional CallBackType As TrayCallBackConstants = WM_MOUSEMOVE, Optional TrayTip As Variant = "Tray") As Long
If m_hWndOwner <> 0 Then Exit Function
m_hWndMessageReceive = hWndMessageReceive
m_CallBackType = CallBackType
m_hWndOwner = hWndOwner
fAction True
AddTray IconHandel, m_hWndMessageReceive, CallBackType, TrayTip
ShowWindow m_hWndOwner, SW_HIDE
End Function

Public Function RestoreFormFromTray(Optional bRemove As Boolean = True)

fAction False
If m_hWndOwner = 0 Then Exit Function
If bRemove Then RemoveTray
Dim ThreadFocus As Long, ThreadForm As Long
ThreadFocus = GetWindowThreadProcessId(GetForegroundWindow, 0)
ThreadForm = GetWindowThreadProcessId(m_hWndOwner, GetCurrentThreadId)
If ThreadFocus <> ThreadForm Then AttachThreadInput ThreadFocus, ThreadForm, True
SetWindowPos m_hWndOwner, 0, 0, 0, 0, 0, &H40 + 3
BringWindowToTop m_hWndOwner
SetForegroundWindow m_hWndOwner
DoEvents
If ThreadFocus <> ThreadForm Then AttachThreadInput ThreadFocus, ThreadForm, False
m_hWndOwner = 0
End Function

Public Function AddTray(IconHandel As Long, hWndMessageReceive As Long, Optional CallBackType As TrayCallBackConstants = WM_MOUSEMOVE, Optional TrayTip As Variant = "Tray") As Long
m_hWndMessageReceive = hWndMessageReceive
m_CallBackType = CallBackType
NIMData.cbSize = Len(NIMData)
NIMData.hWnd = m_hWndMessageReceive
If Not IsMissing(TrayTip) Then m_TrayTip = CStr(TrayTip) & vbNullChar
NIMData.szTip = m_TrayTip
NIMData.hIcon = CLng(IconHandel)
m_TrayIcon = NIMData.hIcon
NIMData.uCallBackMessage = m_CallBackType
InitTray
End Function

Public Function RemoveTray()
Class_Terminate
End Function

Public Function RebuildTray()
RemoveTray
InitTray
End Function

Public Function PopupBallon(Prompt As String, Optional Title As String, Optional DisplayIcon As BallIIcon, Optional TimeOut As Long = 10000) As Long
NIMData.szInfo = Prompt & vbNullChar
NIMData.szInfoTitle = Title & vbNullChar
NIMData.dwInfoFlags = DisplayIcon
NIMData.uFlags = NIF_INFO
NIMData.uTimeoutOrVersion = TimeOut
Shell_NotifyIcon NIM_MODIFY, NIMData
End Function

Public Property Get TrayTip() As String
TrayTip = m_TrayTip
End Property

Public Property Let TrayTip(ByVal newTip As String)
m_TrayTip = newTip
NIMData.szTip = newTip & vbNullChar
NIMData.uFlags = NIF_TIP
Shell_NotifyIcon NIM_MODIFY, NIMData
End Property

Public Property Get TrayIcon() As Long
TrayTip = m_TrayTip
End Property

Public Property Let TrayIcon(ByVal newIcon As Long)
m_TrayIcon = newIcon
NIMData.hIcon = newIcon
NIMData.uFlags = NIF_ICON
Shell_NotifyIcon NIM_MODIFY, NIMData
End Property

Public Property Get TaskBarCreateEvent() As Long
Static h As Long
If h = 0 Then h = RegisterWindowMessage("TaskbarCreated")
TaskBarCreateEvent = h
End Property

Private Sub Class_Terminate()
On Error Resume Next
Shell_NotifyIcon NIM_DELETE, NIMData
Trayed = False
End Sub
蜜蜂小田
2008-10-27 · TA获得超过188个赞
知道答主
回答量:76
采纳率:0%
帮助的人:0
展开全部
由于 x / Screen.TwipsPerPixelX得到的值是恒定的,与Const WM_LBUTTONUP = &H202 进行比较的。窗体宽度的容忍值也就只有这么大,所以,即便是你调X或者WM_LBUTTONUP变小也无济于事。因为只要窗体宽度大于x / Screen.TwipsPerPixelX的值,你的 Form_MouseMove 事件便失效。

解决办法

窗体上放一个 picturebox 的控件,然后把 浏览器对象赋予 pictureBOX 控件对象中即可。

form_load事件里加上
Set Me.WebBrowser1.Container = Me.picture

Private Sub Timer1_Timer()
Me.Caption = WebBrowser1.LocationName

If InStr(Me.Caption, "赢客服-管理端") > 0 Then
Me.Width = 11700
Me.Height = 9100
WebBrowser1.Width = 11700
WebBrowser1.Height = 9100
pic.Width = 11700
pic.Height = 9100
WebBrowser1.ToolBar = 0
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
Timer1.Enabled = False
End If
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ljl88900
2008-10-25 · TA获得超过2661个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2624万
展开全部
除了把下面代码替换相应代码外,其他代码不变

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 And Me.WindowState = 1 Then
Me.WindowState = 0 '程序回复到Normal状态
Me.Visible = True '从任务栏中清除图标
Call Shell_NotifyIcon(NIM_DELETE, nfIconData) '退出图标
End If
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式