 |
NEWBIE
TIP
#1
Avoiding a second instance by activating the current instance
As is the case with so many elements of Windows and MFC programming, once you know
the trick, it's easy.
And it's interesting to note that this, our very first Tip,
has nothing to do with MFC!
Call FindWindow to get a handle to a window with the same title as your application.
If it comes back NULL, there are no windows with the same title, so you're okay.
But if it returns a valid handle, show the window and bring it to the top.
See KB article Q141752 for a different method.
HWND hwnd = ::FindWindow(NULL, "title of your app");
if (hwnd)
{
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::BringWindowToTop(hwnd);
}
return FALSE;
|
Source code
is provided for instructional purposes,
and is released to the public domain.
No claim of ownership is made.
Use it at your own risk.
No warranties are expressed or implied.
The resulting program may not be fully functional.
No animals were used to test this code.
|
Past Tips |
Future Tips |
Home |
Comments on this Tip?
Send us mail.
(Be sure to put the Tip # in the subject.)
Got an idea for a Tip you'd like us to write?
Let us know.
Solved a problem lately that would make a good Tip?
PLEASE tell us about it!!!
We'll give you prominent credit, and we'll even buy you a beer next time you're in town.
|