Ste*_*ven 0 .net c# window winforms form-control
我知道有一些线索已经存在.我需要的是一个窗体窗口总是聚焦意味着如果我点击记事本或任何程序它不会输入任何数据,只在我的窗体文本框中输入数据.
我发现这个代码在某种程度上可以解释更多
//Delegates for safe multi-threading.
delegate void DelegateGetFocus();
private DelegateGetFocus m_getFocus;
Thread newThread;
public MemberLogin()
{
m_getFocus = new DelegateGetFocus(this.getFocus);
InitializeComponent();
spawnThread(keepFocus);
toggleFocusButton.Text = "OFF";
timer1.Interval = 2000;
textBox1.Select();
}
//test focus stuff
//Spawns a new Thread.
private void spawnThread(ThreadStart ts)
{
try
{
newThread = new Thread(ts);
newThread.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
//Continuously call getFocus.
private void keepFocus()
{
while (true)
{
getFocus();
}
}
//Keeps Form on top and gives focus.
private void getFocus()
{
//If we need to invoke this call from another thread.
if (this.InvokeRequired)
{
this.Invoke(m_getFocus, new object[] { });
}
//Otherwise, we're safe.
else
{
//having this seemed to have kept my windows onTop at all times even when off
// this.TopMost = true;
this.TopMost = true;
this.Activate();
this.textBox1.Select();
this.textBox1.Focus();
}
}
Run Code Online (Sandbox Code Playgroud)
这个代码似乎只有在我的项目打开时才有效,这意味着当我的Visual Studio项目关闭时,窗口是最顶层但没有焦点意味着我可以在其他程序中输入.我发现奇怪的是,记事本和我的文本框都有闪烁的线条,告诉你写文本的位置.如果我从Visual Studio项目运行我的应用程序一切正常,当我尝试点击其他窗口时,它不会让我访问这是我想要的.
所以我对为什么它只能在打开的项目中正常工作有点困惑
还要注意,只要项目打开,然后甚至.exe和其他copys我正常工作我关闭项目解决方案,程序完成我上面解释的.
刚做了一些测试,它似乎只在这个进程运行vhost.exe时才能正常工作,这是Visual Studio托管过程.我在设置中禁用了它,当我从VS启动它工作正常但是当我在bin文件夹中运行exe时我仍然得到奇怪的结果
编辑
这是我用结果http://www.youtube.com/watch?v=1ozpHSRGnMo制作的快速视频
新编辑
我做了解决这个问题的方法是将我的应用程序设置为全屏模式,这样用户可以点击其他窗口而不先关闭这个窗口
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
Run Code Online (Sandbox Code Playgroud)
小智 5
这是真的,你不能阻止其他窗口获得焦点,但你可以从他们获得它的第二个;)
private void timer1_Tick(object sender, EventArgs e)
{
if (!this.Focused)
{
this.Activate();
}
}
Run Code Online (Sandbox Code Playgroud)
只需创建一个计时器,从一开始就启用它,使得勾选间隔尽可能小,并将上面的代码作为tick-event.
希望这可以帮助
为坏英语而烦恼
| 归档时间: |
|
| 查看次数: |
9414 次 |
| 最近记录: |