我刚抄了一个让应用程序只运行一个实例的代码,不知能不能帮上忙BOOL CDlgApp::InitInstance() { ///////////////////////////////////这一段是要加的 HANDLE hSem = CreateSemaphore( NULL, 1, 1, m_pszAppName ); if ( GetLastError() == ERROR_ALREADY_EXISTS ) { CloseHandle(hSem); HWND hWndPrevious = ::GetWindow( ::GetDesktopWindow(), GW_CHILD ); while ( ::IsWindow(hWndPrevious) ) { if ( ::GetProp( hWndPrevious, m_pszAppName ) ) { if ( ::IsIconic(hWndPrevious) ) ::ShowWindow( hWndPrevious, SW_RESTORE ); ::SetForegroundWindow(hWndPrevious); ::SetForegroundWindow(::GetLastActivePopup(hWndPrevious)); return FALSE; } hWndPrevious = ::GetWindow( hWndPrevious, GW_HWNDNEXT ); } 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 #endifSetRegistryKey(_T("abc"));CDlgDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel }// Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; } int CDlgDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1;// TODO: Add your specialized creation code here::SetProp( m_hWnd, ::AfxGetApp()->m_pszAppName, (HANDLE)1 );//加这句return 0; } void CDlgDlg::OnDestroy() { CDialog::OnDestroy();// TODO: Add your message handler code here::RemoveProp( m_hWnd, ::AfxGetApp()->m_pszAppName );//加这句}就可以实现了
窗口的Class Name 在创建时就应该被指定;如果你用CButton等的成员函数直接创建时,Class Name 时可以不用指定.你的设置是什么意思呢?你的目的是什么呢?
FindWindow函数检测窗口是否存在,如果已运行就退出.现在问题是无法更改ClassName的值.不知还有什么办法可让程序只能运行一次
你的方法不行.改变不了.
{
///////////////////////////////////这一段是要加的
HANDLE hSem = CreateSemaphore( NULL, 1, 1, m_pszAppName );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle(hSem);
HWND hWndPrevious = ::GetWindow( ::GetDesktopWindow(), GW_CHILD );
while ( ::IsWindow(hWndPrevious) )
{
if ( ::GetProp( hWndPrevious, m_pszAppName ) )
{
if ( ::IsIconic(hWndPrevious) )
::ShowWindow( hWndPrevious, SW_RESTORE );
::SetForegroundWindow(hWndPrevious);
::SetForegroundWindow(::GetLastActivePopup(hWndPrevious));
return FALSE;
}
hWndPrevious = ::GetWindow( hWndPrevious, GW_HWNDNEXT );
}
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
#endifSetRegistryKey(_T("abc"));CDlgDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
int CDlgDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;// TODO: Add your specialized creation code here::SetProp( m_hWnd, ::AfxGetApp()->m_pszAppName, (HANDLE)1 );//加这句return 0;
}
void CDlgDlg::OnDestroy()
{
CDialog::OnDestroy();// TODO: Add your message handler code here::RemoveProp( m_hWnd, ::AfxGetApp()->m_pszAppName );//加这句}就可以实现了
你提供的程序解决了只运行一个实例的问题