防止程序多次运行问题 本帖最后由 yangke1994 于 2009-07-28 22:45:32 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将工程的启动改为:Sub Main过程,在里面添加:运行时先判断是否已经运行该文件,如果没有运行,那么运行。如果运行,那么置顶。 按照你程序的标题+窗口类名,使用 FindWindow 函数来查找你的窗口,再用 ShowWindow 函数显示你的窗口! FindWindow 的方法不可靠,不推荐。专业的做法是使用 RegisterWindowMessage 和 CreateMutex。VC示例代码如下:启动时: //////////////// CHAR szMutexName[] = "aabbcc~~tt"; g_GlobeRestoreMsg = RegisterWindowMessage(szMutexName); // //Register ATOM // HANDLE hMutex = CreateMutex(NULL,FALSE,szMutexName); if (hMutex == NULL) { return FALSE; } else if(ERROR_ALREADY_EXISTS == ::GetLastError()) { // // Restore the exist instance... // ::SendMessage(HWND_BROADCAST, g_GlobeRestoreMsg, -1, //Restore 0); CloseHandle(hMutex); return FALSE; }然后在主窗口的窗口过程中,处理自定义消息:LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { if (message == g_GlobeRestoreMsg) { CString strDebug; strDebug.Format("[MMXShell] HWND_BROADCAST, wParam = %d\n", wParam); OutputDebugString(strDebug); if (wParam == -1) // 是本进程发送的广播消息 { ::SetForegroundWindow(this->m_hWnd); } else // 由MMXDetect发送的 { } return 1; } http://topic.csdn.net/t/20030311/16/1517558.html 启动窗口中加Private Sub Form_Load() If App.PrevInstance Then MsgBox "你已经运行这个应用程序了" End ' 退出新运行的程序 End If End Sub 听说VB区有个很猥琐的版主 VB6 自定义浏览器 Webbrowser 键盘失效问题. ActiveX的DLL无法在ASP环境中使用API函数 用字符串变量名表示文件名 如何将一VB单机版软件转换成网络版 字段部分匹配问题请教各位. 怎样激活指定窗体。 CSTR函数和STR()函数的区别? 请问如何用代码来创控件 探讨一下, VB6最佳的开发平台和软件配置. 关于注册表 有人会调用wmp11的wmp.dll库吗?
启动时:
////////////////
CHAR szMutexName[] = "aabbcc~~tt";
g_GlobeRestoreMsg = RegisterWindowMessage(szMutexName);
//
//Register ATOM
//
HANDLE hMutex = CreateMutex(NULL,FALSE,szMutexName);
if (hMutex == NULL)
{
return FALSE;
}
else if(ERROR_ALREADY_EXISTS == ::GetLastError())
{
//
// Restore the exist instance...
//
::SendMessage(HWND_BROADCAST,
g_GlobeRestoreMsg,
-1, //Restore
0);
CloseHandle(hMutex);
return FALSE;
}然后在主窗口的窗口过程中,处理自定义消息:
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == g_GlobeRestoreMsg)
{
CString strDebug;
strDebug.Format("[MMXShell] HWND_BROADCAST, wParam = %d\n", wParam);
OutputDebugString(strDebug);
if (wParam == -1) // 是本进程发送的广播消息
{
::SetForegroundWindow(this->m_hWnd);
}
else // 由MMXDetect发送的
{
}
return 1;
}
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "你已经运行这个应用程序了"
End ' 退出新运行的程序
End If
End Sub