static INT_PTR CALLBACK DlgProcAutorunOpts(HWND hwndDlg, UINT msg, WPARAM, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: TranslateDialogDefault(hwndDlg); CheckDlgButton(hwndDlg, IDC_AUTORUN, CmpCurrentAndRegistry() ? BST_CHECKED : BST_UNCHECKED); // Check chekbox if Registry value exists return TRUE; case WM_COMMAND: SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button return TRUE; case WM_NOTIFY: switch(((LPNMHDR)lParam)->idFrom) { case 0: switch (((LPNMHDR)lParam)->code) { case PSN_APPLY: // if "Apply" pressed then... SetAutorun(IsDlgButtonChecked(hwndDlg,IDC_AUTORUN)); //Save changes to registry; return TRUE; } break; } break; } return FALSE; }
BOOL CCopyHandlerApp::InitInstance() { // ================================= Crash handling ======================================= SetUnhandledExceptionFilter(&MyUnhandledExceptionFilter); // ================================= Configuration ======================================== CString strPath; CString strCfgPath; CString strLogPath; // note that the GetProgramDataPath() below should create a directory; ExpandPath() could // depend on the directory to be created earlier if(!GetProgramDataPath(strPath)) { AfxMessageBox(_T("Cannot initialize Copy Handler (data path cannot be established)."), MB_ICONERROR | MB_OK); return FALSE; } strCfgPath = strPath + _T("\\ch.ini"); // initialize configuration file m_config.set_callback(ConfigPropertyChangedCallback, NULL); // read the configuration try { m_config.read(strCfgPath); } catch(...) { } // set working dir for the engine icpf::config& rConfig = GetConfig(); // rConfig.SetBasePath(strPath); // register all properties RegisterProperties(&rConfig); // ================================= Logging ======================================== // initialize the global log file if it is requested by configuration file strLogPath = strPath + + _T("\\ch.log"); chcore::TLogger& rLogger = chcore::TLogger::Acquire(); try { rLogger.init(strLogPath, (int_t)m_config.get_signed_num(PP_LOGMAXSIZE), (int_t)rConfig.get_unsigned_num(PP_LOGLEVEL), false, false); rLogger.Enable(m_config.get_bool(PP_LOGENABLELOGGING)); } catch(...) { BOOST_ASSERT(false); } LOG_INFO(_T("============================ Initializing Copy Handler ============================")); LOG_INFO(_T("")); // ================================= COM ======================================== LOG_INFO(_T("Initializing COM")); HRESULT hResult = CoInitializeEx(NULL, COINIT_MULTITHREADED); if(FAILED(hResult)) { CString strMsg; strMsg.Format(_T("Cannot initialize COM, the application will now exit (result = 0x%lx)"), hResult); LOG_ERROR(strMsg); AfxMessageBox(strMsg, MB_ICONERROR | MB_OK); return FALSE; } // ================================= Resource manager ======================================== LOG_INFO(_T("Initializing resource manager...")); ictranslate::CResourceManager& rResManager = ictranslate::CResourceManager::Acquire(); // set current language TCHAR szPath[_MAX_PATH]; rResManager.Init(AfxGetInstanceHandle()); rResManager.SetCallback(ResManCallback); rConfig.get_string(PP_PLANGUAGE, szPath, _MAX_PATH); TRACE(_T("Help path=%s\n"), szPath); if(!rResManager.SetLanguage(ExpandPath(szPath))) { TCHAR szData[2048]; _sntprintf(szData, 2048, _T("Couldn't find the language file specified in configuration file:\n%s\nPlease correct this path to point the language file to use.\nProgram will now exit."), szPath); LOG_ERROR(szData); AfxMessageBox(szData, MB_ICONSTOP | MB_OK); return FALSE; } UpdateHelpPaths(); // for dialogs ictranslate::CLanguageDialog::SetResManager(&rResManager); EnableHtmlHelp(); // ================================= Checking for running instances of CH ======================================== // check instance - return false if it's the second one LOG_INFO(_T("Checking for other running instances of Copy Handler")); if(!IsFirstInstance()) { LOG_WARNING(_T("Other instance of Copy Handler is already running. Exiting.")); MsgBox(IDS_ONECOPY_STRING); return FALSE; } // ================================= Common controls ======================================== LOG_INFO(_T("Initializing GUI common controls")); // InitCommonControlsEx() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // Set this to include all the common control classes you want to use // in your application. InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); // ================================= Shell extension ======================================== LOG_INFO(_T("Initializing shared memory for communication with shell extension")); m_hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(CSharedConfigStruct), _T("CHLMFile")); if (m_hMapObject == NULL) return FALSE; // Get a pointer to the file-mapped shared memory. g_pscsShared=(CSharedConfigStruct*)MapViewOfFile(m_hMapObject, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); if (g_pscsShared == NULL) return FALSE; LOG_INFO(_T("Checking shell extension compatibility")); // calculate ch version long lCHVersion = PRODUCT_VERSION1 << 24 | PRODUCT_VERSION2 << 16 | PRODUCT_VERSION3 << 8 | PRODUCT_VERSION4; // check the version of shell extension LONG lVersion = 0; BSTR bstrVersion = NULL; hResult = CoCreateInstance(CLSID_CShellExtControl, NULL, CLSCTX_ALL, IID_IShellExtControl, (void**)&m_piShellExtControl); if(SUCCEEDED(hResult) && !m_piShellExtControl) hResult = E_FAIL; if(SUCCEEDED(hResult)) hResult = m_piShellExtControl->GetVersion(&lVersion, &bstrVersion); if(SUCCEEDED(hResult) && lVersion == lCHVersion) hResult = m_piShellExtControl->SetFlags(eShellExt_Enabled, eShellExt_Enabled); if(FAILED(hResult) || lCHVersion != lVersion) { CString strMsg; strMsg.Format(_T("Shell extension has different version (0x%lx) than Copy Handler (0x%lx). Shell extension will be disabled."), lVersion, lCHVersion); LOG_WARNING(strMsg); MsgBox(IDS_SHELL_EXTENSION_MISMATCH_STRING); if(m_piShellExtControl) m_piShellExtControl->SetFlags(0, eShellExt_Enabled); } if(bstrVersion) ::SysFreeString(bstrVersion); // ================================= Initial settings ======================================== LOG_INFO(_T("Applying initial settings")); // set this process priority class HANDLE hProcess=GetCurrentProcess(); ::SetPriorityClass(hProcess, (DWORD)rConfig.get_signed_num(PP_PPROCESSPRIORITYCLASS)); #ifndef _DEBUG // for easier writing the program - doesn't collide with std CH // set "run with system" registry settings SetAutorun(rConfig.get_bool(PP_PRELOADAFTERRESTART)); #endif // ================================= Main window ======================================== LOG_INFO(_T("Creating main application window")); // create main window m_pMainWindow=new CMainWnd; if (!((CMainWnd*)m_pMainWindow)->Create()) return FALSE; // will be deleted at destructor m_pMainWnd = m_pMainWindow; CWinApp::InitInstance(); LOG_INFO(_T("Copy Handler initialized successfully")); return TRUE; }
LRESULT CALLBACK MainWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch(uMsg) { case WM_CREATE: ZeroMemory(&m_NotifyIcon, sizeof(m_NotifyIcon)); m_NotifyIcon.cbSize = sizeof(m_NotifyIcon); m_NotifyIcon.hIcon = g_hIcon; m_NotifyIcon.hWnd = hWnd; m_NotifyIcon.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP; m_NotifyIcon.uCallbackMessage = wm_ShellNotify; m_NotifyIcon.uID = 1; _tcscpy_s(m_NotifyIcon.szTip, sizeof(m_NotifyIcon.szTip), _T("TouchFreeze (Automatic mode)")); Shell_NotifyIcon(NIM_ADD, &m_NotifyIcon); return 0; case WM_DESTROY: Shell_NotifyIcon(NIM_DELETE, &m_NotifyIcon); return 0; case WM_TIMER: if (wParam == IDT_HIDE_BALLOON) { if (GetTickCount() > g_HideBalloonTime) { g_HideBalloonTime = 0; KillTimer(hWnd, IDT_HIDE_BALLOON); HideBalllon(hWnd, 1); } } return 0; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_ABOUT: ShowAboutDlg(g_hInst, hWnd); break; case ID_EXIT: PostQuitMessage(1); break; case ID_AUTOSTART_ON: SetAutorun(TRUE); break; case ID_AUTOSTART_OFF: SetAutorun(FALSE); break; case ID_DONATE: ContactOrDonate(hWnd, 1); break; } return 0; case WM_CLOSE: PostQuitMessage(1); return 0; } if (uMsg == wm_KBHookNotify) { ShowBallon(hWnd, 1); g_HideBalloonTime = GetTickCount() + g_BalloonTimeout; KillTimer(hWnd, IDT_HIDE_BALLOON); SetTimer(hWnd, IDT_HIDE_BALLOON, g_BalloonTimeout, NULL); return 0; } else if (uMsg == wm_ShellNotify) { switch(lParam) { case WM_LBUTTONUP: case WM_RBUTTONUP: ShowContextMenu(hWnd); break; } return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }