PSS ID Number: 141752
Article Last Modified on 6/15/2004
119591 How to Obtain Microsoft Support Files from Online Services
ONET32 -d
// Add a static BOOL that indicates whether the class was
// registered so that you can unregister it in ExitInstance
static BOOL bClassRegistered = FALSE;
BOOL COneT32App::InitInstance()
{
// If a previous instance of the application is already running,
// then activate it and return FALSE from InitInstance to
// end the execution of this instance.
if(!FirstInstance())
return FALSE;
// Register your unique class name that you wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS)); // start with NULL
// defaults
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = LoadIcon(IDR_MAINFRAME); // or load a different
// icon
wndcls.hCursor = LoadCursor( IDC_ARROW );
wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wndcls.lpszMenuName = NULL;
// Specify your own class name for using FindWindow later
wndcls.lpszClassName = _T("MyNewClass");
// Register the new class and exit if it fails
if(!AfxRegisterClass(&wndcls))
{
TRACE("Class Registration Failed\n");
return FALSE;
}
bClassRegistered = TRUE;
// Rest of InitInstance goes here
...
...
...
}
BOOL COneT32App::FirstInstance()
{
CWnd *pWndPrev, *pWndChild;
// Determine if another window with your class name exists...
if (pWndPrev = CWnd::FindWindow(_T("MyNewClass"),NULL))
{
// If so, does it have any popups?
pWndChild = pWndPrev->GetLastActivePopup();
// If iconic, restore the main window
if (pWndPrev->IsIconic())
pWndPrev->ShowWindow(SW_RESTORE);
// Bring the main window or its popup to
// the foreground
pWndChild->SetForegroundWindow();
// and you are done activating the previous one.
return FALSE;
}
// First instance. Proceed as normal.
else
return TRUE;
}
int COneT32App::ExitInstance()
{
if(bClassRegistered)
::UnregisterClass(_T("MyNewClass"),AfxGetInstanceHandle());
return CWinApp::ExitInstance();
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// Use the specific class name you established earlier.
cs.lpszClass = _T("MyNewClass");
// Change the following line to call.
// CFrameWnd::PreCreateWindow(cs) if this is an SDI application.
return CMDIFrameWnd::PreCreateWindow(cs);
}
238100 HOWTO: Limiting 32-bit Applications to a Single Instance in WinCE
140596 INFO: MFC 4.0 No Longer Pre-Registers Window Classes
%3 %4
Additional query words: ONETIME one time Chapter 12 Advanced Windows HINSTANCE hPrevInstance single instance
Keywords: kbdownload kbArchitecture kbcode kbFAQ kbfile kbSample KbUIDesign KB141752
Technology: _IKkbbogus kbAudDeveloper kbMFC kbVCNET2002 kbVCNETSearch kbVCsearch