如何将我的应用程序窗口置于前面?例如,我的应用程序需要注意.
这是我的个人计划.我需要这个功能.
这就是我得到的.但它没有 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我从背景工作者那里打电话给他们.
BringToFrontToEnterCaptha();
while (!ready)
{
Thread.Sleep(100);
}
BringToBackAfterEnterCaptha();
Thread.Sleep(300);
Run Code Online (Sandbox Code Playgroud)
按"接受"按钮后,bool ready设置为true.
我工作得很好,但并不总是.
Sha*_*ain 160
这是一段对我有用的代码
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
Run Code Online (Sandbox Code Playgroud)
它总是将所需的窗口带到所有其他窗口的前面.
as-*_*cii 58
使用Form.Activate()或Form.Focus()方法.
Den*_*mit 55
虽然我同意所有人的意见,但这是不太好的行为,这里是代码:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
SetForegroundWindow(Handle.ToInt32());
Run Code Online (Sandbox Code Playgroud)
更新
大卫是完全正确的,为了完整性我在这里包括了大卫的点数+1!
Jon*_*Jon 36
myForm.BringToFront();
Run Code Online (Sandbox Code Playgroud)
lup*_*pok 22
这工作:
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
else
{
TopMost = true;
Focus();
BringToFront();
TopMost = false;
}
Run Code Online (Sandbox Code Playgroud)
小智 18
在绊倒这篇文章之前,我提出了这个解决方案 - 切换TopMost属性:
this.TopMost = true;
this.TopMost = false;
Run Code Online (Sandbox Code Playgroud)
我在我的表单的构造函数中有这个代码,例如:
public MyForm()
{
//...
// Brint-to-front hack
this.TopMost = true;
this.TopMost = false;
//...
}
Run Code Online (Sandbox Code Playgroud)
我使用SwitchToThisWindow将应用程序带到最前端,如下例所示:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
159299 次 |
| 最近记录: |