0

I am trying to bring my running application on click of windows rightclick. Please note I dont want a new instance of the same application but bringing the same application to the front by using SetForegroundWindow

I have tried using AfxRegisterClass and Createwindow (Previos post here) but this creates a new window and onclik bring the new window instead of my current application. Is there a way I can bring up my app instead of newly created window.

Community
  • 1
  • 1
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • 1
    You can always use `FindWindow()` without calling `CreateWindow()`. I don't understand your question - do you want to make sure that only one instance of your application is running? – In silico Nov 17 '10 at 09:55
  • @ In silico, If I am trying to use FindWindow without using Create Window , the handle is getting NULL. If you would like to have a look at [source code](http://stackoverflow.com/questions/4201728/bringwindowtotop-is-not-working-even-if-i-get-the-handle-to-class-window "FindWindow") – Simsons Nov 17 '10 at 10:22

1 Answers1

0

Probably better to use mutexes, but yes, you can use FindWindow for that. Something like this:

HWND hwnd = FindWindow(NULL, "My App's Hopefully Unique Title");
if (hwnd)
{
    SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
    SetFocus(hwnd);
}
Mud
  • 28,277
  • 11
  • 59
  • 92
  • @MSalters, I dont understand the reason , But when I RegisterClass and then try to Findwindow I get NULL but when I use FindWindow (After CreateWindow) I obtain the handle to it. I have all the code in previous [post](http://stackoverflow.com/questions/4201728/bringwindowtotop-is-not-working-even-if-i-get-the-handle-to-class-window "BringWindowToTop is Not working even if I get the handle to Class Window") – Simsons Nov 17 '10 at 10:51
  • That's because there **is no** window to find if you haven't created it. – wj32 Nov 17 '10 at 10:52
  • @wj32, So does that mean if I need to Find without creating the Window then I have to Find by Title instead of ClassName – Simsons Nov 17 '10 at 10:55
  • No, it means that your question doesn't make sense. If you're trying to bring an existing window to the top, then use the code in the answer. Why would you be asking about creating a window? – wj32 Nov 17 '10 at 10:59