home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / dlgcbr32 / dlgcbar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  3.4 KB  |  124 lines

  1. // Dlgcbar.cpp : Defines the class behaviors for the application.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14.  
  15. #include "resource.h"
  16. #include "dlgcbar.h"
  17. #include "aboutdlg.h"
  18. #include "wndlist.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CTheApp
  28.  
  29. CTheApp theApp;
  30.  
  31. BEGIN_MESSAGE_MAP(CTheApp, CWinApp)
  32.     //{{AFX_MSG_MAP(CTheApp)
  33.     ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
  34.         // NOTE - the ClassWizard will add and remove mapping macros here.
  35.         //    DO NOT EDIT what you see in these blocks of generated code!
  36.     //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CTheApp Constructors/Destructors
  41.  
  42. CTheApp::CTheApp()
  43. {
  44. }
  45.  
  46. CTheApp::~CTheApp()
  47. {
  48. }
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CTheApp::FirstInstance
  52. //      FirstInstance checks for an existing instance of the application.
  53. //      If one is found, it is activated.
  54. //
  55. //      This function uses a technique similar to that described in KB
  56. //      article Q109175 to locate the previous instance of the application.
  57. //      However, instead of searching for a matching class name, it searches
  58. //      for a matching caption.  This allows us to use the normal dialog
  59. //      class for our main window.  It assumes that the AFX_IDS_APP_TITLE
  60. //      string resource matches the caption specified in the dialog template.
  61.  
  62. BOOL CTheApp::FirstInstance()
  63. {
  64.     CString strCaption;
  65.     strCaption.LoadString(AFX_IDS_APP_TITLE);
  66.  
  67.     CWnd* pwndFirst = CWnd::FindWindow((LPCSTR)(DWORD)WC_DIALOG,
  68.                                        strCaption);
  69.     if (pwndFirst)
  70.     {
  71.         // another instance is already running - activate it
  72.         CWnd* pwndPopup = pwndFirst->GetLastActivePopup();
  73.         pwndFirst->SetForegroundWindow();
  74.         if (pwndFirst->IsIconic())
  75.             pwndFirst->ShowWindow(SW_SHOWNORMAL);
  76.         if (pwndFirst != pwndPopup)
  77.             pwndPopup->SetForegroundWindow();
  78.         return FALSE;
  79.     }
  80.     else
  81.     {
  82.         // this is the first instance
  83.         return TRUE;
  84.     }
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CTheApp::InitInstance
  89. //      InitInstance performs per-instance initialization of the DLGCBAR
  90. //      application.  If an instance of the application is already running,
  91. //      it activates that instance.  Otherwise, it creates the modeless
  92. //      dialog which serves as the application's interface.
  93.  
  94. BOOL CTheApp::InitInstance()
  95. {
  96.     if (!FirstInstance())
  97.         return FALSE;
  98.  
  99.     // Create main window
  100.     TRY
  101.     {
  102.         CWndListDlg* pMainWnd = new CWndListDlg;
  103.         m_pMainWnd = pMainWnd;
  104.         return pMainWnd->Create();
  105.     }
  106.     CATCH_ALL(e)
  107.     {
  108.         TRACE0("Failed to create main dialog\n");
  109.         return FALSE;
  110.     }
  111.     END_CATCH_ALL
  112.  
  113. }
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CTheApp::OnHelpAbout
  117. //      OnHelpAbout displays the application's about box.
  118.  
  119. void CTheApp::OnHelpAbout()
  120. {
  121.     CAboutDlg dlg(m_pMainWnd);
  122.     dlg.DoModal();
  123. }
  124.