Running only one instance of an SDI/MDI application is easy. You only
have to follow the examples, but doing this in a dialog-based
application is something else.
WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
For some reason, you can't use the PreCreateWindow of a dialog box, so
you can't change the class name to your desired class name, in order to
use it with conjunction with the AfxRegisterClass function in the
InitInstance of your derived CWinApp class.
What you have to do is a little trickey:
After checking with SPY++, I found out that the class name of the dialog
box application is "#32770". Now, in order to try to find previous
instances, you use FindWindow( LPCTSTR lpszClassName, LPCTSTR
lpszWindowName ). In the MDI/SDI application example, the lpszWindowName
is set to NULL, because you have the class name and that is enough.
However, in a dialog-based application, you must also use the Window
title. The title is set up in the InitDialog function with the command
SetWindowText. When you write your FirstInstance function in the CWinApp
class (like in the known examples), you write something like this:
FindWindow ("#32770", "My application "); Yes, I wrote "My application "
with a space in the end on purpose, since this is the name that you
write in the SetWindowText in your InitDialog function.
In order to undrstand this better, just download the example. It was
tested successfully with MSVC 6.0, Win 98.
If it does to you any problems with other MSVC versions/operating
systems, please let me know.
Download demo project - 29 KB
Date Last Updated: February 3, 1999