如何将弹出窗口显示在最前面

fanncy1206 2005-01-10 03:43:32
有一个客服程序,根据来电号码显示客户的详细信息。这个程序一直在后端运行!
当有电话来的时候需要弹出到所有窗口的最前端!

我用了以下代码显示:
Public Sub Show_Windows()
'弹出窗口
If Form.WindowState = vbMinimized Then
FormCSAir.WindowState = vbNormal
FormCSAir.Show
Else
FormCSAir.Show
End If
End Sub

可测试下来,有的时候会被一些窗口盖住,不能显示到最前面
请哪位大虾指点一下?谢谢

不知用那个API函数可以解决?


...全文
1060 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh150304 2010-06-09
  • 打赏
  • 举报
回复
嗯 学习了!
fanncy1206 2005-01-12
  • 打赏
  • 举报
回复
谢谢大家!
我找到解决方法了!

Option Explicit
'
' Required Win32 API Declarations
'
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) 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 GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
'
' Constants used with APIs
'
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9

Public Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean
Dim ThreadID1 As Long
Dim ThreadID2 As Long
Dim nRet As Long
'
' Nothing to do if already in foreground.
'
If hWnd = GetForegroundWindow() Then
ForceForegroundWindow = True
Else
'
' First need to get the thread responsible for this window,
' and the thread for the foreground window.
'
ThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
ThreadID2 = GetWindowThreadProcessId(hWnd, ByVal 0&)
'
' By sharing input state, threads share their concept of
' the active window.
'
If ThreadID1 <> ThreadID2 Then
Call AttachThreadInput(ThreadID1, ThreadID2, True)
nRet = SetForegroundWindow(hWnd)
Call AttachThreadInput(ThreadID1, ThreadID2, False)
Else
nRet = SetForegroundWindow(hWnd)
End If
'
' Restore and repaint
'
If IsIconic(hWnd) Then
Call ShowWindow(hWnd, SW_RESTORE)
Else
Call ShowWindow(hWnd, SW_SHOW)
End If
'
' SetForegroundWindow return accurately reflects success.
'
ForceForegroundWindow = CBool(nRet)
End If
End Function
KissApple 2005-01-10
  • 打赏
  • 举报
回复
最简单的办法
form.Show
form.ZOrder
不用API,不知道能不能解决你的问题
zhengoodman 2005-01-10
  • 打赏
  • 举报
回复
使窗体保持在最上层
有时应用程序需要一个信息或查询窗体总保持在最上层,即使用读书切换到其他窗体也能看到该窗体,如Microsoft Word中的Find窗体,这是通过调用Windows API函数SetWindowPos()实现的,示例如下:
Option Explicit
Private Declare Function SetWindowPos Lib“User32”(ByValhwnd As Long,ByVal h WndInsertAfter As Long,ByVal x As Long,ByVAl y As Long,ByVal cx As Long,ByVal cy As Long,ByVal wFlags As Long)As Long
Const HWND_TOPMOST=-1
Const SWP_SHOWWINDOW=&H40
Private Sub Form_load()
Dim retValue As Long
RetValue=setWindowPos(me.hwnd,HWND_TOPMOST,Me.CurrentX,Me.CurrentY,300,300,SWP_SHOWWINDOW)
End Sub
fanncy1206 2005-01-10
  • 打赏
  • 举报
回复
是不是先show window
然后 调用 SetWindowPos
东方冉 2005-01-10
  • 打赏
  • 举报
回复
一个简单的应用程序.

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 Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const Flags = SWP_NOMOVE Or SWP_NOSIZE

Private Sub Check1_Click()
Dim res As Long
If Check1.Value = 1 Then
res = SetWindowPos(Me.hwnd, HWND_TOPMOST, _
0, 0, 0, 0, Flags)
Else
res = SetWindowPos(Me.hwnd, HWND_NOTOPMOST, _
0, 0, 0, 0, Flags)

End If
End Sub
bu_wen 2005-01-10
  • 打赏
  • 举报
回复
参数定义
hwnd ----------- Long,欲定位的窗口

hWndInsertAfter - Long,窗口句柄。在窗口列表中,窗口hwnd会置于这个窗口句柄的后面。也可能选用下述值之一:
HWND_BOTTOM
将窗口置于窗口列表底部
HWND_TOP
将窗口置于Z序列的顶部;Z序列代表在分级结构中,窗口针对一个给定级别的窗口显示的顺序
HWND_TOPMOST
将窗口置于列表顶部,并位于任何最顶部窗口的前面
HWND_NOTOPMOST
将窗口置于列表顶部,并位于任何最顶部窗口的后面

x -------------- Long,窗口新的x坐标。如hwnd是一个子窗口,则x用父窗口的客户区坐标表示

y -------------- Long,窗口新的y坐标。如hwnd是一个子窗口,则y用父窗口的客户区坐标表示

cx ------------- Long,指定新的窗口宽度

cy ------------- Long,指定新的窗口高度

wFlags --------- Long,包含了旗标的一个整数
SWP_DRAWFRAME
围绕窗口画一个框
SWP_HIDEWINDOW
隐藏窗口
SWP_NOACTIVATE
不激活窗口
SWP_NOMOVE
保持当前位置(x和y设定将被忽略)
SWP_NOREDRAW
窗口不自动重画
SWP_NOSIZE
保持当前大小(cx和cy会被忽略)
SWP_NOZORDER
保持窗口在列表的当前位置(hWndInsertAfter将被忽略)
SWP_SHOWWINDOW
显示窗口
SWP_FRAMECHANGED
强迫一条WM_NCCALCSIZE消息进入窗口,即使窗口的大小没有改变

东方冉 2005-01-10
  • 打赏
  • 举报
回复
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)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧