我怎样才能把我的申请窗口放在前面?

如何使我的应用程序窗口前面? 例如,我的应用程序需要注意什么。

这是我的个人程序,我需要那个功能。

这是我得到的。但它的 没有工作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);

并在按“接受”按钮布尔准备设置为真。

我工作得很好,但并不总是如此。

220158 次浏览

Use Control.BringToFront:

myForm.BringToFront();

Use Form.Activate() or Form.Focus() methods.

While I agree with everyone, this is no-nice behavior, here is code:

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




SetForegroundWindow(Handle.ToInt32());

Update

David is completely right, for completeness I include the list of conditions that must apply for this to work (+1 for David!):

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

Here is a piece of code that worked for me

this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;

It always brings the desired window to the front of all the others.

this works:

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

Before stumbling onto this post, I came up with this solution - to toggle the TopMost property:

this.TopMost = true;
this.TopMost = false;

I have this code in my form's constructor, eg:

public MyForm()
{
//...


// Brint-to-front hack
this.TopMost = true;
this.TopMost = false;


//...
}

I use SwitchToThisWindow to bring the application to the forefront as in this example:

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);


}