[英]Bring VB.Net window on top of all windows
I have a window which should stay on top of Power point slide shows. 我有一个窗口应该保持在Power point幻灯片放映之上。 So it should be on top of all the windows.
所以它应该在所有窗口之上。 I did this easily using VB 6 using Lib "user32", but it seems to be difficut with VB.net.
我使用VB“user32”轻松地使用VB 6,但它似乎与VB.net不同。
Me.TopMost = True
This does not seem to work as it works only within the program. 这似乎不起作用,因为它只在程序中工作。
Private Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long
Private Sub frmTmr_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
BringWindowToTop(Me.Handle)
End Sub
This also gives a error! 这也会出错! Any help is greatly appreciated!
任何帮助是极大的赞赏! Thanks in advance,
提前致谢,
Regards 问候
Manjula 曼居拉
If you want a window in your application to always appear on top of a window of a different application, then the BringWindowToTop
function is definitely not what you want. 如果你想在你的应用程序的窗口总是出现在不同的应用程序的窗口的顶部,那么
BringWindowToTop
函数绝对不是你想要的。 For starters, as you've noticed, you have to repeatedly call the function using a timer. 对于初学者,正如您所注意到的,您必须使用计时器重复调用该函数。 That should be your first clue that it's the wrong API.
这应该是你的第一个线索,它是错误的API。 Another problem is that it's only bringing your window to the top of the Z order for its process, not all of the other processes running on the system.
另一个问题是,它只会将窗口置于其进程的Z顺序顶部,而不是系统上运行的所有其他进程。 As the documentation explains,
正如文档解释的那样,
Calling this function is similar to calling the
SetWindowPos
function to change a window's position in the Z order.调用此函数类似于调用
SetWindowPos
函数以Z顺序更改窗口的位置。BringWindowToTop
does not make a window a top-level window.BringWindowToTop
不会使窗口成为顶级窗口。
That last sentence should indicate that there is a better way. 最后一句应该表明有更好的方法。 Windows has built-in support for top-level windows (ie, those that should always appear on top of other windows): these are called top-most windows.
Windows内置支持顶级窗口(即应始终显示在其他窗口之上的窗口):这些窗口称为最顶层的窗口。 This is exactly what you want.
这正是你想要的。 Top-most windows always appear above non-topmost windows.
最顶层的窗口始终显示在非最顶层的窗口上方。
Raymond Chen attempts to explain some of the confusion on his blog . Raymond Chen试图在他的博客上解释一些混乱。 Note that in this case,
HWND_TOP
is equivalent to BringWindowToTop
. 请注意,在这种情况下,
HWND_TOP
等同于BringWindowToTop
。 Instead, you want HWND_TOPMOST
. 相反,你想要
HWND_TOPMOST
。
The simplest way of making a window top-most is to specify the WS_EX_TOPMOST
flag when you create the window. 制作窗口最顶层的最简单方法是在创建窗口时指定
WS_EX_TOPMOST
标志。 The .NET Framework hides most of the window creation work behind the scenes, but you can customize the parameters when you need to by overriding the CreateParams
property of your form class. .NET Framework隐藏了幕后的大部分窗口创建工作,但您可以在需要时通过覆盖窗体类的
CreateParams
属性来自定义参数。
Here's some sample code to make a form always top-most: 以下是一些示例代码,使表单始终位于最顶层:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Const WS_EX_TOPMOST As Integer = &H00000008
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TOPMOST
Return cp
End Get
End Property
This won't work if you want to toggle the top-most state of the window at run-time. 如果要在运行时切换窗口的最顶层状态,这将不起作用。 To do that, you're going to have to P/Invoke the
SetWindowPos
function. 要做到这一点,你将不得不P / Invoke
SetWindowPos
函数。 P/Invoke is similar to what you used to do in VB6 with the Declare
statement, but the semantics have changed slightly for the .NET world—that's why you can't use your old VB6 Declare
statements in VB.NET. P / Invoke类似于您在VB6中使用
Declare
语句所做的操作,但是.NET世界的语义略有改变 - 这就是为什么你不能在VB.NET中使用旧的VB6 Declare
语句。
Here's what that code might look like for VB.NET: 这是VB.NET的代码:
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Public Function MakeTopMost()
SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function
Public Function MakeNormal()
SetWindowPos(Me.Handle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.