简体   繁体   English

VB.net 将 Window 带到前面

[英]VB.net Bring Window to Front

What code in VB.net 2010 do I need to set a window to come to the front of the screen. VB.net 2010 中的什么代码我需要设置一个 window 到屏幕前面。

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

It should be enough that you set property TopMost of the window that you need to get on the top of the others.设置 window 的属性TopMost就足够了,您需要在其他对象之上。

Form.TopMost = True

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
<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:现在,取您想要放在前面的 window 的名称,然后执行以下操作:

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

This will bring the window to the front of the screen.这会将 window 带到屏幕的前面。

If what you want is bring to from, after the winfom has lost focus or it has been minimized.如果您想要的是在 winfom 失去焦点或已最小化之后带来的结果。 In my case, work when I open a winform at a button.就我而言,当我在一个按钮上打开一个 winform 时工作。

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

Try using the .Shown event .尝试使用.Shown 事件 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.在按钮单击事件结束时,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

Bring window to front from where?从哪里把 window 带到前面?

MDI计量吸入器

In MDI with multiple forms, form.BringToFront() will be enough, this will move the form on top within your application.在具有多个 forms 的 MDI 中, form.BringToFront()就足够了,这会将表单移动到应用程序的顶部。 You can also use form.ShowDialog() method when presenting the warning/error.您还可以在显示警告/错误时使用form.ShowDialog()方法。

Desktop桌面

On your desktop you may have multiple application, you better go setting the Application as a TopMost.在您的桌面上,您可能有多个应用程序,最好 go 将应用程序设置为 TopMost。

If your application is behind another windows, the warning message may not be visible.如果您的应用程序位于另一个 windows 之后,则可能看不到警告消息。

To bring the application to the front you need some more extra work, this are 'extensions' to the 'form' class, so the use will be: form.MakeTopMost() :要将application放在前面,您需要做一些额外的工作,这是对“表单”class 的“扩展”,因此使用如下: 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.与往常一样,扩展代码需要位于单独的模块中。

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).这将我的应用程序用于任何 state(最大、迷你、大、小、后面的东西)。 it brought it to the front and maximized it on the screen.它把它带到前面并在屏幕上最大化。 ie popped it up on the screen so i could see it!即在屏幕上弹出它,所以我可以看到它!

When nothing works try right click and select Bring to Front.如果没有任何效果,请尝试右键单击并将 select 置于前面。 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.如果其他图像覆盖了必须在前面的图像,只需右键单击每个图像,然后根据需要将 select 发送到后面。

I use:我用:

dim tempResult as dialogResult = frmName.showDialog()

and in the called form:并以被调用的形式:

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

The calling form code waits for the called forms result before continuing execution.调用表单代码在继续执行之前等待被调用的 forms 结果。

Just set the Owner property of the form you want to appear on top:只需设置要显示在顶部的表单的Owner属性:

Dim frmMessage As New Form()
frmMessage.Owner = frmMain   'frmMain is the form creating the message window
frmMessage.Show()

Now frmMessage will always be on top of frmMain , regardless of focus.现在frmMessage将始终位于frmMain之上,无论焦点如何。

As another user posted, one of my favorite ways is to set the forms owner.正如另一位用户发布的那样,我最喜欢的方法之一是设置 forms 所有者。 By doing this, the child form will always sit on top of the parent form when either form is focused, activated, etc... What's nice about this is that you don't have to catch any special events and execute any special code.通过这样做,当任一表单被聚焦、激活等时,子表单将始终位于父表单之上。这样做的好处是您不必捕获任何特殊事件并执行任何特殊代码。 Suppose you have a main form frmMain and a popup form frmPopup you could use the following code to ensure the popup is always on top of the main form without using topmost (which works but can have some bad side affects).假设您有一个主窗体 frmMain 和一个弹出窗体 frmPopup,您可以使用以下代码来确保弹出窗口始终位于主窗体的顶部,而无需使用 topmost(这可行,但可能会产生一些不好的副作用)。

frmPopup.show(frmMain)

or you could use the longer version (as posted above by someone或者您可以使用更长的版本(如上面某人发布的那样

frmPopup.Owner = frmMain
frmPopup.show()

Another thing that is great about this is you can also use it with ShowDialog()另一件很棒的事情是您也可以将它与 ShowDialog() 一起使用

frmPopup.ShowDialog(frmMain)

I know this is an old post but perhaps people still looking for easy solutions to this will find this.我知道这是一篇旧帖子,但也许仍在寻找简单解决方案的人会发现这一点。 It has really helped improve the functionality of my programs with a lot less code than I was using before.与我以前使用的代码相比,它确实有助于改进我的程序的功能。

Draw a visible top most form off of the screen and then make this form the owner of the ShowDialog() call.从屏幕上绘制一个可见的最顶部窗体,然后使该窗体成为 ShowDialog() 调用的所有者。

I know this is a little old but I had a similar problem today this is how I solved it.我知道这有点旧,但我今天遇到了类似的问题,这就是我解决它的方法。 It works as long as you don't mind closing the open form and creating a new one.只要您不介意关闭打开的表单并创建一个新表单,它就可以工作。

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

A bit off the OP... I have a "dashboard" that I open from a menu.有点偏离 OP... 我有一个从菜单打开的“仪表板”。 The user can switch to other windows and then "load" the dashboard again.用户可以切换到其他 windows 然后再次“加载”仪表板。 If it is already loaded it is brought to the front.如果它已经加载,它会被带到前面。

Declare frmFISDash = frmFISDashboard "global"声明 frmFISDash = frmFISDashboard "global"

    If frmFISDash Is Nothing Then
        frmFISDash = New frmFISDashboard
        frmFISDash.Show()
    Else
        frmFISDash.WindowState = FormWindowState.Normal
        frmFISDash.BringToFront()
    End If

Note the setting of.WindowsState - if the form is minimized.bringToFront does not work.注意 .WindowsState 的设置 - 如果表单被最小化。bringToFront 不起作用。

I solved it this way (my be of use to somebody) - this way it brings the hidden form to front even in debug mode:我以这种方式解决了它(我对某人有用)-即使在调试模式下,它也会将隐藏的表单带到前面:

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

To take what Jim Nolan said to do based on his description.根据他的描述采取吉姆诺兰所说的去做。 This is what the best way of handling to make sure the form is properly at the front of all other forms as well as addressing disposing the form, assigning ownership of the new form, and showing the form这是确保表单正确位于所有其他 forms 前面以及处理表单、分配新表单的所有权和显示表单的最佳处理方式

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

The following seemed easiest to me to bring up a form called "Projects" after clicking on a menu choice.在单击菜单选项后,以下对我来说似乎最容易调出一个名为“项目”的表单。 Form will be loaded if necessary, UN-minimized if necessary, and brought to front ('focus').如有必要,将加载表格,如有必要,将其最小化,并放在前面(“焦点”)。

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

see folowing example when form load表单加载时请参见以下示例

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

    Me.TopMost = True

End Sub

Try this:尝试这个:

Me.ShowDialog()

should help.应该有帮助。

a little trick:一个小技巧:

me.hide()
me.visible = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM