www.pudn.com > PhoneReaderSrc.rar > MainWnd.cpp
// MainWnd.cpp: implementation of the CMainWnd class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MainWnd.h" #include "OptionDlg.h" #include "VersionDlg.h" #include#include #pragma comment(lib,"note_prj.lib") #define BOOKTYPE _T(".txt.prc.pdb.html") #define BOOKTYPENUM 2 #define LISTNUM 7 #define CASETOTAL 4 #define ROOTKEY HKEY_CURRENT_USER #define SUBKEY TEXT("Software\\Mitac\\PhoneReader") #define RECENTBOOKKEY TEXT("\\RecentBook") #define FAVORITEBOOKKEY TEXT("\\FavoriteBook") #define LASTPOSKEY TEXT("\\BookLastPos") #define SETTINGKEY TEXT("\\Settings") ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMainWnd::CMainWnd() { m_FirstFlag = TRUE; m_CloseFlag = TRUE; m_FocusFlag = FALSE; m_BookListNo = 0; m_BookPageNo = 0; for (int i = 0; i < 7 ; i++) { switch(i) { case 0: case 2: case 3: { m_Setting[i] = 0; break; } case 1: case 5: case 6: { m_Setting[i] = 1; break; } case 4: m_Setting[i] = 3; } } } CMainWnd::~CMainWnd() { } LRESULT CMainWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // Create the Menu Bar. LoadSHMenuBar(MAKEINTRESOURCE(IDR_MAINMENU)); SendMessage(m_hWndCB, SHCMBM_OVERRIDEKEY, VK_TBACK, MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY, SHMBOF_NODEFAULT | SHMBOF_NOTIFY)); if (m_BookListNo != 3) { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); } HICON hIcon = LoadIcon(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDI_PHONEREADER)); SetIcon(hIcon,TRUE); SetIcon(hIcon,FALSE); //get the width and height of BOOKCASE_TITLE CBitmap tempmap; tempmap.LoadBitmap(IDB_BKMAP); BITMAP bmpInfo; tempmap.GetBitmap(&bmpInfo); m_BookCaseWidth = bmpInfo.bmWidth; m_BookCaseHeight = bmpInfo.bmHeight; tempmap.DeleteObject(); //create the BookList RECT rc; GetClientRect(&rc); rc.top += m_BookCaseHeight; m_ListCtrl.Create(m_hWnd, rc); for (int i = 0; i < 4; i++) { if (m_BookList[i].GetCount() > 0) m_BookList[i].RemoveAll(); } m_ListCtrl.SetBkImage(IDB_LISTBKMAP); m_ListCtrl.SetFontColor(RGB(41, 44 ,198),RGB(0,0,0)); GetFromRegistry(0); ShowBookList(); GetFromRegistry(3); UINT CaseID = RECENT_CASE_TXT; for (int j = 0 ; j < CASETOTAL; j++) { m_CaseRect[j].SetRect(0,0,m_BookCaseWidth,m_BookCaseHeight); m_CaseName[j].Empty(); m_CaseName[j].LoadString(CaseID + j); m_CaseRect[j].left = m_BookCaseWidth * j / CASETOTAL+1; m_CaseRect[j].top = m_BookCaseHeight * 2 / 3; m_CaseRect[j].right = m_CaseRect[j].left + m_BookCaseWidth / CASETOTAL; } m_hMenu = (HMENU)SendMessage(m_hWndCB, SHCMBM_GETSUBMENU, 0, IDM_MENU); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); if (m_BookList[m_BookListNo].GetCount() == 0) { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } else { POSITION FavorPos; FavorPos = m_BookList[m_BookListNo].FindIndex(0); if (IfFavorites(m_BookList[m_BookListNo].GetAt(FavorPos).BookPath)) { EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); } } GetSettings(); return 0; } LRESULT CMainWnd::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CPaintDC dc(m_hWnd); //Set BackGround of BookCase if (m_FirstFlag) { //draw the transparent bitmap CBitmap Image, maskBitmap; Image.LoadBitmap(IDB_BKMAP); m_ImageDC.CreateCompatibleDC(dc); m_ImageDC.SelectBitmap(Image.m_hBitmap); m_MaskDC.CreateCompatibleDC(dc); maskBitmap.CreateBitmap( m_BookCaseWidth, m_BookCaseHeight, 1, 1, NULL ); m_MaskDC.SelectBitmap( maskBitmap.m_hBitmap ); m_ImageDC.SetBkColor(RGB(0,128,128)); m_MaskDC.BitBlt( 0, 0, m_BookCaseWidth, m_BookCaseHeight, m_ImageDC.m_hDC, 0, 0, SRCCOPY ); m_FirstFlag = FALSE; } CRect rect; GetClientRect(&rect); //set color of background HPEN hOldPen = dc.SelectStockPen(NULL_PEN); dc.FillSolidRect(&rect,RGB(188,214,252)); //set the color of selected item rect.left += m_BookCaseWidth * m_BookListNo / CASETOTAL; rect.right = rect.left + m_BookCaseWidth / CASETOTAL; rect.bottom = rect.top + m_BookCaseHeight; rect.DeflateRect(3,3,3,2); //draw selected item dc.FillSolidRect(&rect,RGB(41, 44,198)); //write text LOGFONT lf; ZeroMemory(&lf,sizeof(lf)); lstrcpy(lf.lfFaceName, _T("Arial")); lf.lfWeight = 500; lf.lfHeight = 12; CFont NewFont; NewFont.CreateFontIndirect(&lf); HFONT hOldFont = dc.SelectFont(NewFont.m_hFont); COLORREF OldBkColor = dc.SetBkColor(RGB(188,214,252)); COLORREF OldTextColor = dc.SetTextColor(RGB(0,0,0)); for (int j = 0 ; j < CASETOTAL; j++) { if (j == m_BookListNo) { dc.SetTextColor(RGB(255,255,255)); dc.SetBkColor(RGB(41, 44 ,198)); } dc.DrawText(m_CaseName[j],m_CaseName[j].GetLength(),m_CaseRect[j],DT_CENTER); if (j == m_BookListNo) { dc.SetTextColor(RGB(0,0,0)); dc.SetBkColor(RGB(188,214,252)); } } dc.BitBlt(0, 0 ,m_BookCaseWidth , m_BookCaseHeight, m_ImageDC.m_hDC, 0, 0,SRCINVERT); dc.BitBlt(0, 0, m_BookCaseWidth , m_BookCaseHeight, m_MaskDC.m_hDC, 0, 0, SRCAND); dc.BitBlt(0, 0, m_BookCaseWidth , m_BookCaseHeight, m_ImageDC.m_hDC, 0, 0,SRCINVERT); dc.SelectPen(hOldPen); dc.SetBkColor(OldBkColor); dc.SetTextColor(OldTextColor); dc.SelectFont(hOldFont); return 0; } LRESULT CMainWnd::OnAction(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { SendMessage(SPL_EXECUTE,0); return 0; } LRESULT CMainWnd::OnHotKey(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (m_CloseFlag) { if(HIWORD(lParam) == VK_TBACK) { if (m_BookPageNo == 0) { m_ImageDC.DeleteDC(); m_MaskDC.DeleteDC(); PostQuitMessage(0); } else { m_BookPageNo --; ShowBookList(); m_CloseFlag = FALSE; switch (m_BookListNo) { case 0: case 1: case 2: { EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); break; } case 3: { EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_ENABLED); break; } } } } } else m_CloseFlag = TRUE; return 0; } LRESULT CMainWnd::OnExecute(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (m_BookList[m_BookListNo].GetCount() == 0) return 0; RECT rc; CString Item,FilePath; ::SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0); int ItemNo = m_ListCtrl.GetSelectedIndex(); m_ListCtrl.GetItemText(ItemNo,2,Item); POSITION pos; if ((ItemNo == LISTNUM -1) && (m_BookList[m_BookListNo].GetCount() - 1 > (m_BookPageNo + 1) * (LISTNUM -1))) { CString MoreStr; MoreStr.LoadString(MORE_BOOK_TXT); if (MoreStr.Compare(Item) == 0) { m_BookPageNo ++; ShowBookList(); switch (m_BookListNo) { case 0: case 1: case 2: { EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); break; } case 3: { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); break; } } pos = m_BookList[m_BookListNo].FindIndex(m_BookPageNo * (LISTNUM - 1)); if (IfFavorites(m_BookList[m_BookListNo].GetAt(pos).BookPath)) EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); return 0; } } ItemNo = (LISTNUM - 1) * m_BookPageNo + ItemNo; pos = m_BookList[m_BookListNo].FindIndex(ItemNo); CFindFile FindTool; if (!FindTool.FindFile(m_BookList[m_BookListNo].GetAt(pos).BookPath)) { CString Msg,MsgTitle; MsgTitle.LoadString(TITLE_TXT); if (m_BookListNo == 3) { Msg.LoadString(MSG_NOLINK_WARNING); if (MessageBox(Msg,MsgTitle,MB_YESNO | MB_ICONQUESTION) == IDYES) { m_BookList[m_BookListNo].RemoveAt(pos); if ((m_BookListNo == 0) || (m_BookListNo == 3)) { SetRegistry(m_BookListNo); } ShowBookList(); } } else { Msg.LoadString(MSG_FINDERR); MessageBox(Msg,MsgTitle,MB_OK | MB_ICONINFORMATION); } return 0; } FindTool.Close(); if (!m_BookWnd.IsWindow()) { //create window m_BookWnd.Create(NULL,rc,L" ",WS_VISIBLE); } else { m_BookWnd.SetWindowText(L" "); m_BookWnd.ShowWindow(SW_SHOW); } if (m_BookListNo == 3) m_BookWnd.EnableDelFavorite(MF_GRAYED,MF_ENABLED); else { if (IfFavorites(m_BookList[m_BookListNo].GetAt(pos).BookPath)) m_BookWnd.EnableDelFavorite(MF_GRAYED,MF_GRAYED); else m_BookWnd.EnableDelFavorite(MF_ENABLED,MF_GRAYED); } //show book for (int i = 0; i < 7 ; i++) { m_BookWnd.m_Setting[i] = m_Setting[i]; } FilePath = m_BookList[m_BookListNo].GetAt(pos).BookPath; if(m_BookList[m_BookListNo].GetAt(pos).TypeNo == 0) m_BookWnd.m_bFormat = CExBuffer::FORMAT_TEXT; if(m_BookList[m_BookListNo].GetAt(pos).TypeNo == 1) m_BookWnd.m_bFormat = CExBuffer::FORMAT_PRC; if(m_BookList[m_BookListNo].GetAt(pos).TypeNo == 2) m_BookWnd.m_bFormat = CExBuffer::FORMAT_HTML; m_BookWnd.m_BookPath = FilePath; int KeyNo = 0; int Pos = GetLastPos(FilePath,KeyNo); if (Pos != -1) m_BookWnd.m_LastPagePos = Pos; else m_BookWnd.m_LastPagePos = 0; m_BookWnd.m_BookMark = m_BookList[m_BookListNo].GetAt(pos).BookMark; FilePath = GetSpecStr(FilePath,_T("\\")); m_BookWnd.m_BookName = GetSpecStr(FilePath,_T("."),FALSE); m_BookWnd.m_MainWnd = m_hWnd; m_BookWnd.m_RecentPage = 0; if (m_BookListNo != 3) m_BookWnd.m_FavorFlag = FALSE; else m_BookWnd.m_FavorFlag = TRUE; m_BookWnd.ShowBook(); return 0; } ///////////////////////////////////////////////////////////////////////////////// // FUNCTION NAME:FindBook // PARAMETER: // BookListDir ----- Directory of BookList // BookListNo ------ No of BookList(0 -- 4) // REMARK: Find Special Books with No // RETURN: // Find or Not // AUTHOR: // VERSION: 1.0 DATE: 26/09/2002 // MODIFY: DATE: REASON: ////////////////////////////////////////////////////////////////////////////////// BOOL CMainWnd::FindBook(CString BookListDir,int BookListNo) { //prepare for look for books if (BookListDir.Right(1).Compare(_T("\\")) != 0) BookListDir += _T("\\"); BookListDir += _T("*.*"); CFindFile FindTool; BOOL FindFlag = FindTool.FindFile(BookListDir); if (!FindFlag) return FALSE; //begin to look for books CString TypeStr; TypeStr = BOOKTYPE; CString FileName; CString TempStr1; int TypePos = -1; BookItem_Struct BookItem; BOOL TypeFindFlag = FALSE; do { if (FindTool.IsDirectory()) FindBook(FindTool.GetFilePath(),BookListNo); else { FileName = FindTool.GetFileName(); TempStr1 = GetSpecStr(FileName,_T(".")); if (TempStr1.IsEmpty()) continue; TempStr1.MakeLower(); // put file path into booklist by their type TypePos = TypeStr.Find(TempStr1); if (TypePos != -1) { ZeroMemory(&BookItem,sizeof(BookItem)); wcscpy(BookItem.BookPath,FindTool.GetFilePath()); BookItem.TypeNo = (TypePos -1) /4; if (BookItem.TypeNo > 1) BookItem.TypeNo -- ; m_BookList[BookListNo].AddTail(BookItem); } } } while (FindTool.FindNextFile()); return TRUE; } LRESULT CMainWnd::OnMenu(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { switch (wID) { case IDM_SETTING: { COptionDlg OptionDlg; for (int i = 0 ;i < 7 ; i++) OptionDlg.m_Setting[i] = m_Setting[i]; if (OptionDlg.DoModal() == IDOK) { for (i = 0 ; i < 7 ; i++) m_Setting[i] = OptionDlg.m_Setting[i]; } InputSettings(); break; } case IDM_ADDTO: { if (m_BookList[m_BookListNo].GetCount()) { int ItemNo = m_ListCtrl.GetSelectedIndex(); // check if this item is "More" CString Item; m_ListCtrl.GetItemText(ItemNo,2,Item); CString MoreStr; MoreStr.LoadString(MORE_BOOK_TXT); if ((ItemNo == LISTNUM - 1) && (MoreStr.Compare(Item) == 0)) { if (m_BookList[m_BookListNo].GetCount() - 1 > (m_BookPageNo + 1) * (LISTNUM -1)) break; } POSITION pos; pos = m_BookList[m_BookListNo].FindIndex(m_BookPageNo * (LISTNUM -1) + ItemNo); CString Msg,MsgTitle,BookName; m_ListCtrl.GetItemText(ItemNo,2,BookName); Msg.Format(MSG_ADDWARNING,BookName); MsgTitle.LoadString(TITLE_TXT); if (MessageBox(Msg,MsgTitle, MB_YESNO | MB_ICONQUESTION) == IDYES) { if (!IfFavorites(m_BookList[m_BookListNo].GetAt(pos).BookPath)) { m_BookList[3].AddHead(m_BookList[m_BookListNo].GetAt(pos)); SetRegistry(3); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } } } break; } case IDM_DELFROM: { if ((m_BookListNo == 3) && (m_BookList[m_BookListNo].GetCount())) { int ItemNo = m_ListCtrl.GetSelectedIndex(); POSITION pos = m_BookList[3].FindIndex(m_BookPageNo * (LISTNUM -1) + ItemNo); CString Msg,MsgTitle,BookName; m_ListCtrl.GetItemText(ItemNo,2,BookName); Msg.Format(MSG_DELWARNING,BookName); MsgTitle.LoadString(TITLE_TXT); if (MessageBox(Msg,MsgTitle, MB_YESNO | MB_ICONQUESTION) == IDYES) { m_BookList[3].RemoveAt(pos); SetRegistry(3); if (m_BookList[m_BookListNo].GetCount() == 0) EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); ShowBookList(); } } break; } case IDM_VERSION: { CVersionDlg dlg; dlg.DoModal(); break; } case IDM_EXIT: { m_ImageDC.DeleteDC(); m_MaskDC.DeleteDC(); PostQuitMessage(0); } } return 0; } LRESULT CMainWnd::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // TODO : Add Code for message handler. Call DefWindowProc if necessary. CString BookDirStr; if (wParam == VK_TLEFT) { if (m_BookListNo == 0) m_BookListNo = CASETOTAL - 1 ; else m_BookListNo --; m_BookPageNo = 0; } if (wParam == VK_TRIGHT) { if (m_BookListNo == CASETOTAL - 1) m_BookListNo = 0; else m_BookListNo ++; m_BookPageNo = 0; } switch (m_BookListNo) { case 0: { EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); if (wParam == VK_TRIGHT) { m_ListCtrl.m_ImageTypeFlag = TRUE; } GetFromRegistry(0); break; } case 1: { BookDirStr.LoadString(DOCUMENT_DIR_TXT); m_BookList[m_BookListNo].RemoveAll(); FindBook(BookDirStr,m_BookListNo); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); break; } case 2: { m_BookList[m_BookListNo].RemoveAll(); if (FindStorage()) { for (int t = 0 ; t < 2 ; t++) { if (!m_Storage[t].IsEmpty()) FindBook(m_Storage[t],m_BookListNo); } } EnableMenuItem(m_hMenu,IDM_ADDTO,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); if (wParam == VK_LEFT) { m_ListCtrl.m_ImageTypeFlag = TRUE; } break; } case 3: { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_ENABLED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); m_ListCtrl.m_ImageTypeFlag = FALSE; GetFromRegistry(3); break; } } int ItemNo = m_ListCtrl.GetSelectedIndex(); if (wParam == VK_TDOWN) ItemNo ++; if (wParam == VK_TUP) ItemNo --; if ((wParam == VK_TLEFT) || (wParam == VK_TRIGHT)) { if (m_BookList[m_BookListNo].GetCount() == 0) { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } ShowBookList(); } else { // check if this item is "More" if (ItemNo == LISTNUM - 1) { CString Item; m_ListCtrl.GetItemText(ItemNo,2,Item); CString MoreStr; MoreStr.LoadString(MORE_BOOK_TXT); if ( MoreStr.Compare(Item) == 0) { if (m_BookList[m_BookListNo].GetCount() - 1 > (m_BookPageNo + 1) * (LISTNUM -1)) { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } } } } POSITION FavorPos; if ((wParam == VK_TLEFT) || (wParam == VK_TRIGHT)) FavorPos = m_BookList[m_BookListNo].FindIndex(0); else FavorPos = m_BookList[m_BookListNo].FindIndex(ItemNo + m_BookPageNo * (LISTNUM - 1)); if (IfFavorites(m_BookList[m_BookListNo].GetAt(FavorPos).BookPath)) EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); return 0; } void CMainWnd::ShowBookList(BOOL RedrawFlag) { m_ListCtrl.DeleteAllItems(); int BookNum = m_BookList[m_BookListNo].GetCount(); if (BookNum == 0 ) { RedrawWindow(); return; } BOOL PageFlag = FALSE; if (BookNum > LISTNUM * (m_BookPageNo + 1) - m_BookPageNo) { BookNum = LISTNUM * (m_BookPageNo + 1) - m_BookPageNo - 1; PageFlag = TRUE; } POSITION pos; for (int i = LISTNUM * m_BookPageNo - m_BookPageNo ; i < BookNum ; i++) { pos = m_BookList[m_BookListNo].FindIndex(i); CString ItemStr; ItemStr = GetSpecStr(m_BookList[m_BookListNo].GetAt(pos).BookPath,_T("\\")); ItemStr = GetSpecStr(ItemStr,_T("."),FALSE); m_ListCtrl.AddItem(ItemStr,m_BookList[m_BookListNo].GetAt(pos).TypeNo); } if (PageFlag) { CString MoreBook; MoreBook.LoadString(MORE_BOOK_TXT); CString NoStr; NoStr.Format(_T("%d"), LISTNUM); m_ListCtrl.InsertItem(LISTNUM,MoreBook); m_ListCtrl.m_MoreFlag = TRUE; m_ListCtrl.SetItemText(LISTNUM - 1, 1, NoStr); m_ListCtrl.SetItemText(LISTNUM - 1, 2, MoreBook); } else m_ListCtrl.m_MoreFlag = FALSE; m_ListCtrl.SelectItem(0); if (RedrawFlag) RedrawWindow(); } BOOL CMainWnd::GetFromRegistry(int ListNo) { HKEY hKey = NULL; DWORD cbData = 0; CString SubKeys,BookKeys; SubKeys = SUBKEY; if (ListNo == 0) SubKeys += RECENTBOOKKEY; else SubKeys += FAVORITEBOOKKEY; CFindFile FindTool; // get length of list int ListLength = 0; if(ERROR_SUCCESS == RegOpenKeyEx(ROOTKEY, SUBKEY, 0, 0, &hKey)) { cbData = sizeof(int); if (ListNo == 0) { if(ERROR_SUCCESS != RegQueryValueEx(hKey,RECENTBOOKKEY, NULL, NULL, (LPBYTE)&ListLength, &cbData)) { RegCloseKey(hKey); return FALSE; } } else { if(ERROR_SUCCESS != RegQueryValueEx(hKey,FAVORITEBOOKKEY, NULL, NULL, (LPBYTE)&ListLength, &cbData)) { RegCloseKey(hKey); return FALSE; } } } else { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); if ((ListLength > 0) && (m_BookList[ListNo].GetCount() > 0)) m_BookList[ListNo].RemoveAll(); BookItem_Struct item; TCHAR BookPath[255]; for (int i = 0 ;i < ListLength ; i++) { BookKeys.Format(_T("%s%d"),SubKeys,i); if(ERROR_SUCCESS == RegOpenKeyEx(ROOTKEY, BookKeys, 0, 0, &hKey)) { ZeroMemory(&item,sizeof(item)); memset(BookPath, 0 ,sizeof(BookPath)); cbData = 255 * sizeof(TCHAR); if(ERROR_SUCCESS != RegQueryValueEx(hKey, _T("BookPath"), NULL, NULL, (LPBYTE)BookPath, &cbData)) { RegCloseKey(hKey); return FALSE; } wcscpy(item.BookPath,BookPath); cbData = sizeof(int); if(ERROR_SUCCESS != RegQueryValueEx(hKey, _T("TypeNo"), NULL, NULL, (LPBYTE)&item.TypeNo, &cbData)) { RegCloseKey(hKey); return FALSE; } cbData = sizeof(int); if(ERROR_SUCCESS != RegQueryValueEx(hKey, _T("BookMark"), NULL, NULL, (LPBYTE)&item.BookMark, &cbData)) { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); if (ListNo == 0) { if (FindTool.FindFile(item.BookPath)) m_BookList[ListNo].AddTail(item); } else m_BookList[ListNo].AddTail(item); } } return TRUE; } BOOL CMainWnd::SetRegistry(int ListNo) { HKEY hKey ; DWORD dwDisposition ; CString SubKeys,BookKeys; SubKeys = SUBKEY; if (ListNo == 0) SubKeys += RECENTBOOKKEY; else SubKeys += FAVORITEBOOKKEY; POSITION pos; DelFromRegistry(ListNo); if(RegCreateKeyEx(ROOTKEY,SUBKEY,0,NULL,0, 0, NULL,&hKey, &dwDisposition) == ERROR_SUCCESS) { int ListLength = m_BookList[ListNo].GetCount(); if (ListNo == 0) { if(RegSetValueEx(hKey, RECENTBOOKKEY, NULL, REG_DWORD, (LPBYTE)&ListLength, sizeof(int)) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } } else { if(RegSetValueEx(hKey, FAVORITEBOOKKEY, NULL, REG_DWORD, (LPBYTE)&ListLength, sizeof(int)) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } } } else return FALSE; RegCloseKey(hKey); TCHAR BookName[255]; for (int i = 0; i < m_BookList[ListNo].GetCount(); i++) { BookKeys.Format(_T("%s%d"),SubKeys,i); pos = m_BookList[ListNo].FindIndex(i); memset(BookName , 0 ,sizeof(BookName)); wcscpy(BookName , m_BookList[ListNo].GetAt(pos).BookPath); if(RegCreateKeyEx(ROOTKEY,BookKeys,0,NULL,0, 0, NULL,&hKey, &dwDisposition) == ERROR_SUCCESS) { if(RegSetValueEx(hKey, _T("BookPath"), NULL, REG_SZ, (LPBYTE)BookName, wcslen(BookName) * sizeof(TCHAR)) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } if(RegSetValueEx(hKey, _T("TypeNo"), NULL, REG_DWORD, (LPBYTE)&m_BookList[ListNo].GetAt(pos).TypeNo, sizeof(int)) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } if(RegSetValueEx(hKey, _T("BookMark"), NULL, REG_DWORD, (LPBYTE)&m_BookList[ListNo].GetAt(pos).BookMark, sizeof(int)) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); } } return TRUE; } LRESULT CMainWnd::OnCloseBook(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CReadBookWnd *CloseWnd = (CReadBookWnd*)wParam; if (lParam) { //close normal window of readbook m_CloseFlag = FALSE; //get settings for (int i = 0; i < 7 ; i++) { m_Setting[i] = CloseWnd->m_Setting[i]; } InputSettings(); int ItemNo = m_ListCtrl.GetSelectedIndex(); POSITION pos; pos = m_BookList[m_BookListNo].FindIndex(m_BookPageNo * (LISTNUM - 1) + ItemNo); CString BookPath = m_BookList[m_BookListNo].GetAt(pos).BookPath; //set bookmark m_BookList[m_BookListNo].GetAt(pos).BookMark = CloseWnd->m_BookMark; // set Recent Book List BookItem_Struct item; ZeroMemory(&item,sizeof(item)); item = m_BookList[m_BookListNo].GetAt(pos); POSITION FirstPos; for (int t = 0 ; t < m_BookList[0].GetCount(); t ++) { FirstPos = m_BookList[0].FindIndex(t); CString BookInfo = m_BookList[0].GetAt(FirstPos).BookPath; if (BookInfo == item.BookPath) m_BookList[0].RemoveAt(FirstPos); } m_BookList[0].AddHead(item); if (m_BookList[0].GetCount() > 7) m_BookList[0].RemoveTail(); SetRegistry(0); GetFromRegistry(0); //set to favorite if ((m_BookListNo != 3) && (CloseWnd->m_FavorFlag == TRUE)) { if (!IfFavorites(m_BookList[m_BookListNo].GetAt(pos).BookPath)) { m_BookList[3].AddHead(item); SetRegistry(3); EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } } //del from favorite if ((m_BookListNo == 3) && (CloseWnd->m_FavorFlag == FALSE)) { m_BookList[3].RemoveAt(pos); SetRegistry(3); ShowBookList(); } if (CloseWnd->m_hWnd == m_FullBookWnd.m_hWnd) m_BookWnd.DestroyWindow(); CString BookDirStr; if (m_BookListNo == 1) { BookDirStr.LoadString(DOCUMENT_DIR_TXT); m_BookList[m_BookListNo].RemoveAll(); FindBook(BookDirStr,m_BookListNo); ShowBookList(); } if (m_BookListNo == 2) { m_BookList[m_BookListNo].RemoveAll(); if (FindStorage()) { for (int t = 0 ; t < 2 ; t++) { if (!m_Storage[t].IsEmpty()) FindBook(m_Storage[t],m_BookListNo); } } ShowBookList(); } if (m_BookListNo == 0) ShowBookList(); if ((m_BookListNo != 0) && (m_ListCtrl.GetItemCount() > ItemNo)) m_ListCtrl.SelectItem(ItemNo); //set last pos int Last = CloseWnd->m_LastPagePos; if ( Last != -1) SetLastPos(BookPath, Last, TRUE); else SetLastPos(BookPath, 0, TRUE); } else { m_BookWnd.m_FullScreenFlag = FALSE; m_BookWnd.m_bFormat = m_FullBookWnd.m_bFormat; m_BookWnd.m_BookMark = m_FullBookWnd.m_BookMark; m_BookWnd.m_BookPath = m_FullBookWnd.m_BookPath; m_BookWnd.m_FavorFlag = m_FullBookWnd.m_FavorFlag; m_BookWnd.m_LastPagePos = m_FullBookWnd.m_ShowBuf.m_nStartLine; m_BookWnd.m_MainWnd = m_hWnd; for (int i = 0 ; i < 7 ; i ++) m_BookWnd.m_Setting[i] = m_FullBookWnd.m_Setting[i]; if (m_BookListNo == 3) { if (m_BookWnd.m_FavorFlag) m_BookWnd.EnableDelFavorite(MF_GRAYED,MF_ENABLED); else m_BookWnd.EnableDelFavorite(MF_GRAYED,MF_GRAYED); } else { if (m_BookWnd.m_FavorFlag) { m_BookWnd.EnableDelFavorite(MF_GRAYED,MF_GRAYED); } else { m_BookWnd.EnableDelFavorite(MF_ENABLED,MF_GRAYED); } } m_BookWnd.ShowBook(); } if (m_FocusFlag) { ::SetFocus(m_ListCtrl); m_FocusFlag = FALSE; } if (m_BookList[m_BookListNo].GetCount() == 0) { EnableMenuItem(m_hMenu,IDM_DELFROM,MF_GRAYED); EnableMenuItem(m_hMenu,IDM_ADDTO,MF_GRAYED); } return 0; } CString CMainWnd::GetSpecStr(CString TotalStr,LPCTSTR FindStr,BOOL RightFlag) { int Pos = 0, LastPos = 0; CString LastStr; LastStr.Empty(); while (1) { Pos = TotalStr.Find(FindStr,LastPos + 1); if (Pos == -1) break; LastPos = Pos; } if (RightFlag) LastStr = TotalStr.Mid(LastPos + 1); else LastStr = TotalStr.Left(LastPos); return LastStr; } BOOL CMainWnd::DelFromRegistry(int ListNo) { HKEY hKey; CString SubKeys,BookKeys; SubKeys = SUBKEY; if (ListNo == 0) SubKeys += RECENTBOOKKEY; else SubKeys += FAVORITEBOOKKEY; int ListLength = 0; DWORD cbData; if(ERROR_SUCCESS == RegOpenKeyEx(ROOTKEY, SUBKEY, 0, 0, &hKey)) { if (ListNo == 0) { if(ERROR_SUCCESS != RegQueryValueEx(hKey,RECENTBOOKKEY, NULL, NULL, (LPBYTE)&ListLength, &cbData)) { RegCloseKey(hKey); return FALSE; } else RegDeleteValue(hKey, RECENTBOOKKEY); } else { if(ERROR_SUCCESS != RegQueryValueEx(hKey,FAVORITEBOOKKEY, NULL, NULL, (LPBYTE)&ListLength, &cbData)) { RegCloseKey(hKey); return FALSE; } else RegDeleteValue(hKey, FAVORITEBOOKKEY); } } else return FALSE; RegCloseKey(hKey); CString GetPath; GetPath = L""; for (int i = 0; i < ListLength ; i++) { BookKeys.Format(_T("%s%d"),SubKeys,i); if(ERROR_SUCCESS == RegOpenKeyEx(ROOTKEY, BookKeys, 0, 0, &hKey)) { RegCloseKey(hKey); RegDeleteKey(ROOTKEY,BookKeys); } } return TRUE; } int CMainWnd::GetLastPos(LPCTSTR BookPath, int& KeyNo) { HKEY hKey; CString SubKey , BookKey; SubKey = SUBKEY; SubKey += LASTPOSKEY; int i = 0; DWORD cbData = 0; int LastPos = 0; CString GetPath; TCHAR Buf[255]; BOOL FindFlag = FALSE; while (1) { GetPath = L""; BookKey.Format(_T("%s%d"),SubKey,i); if (RegOpenKeyEx(ROOTKEY,BookKey,0,0,&hKey) == ERROR_SUCCESS) { cbData = 255 * sizeof(TCHAR); memset(Buf, 0 , sizeof(Buf)); if (RegQueryValueEx(hKey,_T("BookPath"),0,NULL,(LPBYTE)Buf,&cbData) == ERROR_SUCCESS) { GetPath = Buf; if (GetPath.Find(BookPath) != -1) { cbData = sizeof(int); if (RegQueryValueEx(hKey,_T("LastPos"),0,NULL,(LPBYTE)&LastPos,&cbData) == ERROR_SUCCESS) { FindFlag = TRUE; KeyNo = i; RegCloseKey(hKey); break; } else { RegCloseKey(hKey); break; } } } RegCloseKey(hKey); } else { break; } i++; } if (!FindFlag) return -1; else return LastPos; } BOOL CMainWnd::SetLastPos(LPCTSTR BookPath, int Pos, BOOL SetFlag) { HKEY hKey; int i = 0; DWORD cbData = 0; DWORD dwDisposition; CString SubKey,BookKey; SubKey = SUBKEY; SubKey += LASTPOSKEY; int KeyNo = 0; int GetPos = GetLastPos(BookPath, KeyNo); if (( GetPos == -1) && (SetFlag)) { while (1) { BookKey.Format(_T("%s%d"),SubKey,i); if (RegOpenKeyEx(ROOTKEY, BookKey, 0, 0 ,&hKey) == ERROR_SUCCESS) RegCloseKey(hKey); else { if (RegCreateKeyEx(ROOTKEY,BookKey,0,NULL,0,0,NULL,&hKey,&dwDisposition) == ERROR_SUCCESS) { cbData = wcslen(BookPath) * sizeof(WCHAR) + 1; if (RegSetValueEx(hKey, _T("BookPath"), 0 ,REG_SZ,(const BYTE *)BookPath,cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } cbData = sizeof(int); if (RegSetValueEx(hKey, _T("LastPos"), 0 ,REG_DWORD,(const BYTE *)&Pos,cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); return TRUE; } else return FALSE; } i++; } } else { BookKey.Format(_T("%s%d"),SubKey,KeyNo); if (RegOpenKeyEx(ROOTKEY, BookKey, 0, 0 ,&hKey) == ERROR_SUCCESS) { if (!SetFlag) { RegCloseKey(hKey); if (RegDeleteKey(ROOTKEY,BookKey) != ERROR_SUCCESS) { cbData = sizeof(int); if (RegSetValueEx(hKey, _T("LastPos"), 0 ,REG_DWORD,(LPBYTE)&Pos,cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } } return TRUE; } cbData = sizeof(int); if (RegSetValueEx(hKey, _T("LastPos"), 0 ,REG_DWORD,(LPBYTE)&Pos,cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); return TRUE; } } return TRUE; } LRESULT CMainWnd::OnFullScreen(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CRect rc(0,0,176,220); m_FullBookWnd.m_FullScreenFlag = TRUE; if (!m_FullBookWnd.IsWindow()) { //create window m_FullBookWnd.Create(NULL,rc,L" ",WS_VISIBLE); } else { m_FullBookWnd.SetWindowText(L" "); m_FullBookWnd.ShowWindow(SW_SHOW); } m_FullBookWnd.MoveMenuBar(FALSE); m_FullBookWnd.m_bFormat = m_BookWnd.m_bFormat; m_FullBookWnd.m_BookMark = m_BookWnd.m_BookMark; m_FullBookWnd.m_BookPath = m_BookWnd.m_BookPath; m_FullBookWnd.m_FavorFlag = m_BookWnd.m_FavorFlag; m_FullBookWnd.m_LastPagePos = m_BookWnd.m_ShowBuf.m_nStartLine; m_FullBookWnd.m_MainWnd = m_hWnd; for (int i = 0 ; i < 7 ; i ++) m_FullBookWnd.m_Setting[i] = m_BookWnd.m_Setting[i]; if (m_BookListNo == 3) { if (m_FullBookWnd.m_FavorFlag) m_FullBookWnd.EnableDelFavorite(MF_GRAYED,MF_ENABLED); else m_FullBookWnd.EnableDelFavorite(MF_GRAYED,MF_GRAYED); } else { if (m_FullBookWnd.m_FavorFlag) { m_FullBookWnd.EnableDelFavorite(MF_GRAYED,MF_GRAYED); } else { m_FullBookWnd.EnableDelFavorite(MF_ENABLED,MF_GRAYED); } } m_FullBookWnd.ShowBook(); return 0; } void CMainWnd::GetSettings() { HKEY hKey; DWORD cbData = 0; CString SubKey,BookKey; SubKey = SUBKEY; SubKey += SETTINGKEY; if (RegOpenKeyEx(ROOTKEY, SubKey, 0, 0 ,&hKey) != ERROR_SUCCESS) return; int Setting = 0; int SettingNum[7]; SettingNum[0] = 1; SettingNum[1] = 3; SettingNum[2] = 4; SettingNum[3] = 16; SettingNum[4] = 16; SettingNum[5] = 3; SettingNum[6] = 2; for (int i = 0; i < 7 ; i++) { BookKey.Format(_T("%s%d"),SETTINGKEY,i); cbData = sizeof(int); if (RegQueryValueEx(hKey, BookKey , NULL ,NULL,(LPBYTE)&Setting,&cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return ; } if ((Setting >= 0 ) && (Setting < SettingNum[i])) m_Setting[i] = Setting; } RegCloseKey(hKey); } void CMainWnd::InputSettings() { HKEY hKey; int cbData = 0; DWORD dwDisposition; CString SubKey,BookKey; SubKey = SUBKEY; SubKey += SETTINGKEY; if (RegOpenKeyEx(ROOTKEY, SubKey, 0, 0 ,&hKey) != ERROR_SUCCESS) { if (RegCreateKeyEx(ROOTKEY,SubKey,0,NULL,0,0,NULL,&hKey,&dwDisposition) != ERROR_SUCCESS) return; } for (int i = 0; i < 7 ; i++) { BookKey.Format(_T("%s%d"),SETTINGKEY,i); cbData = sizeof(int); int Setting = m_Setting[i]; if (RegSetValueEx(hKey, BookKey , 0 ,REG_DWORD,(LPBYTE)&Setting,cbData) != ERROR_SUCCESS) { RegCloseKey(hKey); return ; } } RegCloseKey(hKey); } LRESULT CMainWnd::OnActivatePreInstance(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { m_FocusFlag = TRUE; const DWORD dwThreadid = ::GetWindowThreadProcessId( m_hWnd , NULL ) ; HWND hWndFind = GetDesktopWindow(); TCHAR szClsName[256],szWinTitle[256]; CString sAppName; sAppName.LoadString(IDS_APPNAME); BOOL MsgFlag = FALSE; //先关闭所有的MessageBox while((hWndFind = ::GetWindow(hWndFind, GW_HWNDNEXT)) != NULL) { if(::GetWindowThreadProcessId(hWndFind,NULL) == dwThreadid && hWndFind != g_hWndLast && hWndFind != m_hWnd) { ::GetClassName(hWndFind,szClsName,256); ::GetWindowText(hWndFind,szWinTitle,256); if(_tcscmp(szClsName,_T("Dialog")) == 0 && _tcscmp(szWinTitle,sAppName) == 0) { //::PostMessage(hWndFind,WM_QUIT,IDCANCEL,0); ::SetForegroundWindow(hWndFind); ::UpdateWindow(hWndFind); MsgFlag = TRUE; break; } } } if (!MsgFlag) { //如果程序有其他对话框,激活 if(g_hWndLast) { ::SetForegroundWindow(g_hWndLast); } else//否则激活程序的主窗体 { ::SetForegroundWindow(m_hWnd); } } return 0; } BOOL CMainWnd::FindStorage() { // Search handle for storage cards. HANDLE hFlashCard; // Structure for storing storage WIN32_FIND_DATA *lpwfdFlashCardTmp; // card information temporarily. lpwfdFlashCardTmp = (WIN32_FIND_DATA *) LocalAlloc (LPTR, 10 * sizeof (WIN32_FIND_DATA)); // Failed allocate memory return; if (lpwfdFlashCardTmp == NULL) return FALSE; //init the string array for(int j = 0; j < 2 ;j++) m_Storage[j].Empty(); //Find all flash card and add their name to string array hFlashCard = FindFirstFlashCard(&lpwfdFlashCardTmp [0]); if(hFlashCard == INVALID_HANDLE_VALUE) return FALSE; int i = 0; CString CheckStr(lpwfdFlashCardTmp->cFileName); if ((CheckStr.Find(L"SD") != -1) || (CheckStr.Find(L"MMC") != -1)) m_Storage[i++] = CString(L"\\") + lpwfdFlashCardTmp->cFileName; while(FindNextFlashCard(hFlashCard,&lpwfdFlashCardTmp[0])) { CheckStr.Format(lpwfdFlashCardTmp->cFileName); if ((CheckStr.Find(L"SD") != -1) || (CheckStr.Find(L"MMC") != -1)) m_Storage[i++] = CString(L"\\") +lpwfdFlashCardTmp->cFileName; } return TRUE; } BOOL CMainWnd::IfFavorites(LPCTSTR BookPath) { CString FavorStr; FavorStr = L""; POSITION FavorPos; for (int t = 0 ; t < m_BookList[3].GetCount() ; t++) { FavorPos = m_BookList[3].FindIndex(t); FavorStr = m_BookList[3].GetAt(FavorPos).BookPath; if (FavorStr.Find(BookPath) != -1) return TRUE; } return FALSE; }