C# 以编程方式设置焦点时,窗体边框和主题会闪烁
我有一个通常隐藏的应用程序,但我希望当我将鼠标光标移动到屏幕上的特定位置时,它能获得焦点 我已尝试使用以下代码激活表单:C# 以编程方式设置焦点时,窗体边框和主题会闪烁,c#,winforms,focus,windows-10,C#,Winforms,Focus,Windows 10,我有一个通常隐藏的应用程序,但我希望当我将鼠标光标移动到屏幕上的特定位置时,它能获得焦点 我已尝试使用以下代码激活表单: this.Show(); this.BringToFront(); this.Focus(); this.Activate(); 还尝试添加以下内容: [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); SetForegroundWindow(this.Handle)
this.Show();
this.BringToFront();
this.Focus();
this.Activate();
还尝试添加以下内容:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
SetForegroundWindow(this.Handle);
无论我做什么,我的应用程序都不会从当前活动的应用程序中窃取焦点,而是只会自身闪烁而不会获得焦点
有没有其他方法可以将窗体强制置于前台并聚焦?您可以尝试几种方法,然后选择一种适合您的方法。
这个应该总是把表格放在前面
this.TopMost = true;
this.ShowDialog();
您也可以尝试:
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
此外:
也许很难看,但试着称之为。TopMost=true;然后这个。TopMost=false;我马上就想到了。谢谢!首先最小化,然后不透明度为0(以避免取消最小化时出现动画),然后显示,然后是正常大小,最后不透明度恢复为1。很难看,但很管用。
this.BringToFront();