C# 如何将应用程序窗口置于最前面?

C# 如何将应用程序窗口置于最前面?,c#,.net,winforms,C#,.net,Winforms,如何将我的应用程序窗口置于最前面?例如,我的应用程序需要注意什么 这是我个人的节目。我需要这个功能 这就是我得到的。但它不是100%工作的 public void BringToFrontToEnterCaptha() { if (InvokeRequired) { Invoke(new Action(BringToFrontToEnterCaptha)); } else { this.TopMost = true;

如何将我的应用程序窗口置于最前面?例如,我的应用程序需要注意什么

这是我个人的节目。我需要这个功能

这就是我得到的。但它不是100%工作的

public void BringToFrontToEnterCaptha()
{
    if (InvokeRequired)
    {
        Invoke(new Action(BringToFrontToEnterCaptha));
    }
    else
    {
        this.TopMost = true;
        this.Focus();
        this.BringToFront();
        this.textBox1.Focus();
        this.textBox1.Text = string.Empty;
        System.Media.SystemSounds.Beep.Play();
    }
}

public void BringToBackAfterEnterCaptha()
{
    if (InvokeRequired)
    {
        Invoke(new Action(BringToBackAfterEnterCaptha));
    }
    else
    {
        this.TopMost = false;
    }
}
我从后台工作人员那里打电话给他们

BringToFrontToEnterCaptha();
while (!ready)
{
    Thread.Sleep(100);
}
BringToBackAfterEnterCaptha();
Thread.Sleep(300);
按下“接受”按钮后,布尔就绪设置为真

我工作很好,但并不总是这样

使用:


使用
Form.Activate()
Form.Focus()
方法。

虽然我同意大家的看法,但这不是一个好行为,下面是代码:

[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);   


SetForegroundWindow(Handle.ToInt32());
更新

David是完全正确的,为了完整性,我列出了必须适用于此的条件列表(+1代表David!):

  • 这个过程就是前景 过程
  • 这个过程是由 前景进程
  • 进程收到最后一个输入 事件
  • 没有前台进程
  • 前景进程正在进行中 调试
  • 前景未锁定(请参见) 锁设置(如图所示)
  • 前台锁定超时已停止 过期(见 SPI_GETFOREGROUNDLOCKTIMEOUT in 系统参数信息)
  • 没有激活的菜单

    • 这是一段对我有用的代码

      this.WindowState = FormWindowState.Minimized;
      this.Show();
      this.WindowState = FormWindowState.Normal;
      
      它总是将所需的窗口带到所有其他窗口的前面。

      此功能:

      if (WindowState == FormWindowState.Minimized)
          WindowState = FormWindowState.Normal;
      else
      {
          TopMost = true;
          Focus();
          BringToFront();
          TopMost = false;
      }
      

      在无意中读到这篇文章之前,我想出了一个解决方案——切换最顶层的属性:

      this.TopMost = true;
      this.TopMost = false;
      
      我在表单的构造函数中有此代码,例如:

      public MyForm()
      {
          //...
      
          // Brint-to-front hack
          this.TopMost = true;
          this.TopMost = false;
      
          //...
      }
      

      我使用SwitchToThis窗口将应用程序置于最前沿,如本例所示:

      static class Program
      {
          [DllImport("User32.dll", SetLastError = true)]
          static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
      
      
      
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main()
          {
              bool createdNew;
              int iP;
              Process currentProcess = Process.GetCurrentProcess();
              Mutex m = new Mutex(true, "XYZ", out createdNew);
              if (!createdNew)
              {
                  // app is already running...
                  Process[] proc = Process.GetProcessesByName("XYZ");
      
                  // switch to other process
                  for (iP = 0; iP < proc.Length; iP++)
                  {
                      if (proc[iP].Id != currentProcess.Id)
                          SwitchToThisWindow(proc[0].MainWindowHandle, true);
                  }
      
                  return;
              }
      
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new form());
              GC.KeepAlive(m);
      
          }
      
      静态类程序
      {
      [DllImport(“User32.dll”,SetLastError=true)]
      静态外部空隙切换到此窗口(IntPtr hWnd、bool fAltTab);
      /// 
      ///应用程序的主要入口点。
      /// 
      [状态线程]
      静态void Main()
      {
      布尔创造了新的;
      int iP;
      Process currentProcess=Process.GetCurrentProcess();
      互斥量m=新互斥量(true,“XYZ”,out createdNew);
      如果(!createdNew)
      {
      //应用程序已在运行。。。
      Process[]proc=Process.GetProcessesByName(“XYZ”);
      //切换到其他进程
      对于(iP=0;iP
      当你的应用程序决定需要注意时,它是否有输入焦点?输入焦点如何?没有输入焦点,你无法轻松地将应用程序放在前面。我添加此作为建议的注释:我害怕你的“个人”一天程序逃逸到世界上,并将自己强加给一些毫无戒心的用户。如果你需要,也许你的设计需要审查…@Vincentvancalberg,这是“私人的”程序。但是如果id没有为用户弹出,那么这个程序就没有意义了。整个想法都很好。现在它不再被使用了。有趣的是,我正在寻找一个通用的解决方案,将一个窗口放在前面,并找到这个答案,这个答案似乎与我的原因相同-局部验证码传递:)如果应用程序没有验证码,它会起作用吗没有输入焦点?这对我不起作用。@JonCage try
      Application.OpenForms[“yourForm”]。BringToFront();
      这是错误的。Control.BringToFront会将一个控件放在宿主控件的前面,例如,您可以将一个文本框放在标签的前面,以确保标签的空白不会遮挡文本框的一部分。表单从控件继承了此项,当它是MDI子级-也就是说,表单托管在另一个表单中。它不会将表单放在另一个应用程序前面。Form.Activate()可以工作,下面的答案是向上投票。请阅读要使其工作,必须应用的项目符号列表。Form.Activate()调用SetForegroundWindow()避免使用PInvoke。它对我的启动屏幕很有吸引力,thx:)这是唯一适合我的解决方案。我不知道的原因是Visual Studio在我运行项目后不会自动将焦点切换到应用程序。通常,对于我的所有其他应用程序,切换是自动的,但对于其中一个应用程序他们,我必须在程序开始时仅在调试模式下使用此破解。在这里尝试了所有提供的解决方案,这是唯一有效的解决方案,但Dennis的要点(来自Microsoft)没有完全一致。我已经在使用SetForeGroundindow()为了将一个不同的应用程序带到前台,我想弹出一个对话框,输入一些与该应用程序相关的信息。我尝试了这里提供的所有.Net解决方案(以及我自己的半打死胡同),然后看到这一点,想“当然是”.即使我违反了“没有前台进程”的规定,但我以管理员权限运行的事实可能是它工作的原因bullet point.在我用谷歌找到的所有方法中,每个人都坚持激活和聚焦是关键所在。这是唯一对我有效的方法。谢谢,因为我从来没有想过这个把戏。谢谢。在我找到的所有解决方案中,这是唯一一个似乎一直有效的方法。+1Brilliant!是唯一对我有用的东西。适用于WinForm。幸运的是,我可以最小化和还原我的应用程序,因为它没有任何可见的UI。不过,这在另一种情况下也很有用:谢谢-调用我的启动屏幕的Hide()方法(在Form::Load事件中)将我的GUI留在后台,只有这个(或者根据cii的答案调用Form.Activate())方法似乎可以将其恢复过来!
      Activate()
      解决方案更好。同样,在您的情况下,检查
      static class Program
      {
          [DllImport("User32.dll", SetLastError = true)]
          static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
      
      
      
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main()
          {
              bool createdNew;
              int iP;
              Process currentProcess = Process.GetCurrentProcess();
              Mutex m = new Mutex(true, "XYZ", out createdNew);
              if (!createdNew)
              {
                  // app is already running...
                  Process[] proc = Process.GetProcessesByName("XYZ");
      
                  // switch to other process
                  for (iP = 0; iP < proc.Length; iP++)
                  {
                      if (proc[iP].Id != currentProcess.Id)
                          SwitchToThisWindow(proc[0].MainWindowHandle, true);
                  }
      
                  return;
              }
      
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new form());
              GC.KeepAlive(m);
      
          }