Click here to Skip to main content
15,887,333 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionSetWindowForeground Pin
sashoalm6-Jun-09 3:13
sashoalm6-Jun-09 3:13 
Hello,

I've been trying to solve that problem for some time now and I'll be very grateful to anyone who can suggest a solution. The problem is a little tricky but I hope I'll be able to explain it clearly.

I have an application which might need to notify the user of an event even if it's not focused at the moment or is minimized. Think of something along the lines of an app that copies files for 5-6 minutes and needs to say 'Your files were copied' at the end, but the user has switched to writing emails in the meantime. I don't want to grab the keyboard focus because he might be doing something like writing in notepad in which case he'll be randomly pressing hotkeys on my dialog instead of writing to notepad. But I can't figure out how to do this properly.

Right now I use this code (from my main dialog class):
//	if (IsIconic())
//		ShowWindow(SW_RESTORE);
	SetForegroundWindow();


By calling SetForegroundWindow() my dialog comes to the foreground (or flashes - but I don't know how Windows decides which of the two) and sometimes will steal focus which I don't want, but there are many problems if my dialog is also minimized. In this case the taskbar icon of my dialog will seem to become active on the taskbar, but my dialog won't show itself (it'll remain minimized but receive keyboard focus it seems). If I uncomment the other two lines, then it solves all problems with minimizing as it first unminimizes the dialog, but it _always_ steals focus which I don't want.

My second problem with the main dialog being minimized is I also create a dialog containing some information about the event and I display it with DoModal, and if I call DoModal while the main window was minimized, when the user switches to my program the new dialog created with DoModal is shown but the main dialog is not. Any way to fix that?

So it's all very complicated and I'm not sure how it's supposed to be implemented. Again, all I want is to notify the user without stealing keyboard focus, even if my dialog is minimized at the time. Do I unminimize my dialog? How do I prevent it from stealing focus then?

Any help will be greatly appreciated!

There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal

AnswerRe: SetWindowForeground Pin
Stuart Dootson6-Jun-09 3:46
professionalStuart Dootson6-Jun-09 3:46 
GeneralRe: SetWindowForeground Pin
sashoalm6-Jun-09 4:02
sashoalm6-Jun-09 4:02 
GeneralRe: SetWindowForeground Pin
sashoalm6-Jun-09 4:30
sashoalm6-Jun-09 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.