www.pudn.com > SimplePlayer.rar > SimplyPlayer.cpp
// SimplyPlayer.cpp : 定义应用程序的类行为。 // #include "stdafx.h" #include "SimplyPlayer.h" #include "SimplyPlayerDlg.h" #include ".\simplyplayer.h" #include#ifdef _DEBUG #define new DEBUG_NEW #endif // CSimplyPlayerApp BEGIN_MESSAGE_MAP(CSimplyPlayerApp, CWinApp) ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() // CSimplyPlayerApp 构造 CSimplyPlayerApp::CSimplyPlayerApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } // 唯一的一个 CSimplyPlayerApp 对象 CSimplyPlayerApp theApp; // CSimplyPlayerApp 初始化 HANDLE hExeCheckMutex; BOOL CSimplyPlayerApp::InitInstance() { // 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControls()。否则,将无法创建窗口。 if( !FirstInstance() ) { AfxMessageBox(_T("程序已经在运行了,请检查是否已被最小化到托盘!")); return FALSE; } //第二种方法实现防止程序多次运行 /*HWND hWndCap,hWndPopup; CString strCaption; HANDLE hPrevMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,_T("h_XXXX_PrevInstance")); if(hPrevMutex) { CloseHandle(hPrevMutex); strCaption.LoadString(AFX_IDS_APP_TITLE); hWndCap = FindWindow(NULL,strCaption); if( hWndCap != NULL ) { AfxMessageBox(_T("程序已经在运行了,请检查是否已被最小化到托盘!")); return FALSE; } } //这里须重载APP的ExitInstance去释放hExeCheckMutex hExeCheckMutex = CreateMutex(FALSE,0,"h_XXXX_PrevInstance");*/ InitCommonControls(); CWinApp::InitInstance(); AfxEnableControlContainer(); // 标准初始化 // 如果未使用这些功能并希望减小 // 最终可执行文件的大小,则应移除下列 // 不需要的特定初始化例程 // 更改用于存储设置的注册表项 // TODO: 应适当修改该字符串, // 例如修改为公司或组织名 SetRegistryKey(_T("应用程序向导生成的本地应用程序")); CSimplyPlayerDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: 在此放置处理何时用“确定”来关闭 //对话框的代码 } else if (nResponse == IDCANCEL) { // TODO: 在此放置处理何时用“取消”来关闭 //对话框的代码 } //判断程序是否在运行 return FALSE; } /*BOOL CSimplyPlayerApp::FindProcess(LPCTSTR pszProcessName) { PDH_STATUS pdhStatus = ERROR_SUCCESS; LPTSTR pszCounterListBuffer = NULL; DWORD dwCounterListSize = 0; LPTSTR pszInstanceListBuffer = NULL; DWORD dwInstanceListSize = 0; LPTSTR pszThisInstance = NULL; pdhStatus = PdhEnumObjectItems( NULL, NULL, TEXT("Process"), pszCounterListBuffer, &dwCounterListSize, pszInstanceListBuffer, &dwInstanceListSize, PERF_DETAIL_WIZARD, 0); if (pdhStatus == ERROR_SUCCESS || pdhStatus == PDH_MORE_DATA ) { pszCounterListBuffer = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,dwCounterListSize * sizeof (TCHAR)); pszInstanceListBuffer =(LPTSTR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,dwInstanceListSize * sizeof (TCHAR)); if((pszCounterListBuffer != NULL) &&(pszInstanceListBuffer != NULL)) { pdhStatus = PdhEnumObjectItems ( NULL, NULL, TEXT("Process"), pszCounterListBuffer, &dwCounterListSize, pszInstanceListBuffer, &dwInstanceListSize, PERF_DETAIL_WIZARD, 0); if(pdhStatus == ERROR_SUCCESS) { for (pszThisInstance = pszInstanceListBuffer; *pszThisInstance != 0; pszThisInstance += lstrlen(pszThisInstance) + 1) { if(lstrcmpi(pszThisInstance,pszProcessName)==0) return TRUE; } } } if(pszCounterListBuffer) HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,pszCounterListBuffer); if(pszInstanceListBuffer) HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,pszInstanceListBuffer); } return FALSE; }*/ BOOL CSimplyPlayerApp::FirstInstance() { CWnd *pWndPrev, *pWndChild; pWndPrev = CWnd::FindWindow(NULL, _T("MyPlayer → By Stefanie924")); if(pWndPrev != NULL) { //pWndChild = pWndPrev->GetLastActivePopup(); /*if(pWndPrev->IsIconic()) { pWndPrev->ShowWindow(TRUE); pWndPrev->SetForegroundWindow(); //CSimplyPlayerDlg dlg; //Shell_NotifyIcon(NIM_DELETE,&dlg.nid);//从托盘中删除 }*/ return FALSE; } else { return TRUE; } }