1.在对话框APP的InitInstanse()中添加HWND oldHWnd=NULL;
EnumWindows(EnumWndProc,(LPARAM)&oldHWnd);
if(oldHWnd!=NULL)
{
AfxMessageBox("本程序已经在运行了");
ShowWindow(oldHWnd,SW_SHOWNORMAL);
SetForegroundWindow(oldHWnd);
return FALSE;
}2.添加全局变量和全局回调函数CString g_szPropName="whu_woody";
HANDLE g_hValue=(HANDLE)8729;
BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
HANDLE h = GetProp(hwnd,g_szPropName);
if( h == g_hValue)
{
*(HWND*)lParam = hwnd;
return false;
}
return true;
}3.在对话框的OnInitDialog()中添加SetProp(m_hWnd,g_szPropName,g_hValue);为什么编译后提示EnumWndProc未定义?如果可以,请给予指导 [email protected]
EnumWindows(EnumWndProc,(LPARAM)&oldHWnd);
if(oldHWnd!=NULL)
{
AfxMessageBox("本程序已经在运行了");
ShowWindow(oldHWnd,SW_SHOWNORMAL);
SetForegroundWindow(oldHWnd);
return FALSE;
}2.添加全局变量和全局回调函数CString g_szPropName="whu_woody";
HANDLE g_hValue=(HANDLE)8729;
BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
HANDLE h = GetProp(hwnd,g_szPropName);
if( h == g_hValue)
{
*(HWND*)lParam = hwnd;
return false;
}
return true;
}3.在对话框的OnInitDialog()中添加SetProp(m_hWnd,g_szPropName,g_hValue);为什么编译后提示EnumWndProc未定义?如果可以,请给予指导 [email protected]
HANDLE hMutex = CreateMutex(NULL, FALSE, TEXT("Your name"));
// 检查错误代码
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
// 如果已有互斥量存在则释放句柄并复位互斥量
CloseHandle(hMutex);
hMutex = NULL;
// 程序退出
AfxMessageBox(TEXT("Error:\r\nAnother copy of \"Your Exe name\" is already running!"));
return FALSE;
}
虽然二楼提供的方法可行,但是我还是想把原来那方法弄懂,请执教。谢谢