如何让我的程序只运行一次

alionx 2003-03-25 03:26:08
如果用户再次运行程序,则让已运行的程序获得焦点并闪烁.请问该如何写.
...全文
20 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
alionx 2003-03-25
  • 打赏
  • 举报
回复
谢谢各位的热心帮助分不够还请见谅!
zhuwenzheng 2003-03-25
  • 打赏
  • 举报
回复
BOOL CYourApp::InitInstance()
{
if(FirstInstance())
{
return FALSE;
}
..............
}

BOOL CYourApp::FirstInstance()
{
BOOL bFound = FALSE;

// Try to create a mutex with the app's name
HANDLE hMutexOneInstance = ::CreateMutex(NULL,TRUE,_T(AfxGetAppName()));

// Already there...means that we are already running an instance
if(::GetLastError() == ERROR_ALREADY_EXISTS)
bFound = TRUE;

// Release the mutex
if(hMutexOneInstance)
::ReleaseMutex(hMutexOneInstance);

return(bFound);

}
updownok 2003-03-25
  • 打赏
  • 举报
回复
Win32Process m_win32proc;
m_win32proc.Init();
m_win32proc.EnumAllProcesses();
int count(0);
CString m_strProcessToCheck;
m_strProcessToCheck="server";
int size=m_win32proc.GetAllProcessesNames()->GetSize();
int i;
CString yyy;
for(i=0;i<size;i++)
{

yyy=m_win32proc.GetAllProcessesNames()->GetAt(i);
if(yyy==m_strProcessToCheck)
count++;
else
continue;
}

/////////////////该断代码,放在程序初始化函数中
每次执行新程序,检测进程中有没有已经执行的程序,有则退出
alionx 2003-03-25
  • 打赏
  • 举报
回复
HWND hWnd = FindWindowEx( NULL, NULL, m_strClassName, NULL );
if ( hWnd != NULL )
{
ShowWindow( hWnd, SW_RESTORE );
BringWindowToTop( hWnd );
SetForegroundWindow( hWnd );
}
return FALSE;
执行后没有使已经运行的程序获得焦点啊请指教
dark 2003-03-25
  • 打赏
  • 举报
回复
给你一段代码,返回FALSE则证明已经存在

BOOL CSingleInstance::Create( UINT nID )
{
CString strFullString;

// Create our class name string
if ( strFullString.LoadString( nID ) ) {
// Extract the first sub-string
AfxExtractSubString( m_strClassName, strFullString, 0 );
}

// Add the word 'Class' to the end
m_strClassName += _T(" Class");

// Create the mutex
m_hMutex = CreateMutex( NULL, TRUE, m_strClassName );
// Check for errors
if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
// Reset our mutext handle (just in case)
m_hMutex = NULL;
// The mutex already exists, which means an instance is already
// running. Find the app and pop it up
HWND hWnd = FindWindowEx( NULL, NULL, m_strClassName, NULL );
if ( hWnd != NULL ) {
ShowWindow( hWnd, SW_RESTORE );
BringWindowToTop( hWnd );
SetForegroundWindow( hWnd );
}
// Return failure
return FALSE;
}

// Register the unique window class name so others can find it.
WNDCLASS wndcls; memset(&wndcls, 0, sizeof(WNDCLASS));
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = AfxWndProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = LoadIcon( wndcls.hInstance, MAKEINTRESOURCE( nID ) );//or AFX_IDI_STD_FRAME;
wndcls.hCursor = LoadCursor( wndcls.hInstance, IDC_ARROW );
wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wndcls.lpszMenuName = NULL;//You may need to fix this
wndcls.lpszClassName = m_strClassName; // my class name
// Register name, exit if it fails
if(!AfxRegisterClass(&wndcls)) {
AfxMessageBox( _T("Failed to register window class!"), MB_ICONSTOP | MB_OK );
return FALSE;
}

// Return success
return TRUE;
}
alionx 2003-03-25
  • 打赏
  • 举报
回复
但是如何让已经运行的同一个程序获得焦点并闪烁呢??
mo01 2003-03-25
  • 打赏
  • 举报
回复
在InitInstance()函数中加入:
CreateMutex(NULL,true,AfxGetAppName());
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
AfxMessageBox("程序已经运行");
return false;
}
kite_zeng 2003-03-25
  • 打赏
  • 举报
回复
class CMutex OnlyOne(FALSE,"LOCALICQ",NULL); //确保只有这个一例程在运行
if (!OnlyOne.Lock(1))
{
PostMessage(自定义消息);
return FALSE; //退出
}
//已运行的程序响应消息,SetFocus()

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧