VB.net 把窗口放在前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6737727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:37:11  来源:igfitidea点击:

VB.net Bring Window to Front

vb.netwinforms

提问by developer__c

What code in VB.net 2010 do I need to set a window to come to the front of the screen.

我需要在 VB.net 2010 中设置一个窗口到屏幕前面的什么代码。

What I am trying to achieve is to display an urgent alert type, its a form, for certain reasons I am not using message box.

我想要实现的是显示紧急警报类型,它是一种形式,出于某些原因我没有使用消息框。

Someone suggested the following code, but this does not work:

有人建议使用以下代码,但这不起作用:

  Private Sub frmMessage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.BringToFront()
    End Sub

回答by ste.xin

It should be enough that you set property TopMostof the window that you need to get on the top of the others.

设置需要位于其他窗口顶部的窗口的TopMost属性就足够了。

Form.TopMost = True

Form.TopMost = True

回答by sealz

try

尝试

me.Activate()

This outta do the trick

这出戏

EDIT: I googled to find backup for my answer

编辑:我用谷歌搜索为我的答案找到备份

My Case

我的情况

EDIT2:

编辑2:

There seems to be a few things that work. the Above as well as

似乎有几件事情有效。以上以及

''depending on setup
Me.Show
Form2.Show()

also

Form2.ShowDialog()

also

Form2.Visible = True

回答by Brian Graham

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Now, take the name of the window you want to bring to the front, and do the following:

现在,取您想要放在前面的窗口的名称,然后执行以下操作:

string name = "Untitled - Notepad";
IntPtr ptr = FindWindow(null, name);
SetForegroundWindow(ptr);

This will bring the window to the front of the screen.

这会将窗口带到屏幕的前面。

回答by dbasnett

Try using the .Shown event. Here is the code for a three form test. At the end of the button click event, Form3 should be on top of Form2, on top of Form1.

尝试使用.Shown 事件。这是三形式测试的代码。在按钮单击事件结束时,Form3 应该在 Form2 之上,在 Form1 之上。

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.SendToBack()
        Dim f2 As New Form2
        f2.Show()
        Dim f3 As New Form3
        f3.Show()
    End Sub
End Class

Public Class Form2
    Private Sub Form2_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        Me.BringToFront()
    End Sub
End Class

Public Class Form3
    Private Sub Form3_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        Me.BringToFront()
    End Sub
End Class

回答by Andrei Rojas

If what you want is bring to from, after the winfom has lost focus or it has been minimized. In my case, work when I open a winform at a button.

如果你想要的是从,在 winfom 失去焦点或它已被最小化之后。在我的情况下,当我在一个按钮上打开一个 winform 时工作。

    frmProducts.Show()
    'Retorre the original State
    frmProducts.BringToFront()
    frmProducts.WindowState = FormWindowState.Normal

回答by fcm

Bring window to front from where?

从哪里把窗户拉到前面?

MDI

计量吸入器

In MDI with multiple forms, form.BringToFront()will be enough, this will move the form on top within your application. You can also use form.ShowDialog()method when presenting the warning/error.

在具有多个表单的 MDI 中,form.BringToFront()就足够了,这会将表单移动到您的应用程序的顶部。您也可以form.ShowDialog()在显示警告/错误时使用方法。

Desktop

桌面

On your desktop you may have multiple application, you better go setting the Application as a TopMost.

在您的桌面上,您可能有多个应用程序,最好将应用程序设置为 TopMost。

If your application is behind another windows, the warning message may not be visible.

如果您的应用程序位于其他窗口之后,则可能看不到警告消息。

To bring the applicationto the front you need some more extra work, this are 'extensions' to the 'form' class, so the use will be: form.MakeTopMost():

要将application放在前面,您需要做一些额外的工作,这是“表单”类的“扩展”,因此用途是form.MakeTopMost()

<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private 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 ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private ReadOnly HWND_NOTOPMOST As New IntPtr(-2)

<System.Runtime.CompilerServices.Extension()> _
Public Sub MakeTopMost(frm As Form)
    SetWindowPos(frm.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub

<System.Runtime.CompilerServices.Extension()> _
Public Sub MakeNormal(frm As Form)
    SetWindowPos(frm.Handle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub

as always, the extension code need to be in a separate module.

与往常一样,扩展代码需要在一个单独的模块中。

回答by betrice mpalanzi

see folowing example when form load

表单加载时请参见以下示例

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.TopMost = True

End Sub

回答by betrice mpalanzi

Try this:

尝试这个:

Me.ShowDialog()

should help.

应该有帮助。

回答by shadetree

My requirement was to pop up a possibly minimized application and have it maximized and active. after scouring the net a bit, i found part of the answer in a c++ forum.

我的要求是弹出一个可能最小化的应用程序并使其最大化并处于活动状态。在网上搜索了一下之后,我在一个 c++ 论坛中找到了部分答案。

WindowState = FormWindowState.Maximized
Me.Activate()

This took my application in any state (maxxed, mini'd, big, small, behind stuff). it brought it to the front and maximized it on the screen. i.e. popped it up on the screen so i could see it!

这使我的应用程序处于任何状态(maxxed、mini'd、big、small、后面的东西)。它将它带到前面并在屏幕上最大化它。即弹出它在屏幕上,所以我可以看到它!

回答by user2364446

When nothing works try right click and select Bring to Front. If other images are covering the one that must be in the front, just right click on each one and select Send to Back as needed.

当没有任何效果时,请尝试右键单击并选择置于前面。如果其他图像覆盖了必须位于前面的图像,只需右键单击每个图像并根据需要选择“置于后置”。