问题
I have a production application that I always want to force focus on. Sometimes the users will accidentally click off the form and the blue focused window will turn gray. I have a timer that fires every 30 seconds and I want to programatically give focus back to the form so that it goes from gray back to blue with focus. I've tried using the .focus event and the .activecontrol property but none of those seem to work and I'm not seeing any other viable options. Surely there is a way to do this, though right? I'm using Visual Studio 2008 in VB.Net.
Please advise.
回答1:
Nope, there isn't a way [anymore.] Even if you give focus to an application the best you can hope for is that its icon in the taskbar will flash three times. Thank christ they fixed this (since XP I think?), as while I see your point of view, for users this kind of behaviour is hellish. They should control their operating system and applications, not you. Sorry!
回答2:
I have had good luck using the property, this.TopMost = true;
private void frmMain_Shown(object sender, EventArgs e)
{
// Make this form the active form and make it TopMost
this.ShowInTaskbar = false;
this.TopMost = true;
this.Focus();
this.BringToFront();
this.TopMost = false;
}
You can try making a function and calling it from your timer code. (Sorry it is in C#).
回答3:
This is what I used to set the focus back to my application when the user clicks the notification balloon:
'My app is maximized always, so change this as you need. WindowState focuses your application
Me.WindowState = FormWindowState.Maximized
'Bring the desired from to fucos
MyForm.Activate()
来源:https://stackoverflow.com/questions/3374312/forcing-focus-for-desktop-applications-built-in-net