
帮忙解释一下这段vc++程序
BOOLCSnowWorldApp::InitInstance(){HANDLEhMutext=CreateMutex(NULL,TRUE,m_pszAppName);U...
BOOL CSnowWorldApp::InitInstance()
{
HANDLE hMutext = CreateMutex( NULL, TRUE, m_pszAppName );
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object.
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
// create and load the frame with its resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been initialized, so show and update it.
pFrame->CenterWindow();
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CSnowWorldApp message handlers
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CSnowWorldApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CSnowWorldApp message handlers 展开
{
HANDLE hMutext = CreateMutex( NULL, TRUE, m_pszAppName );
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object.
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
// create and load the frame with its resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been initialized, so show and update it.
pFrame->CenterWindow();
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CSnowWorldApp message handlers
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CSnowWorldApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CSnowWorldApp message handlers 展开
3个回答
展开全部
HANDLE hMutext = CreateMutex( NULL, TRUE, m_pszAppName );
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
关键代码在上面几行
让程序始终只有一个实例运行
通过创建命名Mutex来判断是否程序已经运行,是则退出。
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
关键代码在上面几行
让程序始终只有一个实例运行
通过创建命名Mutex来判断是否程序已经运行,是则退出。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同意楼上看法,关键在于下面的代码:
HANDLE hMutext = CreateMutex( NULL, TRUE, m_pszAppName );
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
其他的代码是MFC自己添加的。你如果想搞情况的话,可以看下孙鑫老师的《VC++深入详解》,挺好的。另外可以可以自己查看定义,设置断点。
上面的代码就是让程序只有一个实例。让程序创建一个互斥对象。这样让程序再有实力的时候就会自动退出。保持只有一个实例!
HANDLE hMutext = CreateMutex( NULL, TRUE, m_pszAppName );
UNREFERENCED_PARAMETER( hMutext );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
return FALSE;
其他的代码是MFC自己添加的。你如果想搞情况的话,可以看下孙鑫老师的《VC++深入详解》,挺好的。另外可以可以自己查看定义,设置断点。
上面的代码就是让程序只有一个实例。让程序创建一个互斥对象。这样让程序再有实力的时候就会自动退出。保持只有一个实例!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MFC自带的。无视掉吧·····或者看看孙新童鞋的VC++深入详解
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询