Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I "Steal" Focus???

Status
Not open for further replies.

ColdPhreze

Programmer
Apr 15, 2002
93
I am working on an application that must be StayOnTop, which is no problem, and needs to:

Have focus at all times (until closed)
OR
Be able to "Steal" focus back from some other application (or Windows).

Any help will be greatly appreciated.

KyferEz
 
This line should help you. You will need a pointer to the window you want to do this to.

pWnd->SetWindowPos(&CWnd::wndTopMost ,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW);

I am not sure about all the SWP constants. Also, you may want to look at

pWnd->ModifyStyleEx(WS_EX_TOPMOST,0,0);

Matt
 
SOrry... forgot what forum I was in... i dont know how to do it in Borland...

Off to get more coffee... |-I

Matt

 
i'm going to name your main window Form1.

Form1->ShowModal();


then i think it's FormStyle that you can set to AlwaysOnTop Cyprus
[noevil]
 
Thanks all... but still no answer.
Zyrenthian, maybe I can find equilivent for Borland from that.

Cyprus106, ummm, I think you need some coffee too. I said I know how to make it stayontop, but need to also STEAL FOCUS from another application.
 
Actually Zyrenthian's answer should also work for Borland, too. I was wondering if you could put something in the apps's OnDeactivate event that would set the focus back to the app. The OnDeactivate event is supposed to be called when the app loses focus.
Code:
void __fastcall TForm1::AppDeactivate(TObject *Sender)
{
  // Reset focus here
}
I'm just guessing at this so take it for what it is worth. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
wow... sorry. I need something alot stronger than coffee.

"when you have insomnia, your never asleep and never really awake"
-Fight Club

So you don't want it always focused, or do you? Do you want to steal the focus after an event or something?
Form1->SetFocus(); could work.

Or maybe that's not what your looking for...

Sorry. Maybe I'll take you up on that coffee. Cyprus
[noevil]
 
Ok Cyprus, maybe the way I made my initial post is confusing. This is what I need to know how to do:

1) Steal Focus from another running application
OR
2) Keep focus at all times until my program is closed

Knowing how to do both would be nice too...
Thanx all
KyferEz
 
Just curious...

what is the difference from always having focus and "or stealing" it from application?

My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Always having focus means the user cannot go to another application to do something w/o shutting down my program first.

Stealing focus means that if another program has focus, and my program decides it needs to interrupt, it can give itself focus, like taking over.

For example, any antivirus program, when it sees a virus, it steals focus from whatever you are doing, and then it retains focus and will not let you do anything else until you take care of it's virus warning.
 
HWND GetFocus(void);
this is one function
it gives the window with the current focus so you can restore the focus later

use this to get the focus
SendMessage(hWnd, WM_SETFOCUS, 0, 0);
or
PostMessage(hWnd, WM_SETFOCUS, 0, 0);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top