VB.net将窗口带到前面

VB.net将窗口带到前面,vb.net,winforms,Vb.net,Winforms,在VB.net 2010中,我需要设置一个窗口以显示在屏幕前面,这需要什么代码 我试图实现的是显示一个紧急警报类型,它是一个表单,出于某些原因,我没有使用消息框 有人建议使用以下代码,但这不起作用: Private Sub frmMessage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.BringToFront() End Sub 试一试 这是不可能的 编

在VB.net 2010中,我需要设置一个窗口以显示在屏幕前面,这需要什么代码

我试图实现的是显示一个紧急警报类型,它是一个表单,出于某些原因,我没有使用消息框

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

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

这是不可能的

编辑:我在谷歌上找到了答案的备份

编辑2:

似乎有一些事情是有效的。上述以及

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


这将使窗口位于屏幕前面。

尝试使用显示的事件。下面是三种形式测试的代码。在按钮点击事件的末尾,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

将窗口的属性
设置在最顶部就足够了,您需要将该属性设置在其他属性的顶部

Form.TopMost=True

我的要求是弹出一个可能最小化的应用程序,使其最大化并处于活动状态。在浏览了一下网络之后,我在C++论坛上找到了答案的一部分。
WindowState = FormWindowState.Maximized
Me.Activate()

这使我的应用程序处于任何状态(maxx、mini'd、大、小、落后)。它把它带到前面,并在屏幕上最大化它。i、 在屏幕上弹出它,这样我就可以看到它了

当什么都不起作用时,请尝试右键单击并选择“带到前面”。如果其他图像覆盖了必须在前面的图像,只需右键单击每个图像并根据需要选择“发送到后面”。

我使用:

dim tempResult as dialogResult = frmName.showDialog()
以所谓的形式:

me.dialogResult = dialogResult.{OK, Abort, whatever}

调用表单代码在继续执行之前等待调用的表单结果。

只需设置要显示在顶部的表单的
所有者属性:

Dim frmMessage As New Form()
frmMessage.Owner = frmMain   'frmMain is the form creating the message window
frmMessage.Show()
现在无论焦点如何,
frmMessage
始终位于
frmMain
之上。

小技巧:

me.hide()
me.visible = true

正如另一位用户所发布的,我最喜欢的方法之一是设置表单所有者。这样,当任一窗体被聚焦、激活等时,子窗体将始终位于父窗体的顶部。。。这样做的好处是,您不必捕获任何特殊事件并执行任何特殊代码。假设您有一个主窗体frmMain和一个弹出窗体frmPopup,您可以使用以下代码来确保弹出窗口始终位于主窗体的顶部,而不使用topmost(这可以工作,但可能会产生一些不良的副作用)

或者你可以使用较长的版本(如上面某人发布的)

frmPopup.Owner = frmMain
frmPopup.show()
另一个很棒的地方是,您还可以将它与ShowDialog()一起使用


我知道这是一篇老文章,但也许仍在寻找简单解决方案的人们会发现这一点。它确实帮助我改进了程序的功能,使用的代码比以前少得多。

从屏幕上画出一个可见的最上面的表单,然后使该表单成为ShowDialog()的所有者打电话。

我知道这有点旧,但我今天遇到了一个类似的问题,这就是我如何解决它的。只要您不介意关闭打开的表单并创建一个新表单,它就可以工作

 Dim MyRemoteForm As New Form
    MyRemoteForm = Application.OpenForms("frmManualRemote")
    If MyRemoteForm Is Nothing Then
        frmManualRemote.Show()
    Else
        frmManualRemote.Close()
        frmManualRemote.Show()
    End If

我有一个从菜单打开的“仪表板”。用户可以切换到其他窗口,然后再次“加载”仪表板。如果仪表板已经加载,则将其置于前面

声明frmFISDash=FRMFISDASHORD“全局”

请注意.WindowsState的设置-如果窗体被最小化,则.bringToFront不起作用。

我通过这种方式解决了它(我对某人有用)-这种方式即使在调试模式下也会将隐藏的窗体置于最前面:

Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
    If Me.WindowState = FormWindowState.Minimized Then
        HideForm()
    Else
        BringFormToFront()
    End If
End Sub


Private Sub NotifyIcon_Click(sender As Object, e As EventArgs) Handles NotifyIcon.Click
    'Determine which mouse button was pressed, in order to differentiate between Left/Right Mouse Button
    Dim MouseButton As System.Windows.Forms.MouseEventArgs = CType(e, MouseEventArgs)
    If MouseButton.Button = MouseButtons.Left Then
        BringFormToFront()
    End If
End Sub

Private Sub NotifyIcon_DoubleClick(sender As Object, e As EventArgs) Handles NotifyIcon.DoubleClick
    BringFormToFront()
End Sub

Private Sub HideForm()
    Me.NotifyIcon.Visible = True
    Me.ShowInTaskbar = False
    'Windowstate controlled by user when minimizing Form
   Msgbox("Minimized, click on Notify Icon to show")
End Sub

Private Sub BringFormToFront()
    Me.NotifyIcon.Visible = False
    Me.ShowInTaskbar = True
    Me.WindowState = FormWindowState.Normal
End Sub

根据Jim Nolan的描述,采取Jim Nolan所说的操作。这是确保表单正确位于所有其他表单前面的最佳处理方式,以及处理表单、分配新表单的所有权和显示表单的最佳方式

Dim form As Form = new Form
form.TopMost = True
form.Owner = Me
form.ShowDialog()
form.Dispose()

在点击一个菜单选项后,我觉得下面的内容最容易显示一个名为“Projects”的表单。表单将在必要时加载,在必要时取消最小化,并显示在最前面(“焦点”)


从哪里把窗户带到前面

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

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

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

要将
应用程序
带到前端,您需要做一些额外的工作,这是对“form”类的“扩展”,因此其用途是:
form.maketopmest()

_
私有函数SetWindowPos(ByVal hWnd作为IntPtr,ByVal hwninsertafter作为IntPtr,ByVal X作为整数,ByVal Y作为整数,ByVal cx作为整数,ByVal cy作为整数,ByVal uFlags作为整数)作为布尔值
端函数
私有只读硬件和最顶层作为新IntPtr(-1)
私有只读HWND_NOTOPMOST作为新IntPtr(-2)
_
公共子MakeTopMost(frm作为表格)
SetWindowPos(frm.Handle()、最上面的HWND_、0、0、0、SWP_NOMOVE或SWP_NOSIZE)
端接头
_
公共子规范(frm作为表格)
SetWindowPos(frm.Handle()、HWND_NOTOPMOST、0、0、0、SWP_NOMOVE或SWP_NOSIZE)
端接头

一如既往,扩展代码需要位于单独的模块中。

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

Private Sub Form1\u Load(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load

    Me.TopMost = True

End Sub

如果您想要的是在winfom失去焦点或最小化后从中获取。在我的情况下,当我按一个按钮打开winform时工作

    frmProducts.Show()
    'Retorre the original State
    frmProducts.BringToFront()
    frmProducts.WindowState = FormWindowState.Normal
试试这个:

Me.ShowDialog()

应该会有帮助。

请提供打开窗口的代码。只需frmMessage.Show()就可以了。您正在打开的表单或打开表单的方式必须有一些特定的信息。我刚刚创建了一个“hello world”项目;两个表单,每个表单都有一个打开另一个的按钮。打开的表单总是
frmPopup.ShowDialog(frmMain)
 Dim MyRemoteForm As New Form
    MyRemoteForm = Application.OpenForms("frmManualRemote")
    If MyRemoteForm Is Nothing Then
        frmManualRemote.Show()
    Else
        frmManualRemote.Close()
        frmManualRemote.Show()
    End If
    If frmFISDash Is Nothing Then
        frmFISDash = New frmFISDashboard
        frmFISDash.Show()
    Else
        frmFISDash.WindowState = FormWindowState.Normal
        frmFISDash.BringToFront()
    End If
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
    If Me.WindowState = FormWindowState.Minimized Then
        HideForm()
    Else
        BringFormToFront()
    End If
End Sub


Private Sub NotifyIcon_Click(sender As Object, e As EventArgs) Handles NotifyIcon.Click
    'Determine which mouse button was pressed, in order to differentiate between Left/Right Mouse Button
    Dim MouseButton As System.Windows.Forms.MouseEventArgs = CType(e, MouseEventArgs)
    If MouseButton.Button = MouseButtons.Left Then
        BringFormToFront()
    End If
End Sub

Private Sub NotifyIcon_DoubleClick(sender As Object, e As EventArgs) Handles NotifyIcon.DoubleClick
    BringFormToFront()
End Sub

Private Sub HideForm()
    Me.NotifyIcon.Visible = True
    Me.ShowInTaskbar = False
    'Windowstate controlled by user when minimizing Form
   Msgbox("Minimized, click on Notify Icon to show")
End Sub

Private Sub BringFormToFront()
    Me.NotifyIcon.Visible = False
    Me.ShowInTaskbar = True
    Me.WindowState = FormWindowState.Normal
End Sub
Dim form As Form = new Form
form.TopMost = True
form.Owner = Me
form.ShowDialog()
form.Dispose()
Private Sub ProjectsToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ProjectsToolStripMenuItem1.Click

    Me.Cursor = Cursors.WaitCursor       ' if form is slow to load for any reason
    Projects.Show()
    Me.Cursor = Cursors.Default
    Projects.WindowState = FormWindowState.Normal
    Projects.Focus()

End Sub
<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
    Me.TopMost = True

End Sub
    frmProducts.Show()
    'Retorre the original State
    frmProducts.BringToFront()
    frmProducts.WindowState = FormWindowState.Normal
Me.ShowDialog()