📄 wordpad.cpp
字号:
for (int i=0;i<=nDec;i++)
{
pVal[i] = lval/div; //integer number
lval -= pVal[i]*div;
lval *= 10;
}
i--;
if (lval >= div/2)
pVal[i]++;
while ((pVal[i] == 10) && (i != 0))
{
pVal[i] = 0;
pVal[--i]++;
}
while (nDec && pVal[nDec] == 0)
nDec--;
_stprintf(buf, _T("%.*f"), nDec, (float)nValue/(float)div);
if (m_units[m_nUnits].m_bSpaceAbbrev)
lstrcat(buf, _T(" "));
lstrcat(buf, GetAbbrev());
delete []pVal;
}
/////////////////////////////////////////////////////////////////////////////
// CWordPadApp commands
void CWordPadApp::OnAppAbout()
{
CString strTitle;
VERIFY(strTitle.LoadString(AFX_IDS_APP_TITLE));
ShellAbout(m_pMainWnd->GetSafeHwnd(), strTitle, _T(""), LoadIcon(IDR_MAINFRAME));
}
int CWordPadApp::ExitInstance()
{
m_pszHelpFilePath = NULL;
FreeLibrary(GetModuleHandle(_T("RICHED32.DLL")));
SaveOptions();
return CWinApp::ExitInstance();
}
void CWordPadApp::OnFileNew()
{
int nDocType = -1;
if (!m_bPromptForType)
{
if (cmdInfo.m_bForceTextMode)
nDocType = RD_TEXT;
else if (!cmdInfo.m_strFileName.IsEmpty())
{
CFileException fe;
nDocType = GetDocTypeFromName(cmdInfo.m_strFileName, fe);
}
if (nDocType == -1)
nDocType = RD_DEFAULT;
}
else
{
CFileNewDialog dlg;
if (dlg.DoModal() == IDCANCEL)
return;
nDocType = (dlg.m_nSel == 0) ? RD_DEFAULT: //Word 6
(dlg.m_nSel == 1) ? RD_RICHTEXT : //RTF
RD_TEXT ; //text
if (nDocType != RD_TEXT)
cmdInfo.m_bForceTextMode = FALSE;
}
m_nNewDocType = nDocType;
DocTemplate.OpenDocumentFile(NULL);
// if returns NULL, the user has already been alerted
}
// prompt for file name - used for open and save as
// static function called from app
BOOL CWordPadApp::PromptForFileName(CString& fileName, UINT nIDSTitle,
DWORD dwFlags, BOOL bOpenFileDialog, int* pType)
{
ScanForConverters();
CFileDialog dlgFile(bOpenFileDialog);
CString title;
VERIFY(title.LoadString(nIDSTitle));
dlgFile.m_ofn.Flags |= dwFlags;
// dlgFile.m_ofn.Flags &= ~OFN_SHOWHELP;
int nIndex = m_nFilterIndex;
if (!bOpenFileDialog)
{
int nDocType = (pType != NULL) ? *pType : RD_DEFAULT;
nIndex = GetIndexFromType(nDocType, bOpenFileDialog);
if (nIndex == -1)
nIndex = GetIndexFromType(RD_DEFAULT, bOpenFileDialog);
if (nIndex == -1)
nIndex = GetIndexFromType(RD_NATIVE, bOpenFileDialog);
ASSERT(nIndex != -1);
nIndex++;
}
dlgFile.m_ofn.nFilterIndex = nIndex;
// strDefExt is necessary to hold onto the memory from GetExtFromType
CString strDefExt = GetExtFromType(GetTypeFromIndex(nIndex-1, bOpenFileDialog));
dlgFile.m_ofn.lpstrDefExt = strDefExt;
CString strFilter = GetFileTypes(bOpenFileDialog);
dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
BOOL bRet = (dlgFile.DoModal() == IDOK) ? TRUE : FALSE;
fileName.ReleaseBuffer();
if (bRet)
{
if (bOpenFileDialog)
m_nFilterIndex = dlgFile.m_ofn.nFilterIndex;
if (pType != NULL)
{
int nIndex = (int)dlgFile.m_ofn.nFilterIndex - 1;
ASSERT(nIndex >= 0);
*pType = GetTypeFromIndex(nIndex, bOpenFileDialog);
}
}
return bRet;
}
void CWordPadApp::OnFileOpen()
{
// prompt the user (with all document templates)
CString newName;
int nType = RD_DEFAULT;
if (!PromptForFileName(newName, AFX_IDS_OPENFILE,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, &nType))
return; // open cancelled
if (nType == RD_OEMTEXT)
m_bForceOEM = TRUE;
OpenDocumentFile(newName);
m_bForceOEM = FALSE;
// if returns NULL, the user has already been alerted
}
BOOL CWordPadApp::OnDDECommand(LPTSTR /*lpszCommand*/)
{
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// DDE and ShellExecute support
//HKEY_CLASSES_ROOT\.RTF = rtffile
//HKEY_CLASSES_ROOT\rtffile =
//HKEY_CLASSES_ROOT\rtffile\CLSID = {73FDDC80-AEA9-101A-98A7-00AA00374959}
//HKEY_CLASSES_ROOT\rtffile\protocol\StdFileEditing\server = WORDPAD.EXE
//HKEY_CLASSES_ROOT\rtffile\protocol\StdFileEditing\verb\0 = &Edit
//HKEY_CLASSES_ROOT\rtffile\shell\open\command = WORDPAD.EXE %1
//HKEY_CLASSES_ROOT\rtffile\shell\print\command = WORDPAD.EXE /p %1
#define REGENTRY(key, value) _T(key) _T("\0\0") _T(value)
#define REGENTRYX(key, valuename, value) _T(key) _T("\0") _T(valuename) _T("\0") _T(value)
static const TCHAR sz00[] = REGENTRY("%2", "%5");
static const TCHAR sz01[] = REGENTRY("%2\\CLSID", "%1");
static const TCHAR sz02[] = REGENTRY("%2\\Insertable", "");
static const TCHAR sz03[] = REGENTRY("%2\\protocol\\StdFileEditing\\verb\\0", "&Edit");
static const TCHAR sz04[] = REGENTRY("%2\\protocol\\StdFileEditing\\server", "%3");
static const TCHAR sz05[] = REGENTRY("CLSID\\%1", "%5");
static const TCHAR sz06[] = REGENTRY("CLSID\\%1\\ProgID", "%2");
static const TCHAR sz07[] = REGENTRY("CLSID\\%1\\InprocHandler32", "ole32.dll");
static const TCHAR sz08[] = REGENTRY("CLSID\\%1\\LocalServer32", "%3");
static const TCHAR sz09[] = REGENTRY("CLSID\\%1\\Verb\\0", "&Edit,0,2");
static const TCHAR sz10[] = REGENTRY("CLSID\\%1\\Verb\\1", "&Open,0,2");
static const TCHAR sz11[] = REGENTRY("CLSID\\%1\\Insertable", "");
static const TCHAR sz12[] = REGENTRY("CLSID\\%1\\AuxUserType\\2", "%4");
static const TCHAR sz13[] = REGENTRY("CLSID\\%1\\AuxUserType\\3", "%6");
static const TCHAR sz14[] = REGENTRY("CLSID\\%1\\DefaultIcon", "%3,1");
static const TCHAR sz15[] = REGENTRY("CLSID\\%1\\MiscStatus", "0");
static const TCHAR sz16[] = REGENTRY("%2\\shell\\open\\command", "%3 \"%%1\"");
static const TCHAR sz17[] = REGENTRY("%2\\shell\\print\\command", "%3 /p \"%%1\"");
static const TCHAR sz18[] = REGENTRY("%7", "%2");
static const TCHAR sz19[] = REGENTRY("%2", ""); // like sz00 only no long type name
static const TCHAR sz20[] = REGENTRY("%2\\shell\\printto\\command", "%3 /pt \"%%1\" \"%%2\" \"%%3\" \"%%4\"");
static const TCHAR sz21[] = REGENTRY("%2\\DefaultIcon", "%3,%8");
static const TCHAR sz22[] = REGENTRYX("%7\\ShellNew", "NullFile", "true");
static const TCHAR sz23[] = REGENTRYX("%7\\ShellNew", "Data", "{\\rtf1}");
// %1 - class ID
// %2 - class name WordPad.Document.1
// %3 - SFN executable path C:\PROGRA~1\ACCESS~1\WORDPAD.EXE
// %4 - short type name Document
// %5 - long type name Microsoft WordPad Document
// %6 - long application name Microsoft WordPad
// %7 = extension .rtf
// %8 = default icon 0,1,2,3
#define NUM_REG_ARGS 8
static const LPCTSTR rglpszWordPadRegister[] =
{sz00, sz02, sz03, sz05, sz09, sz10, sz11, sz15, NULL};
static const LPCTSTR rglpszWordPadOverwrite[] =
{sz01, sz04, sz06, sz07, sz08, sz12, sz13, sz14, sz16, sz17, sz20, NULL};
//static const LPCTSTR rglpszExtRegister[] =
//{sz00, sz18, NULL};
//static const LPCTSTR rglpszExtOverwrite[] =
//{sz01, sz16, sz17, sz21, NULL};
static const LPCTSTR rglpszWriExtRegister[] =
{sz18, NULL};
static const LPCTSTR rglpszWriRegister[] =
{sz00, sz01, sz16, sz17, sz20, sz21, NULL};
static const LPCTSTR rglpszRtfExtRegister[] =
{sz18, sz23, NULL};
static const LPCTSTR rglpszRtfRegister[] =
{sz00, sz01, sz16, sz17, sz20, sz21, NULL};
static const LPCTSTR rglpszTxtExtRegister[] =
{sz18, sz22, NULL};
static const LPCTSTR rglpszTxtRegister[] =
{sz00, sz01, sz16, sz17, sz20, sz21, NULL};
static const LPCTSTR rglpszDocExtRegister[] =
{sz18, sz22, NULL};
static const LPCTSTR rglpszDocRegister[] =
{sz00, sz01, sz16, sz17, sz20, sz21, NULL};
static void RegisterExt(LPCTSTR lpszExt, LPCTSTR lpszProgID, UINT nIDTypeName,
LPCTSTR* rglpszSymbols, LPCTSTR* rglpszExtRegister,
LPCTSTR* rglpszRegister, int nIcon)
{
// don't overwrite anything with the extensions
CString strWhole;
VERIFY(strWhole.LoadString(nIDTypeName));
CString str;
AfxExtractSubString(str, strWhole, DOCTYPE_PROGID);
rglpszSymbols[1] = lpszProgID;
rglpszSymbols[4] = str;
rglpszSymbols[6] = lpszExt;
TCHAR buf[10];
wsprintf(buf, _T("%d"), nIcon);
rglpszSymbols[7] = buf;
// check for .ext and progid
CKey key;
if (!key.Open(HKEY_CLASSES_ROOT, lpszExt)) // .ext doesn't exist
RegisterHelper(rglpszExtRegister, rglpszSymbols, TRUE);
key.Close();
if (!key.Open(HKEY_CLASSES_ROOT, lpszProgID)) // ProgID doesn't exist (i.e. txtfile)
RegisterHelper(rglpszRegister, rglpszSymbols, TRUE);
}
void CWordPadApp::UpdateRegistry()
{
USES_CONVERSION;
LPOLESTR lpszClassID = NULL;
CDocTemplate* pDocTemplate = &DocTemplate;
// get registration info from doc template string
CString strServerName;
CString strLocalServerName;
CString strLocalShortName;
if (!pDocTemplate->GetDocString(strServerName,
CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
{
TRACE0("Error: not enough information in DocTemplate to register OLE server.\n");
return;
}
if (!pDocTemplate->GetDocString(strLocalServerName,
CDocTemplate::regFileTypeName))
strLocalServerName = strServerName; // use non-localized name
if (!pDocTemplate->GetDocString(strLocalShortName,
CDocTemplate::fileNewName))
strLocalShortName = strLocalServerName; // use long name
ASSERT(strServerName.Find(' ') == -1); // no spaces allowed
::StringFromCLSID(clsid, &lpszClassID);
ASSERT (lpszClassID != NULL);
// get path name to server
TCHAR szLongPathName[_MAX_PATH];
TCHAR szShortPathName[_MAX_PATH];
::GetModuleFileName(AfxGetInstanceHandle(), szLongPathName, _MAX_PATH);
::GetShortPathName(szLongPathName, szShortPathName, _MAX_PATH);
LPCTSTR rglpszSymbols[NUM_REG_ARGS];
rglpszSymbols[0] = OLE2CT(lpszClassID);
rglpszSymbols[1] = strServerName;
rglpszSymbols[2] = szShortPathName;
rglpszSymbols[3] = strLocalShortName;
rglpszSymbols[4] = strLocalServerName;
rglpszSymbols[5] = m_pszAppName; // will usually be long, readable name
rglpszSymbols[6] = NULL;
if (RegisterHelper((LPCTSTR*)rglpszWordPadRegister, rglpszSymbols, FALSE))
RegisterHelper((LPCTSTR*)rglpszWordPadOverwrite, rglpszSymbols, TRUE);
// RegisterExt(_T(".txt"), _T("txtfile"), IDS_TEXT_DOC, rglpszSymbols,
// (LPCTSTR*)rglpszTxtExtRegister, (LPCTSTR*)rglpszTxtRegister, 3);
RegisterExt(_T(".rtf"), _T("rtffile"), IDS_RICHTEXT_DOC, rglpszSymbols,
(LPCTSTR*)rglpszRtfExtRegister, (LPCTSTR*)rglpszRtfRegister, 1);
RegisterExt(_T(".wri"), _T("wrifile"), IDS_WRITE_DOC, rglpszSymbols,
(LPCTSTR*)rglpszWriExtRegister, (LPCTSTR*)rglpszWriRegister, 2);
RegisterExt(_T(".doc"), _T("WordPad.Document.1"), IDS_WINWORD6_DOC, rglpszSymbols,
(LPCTSTR*)rglpszDocExtRegister, (LPCTSTR*)rglpszDocRegister, 1);
// free memory for class ID
ASSERT(lpszClassID != NULL);
CoTaskMemFree(lpszClassID);
}
BOOL RegisterHelper(LPCTSTR* rglpszRegister, LPCTSTR* rglpszSymbols,
BOOL bReplace)
{
ASSERT(rglpszRegister != NULL);
ASSERT(rglpszSymbols != NULL);
CString strKey;
CString strValueName;
CString strValue;
// keeping a key open makes this go a bit faster
CKey keyTemp;
VERIFY(keyTemp.Create(HKEY_CLASSES_ROOT, _T("CLSID")));
BOOL bResult = TRUE;
while (*rglpszRegister != NULL)
{
LPCTSTR lpszKey = *rglpszRegister++;
if (*lpszKey == '\0')
continue;
LPCTSTR lpszValueName = lpszKey + lstrlen(lpszKey) + 1;
LPCTSTR lpszValue = lpszValueName + lstrlen(lpszValueName) + 1;
strKey.ReleaseBuffer(
FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, lpszKey, NULL, NULL,
strKey.GetBuffer(256), 256, (va_list*) rglpszSymbols));
strValueName = lpszValueName;
strValue.ReleaseBuffer(
FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ARGUMENT_ARRAY, lpszValue, NULL, NULL,
strValue.GetBuffer(256), 256, (va_list*) rglpszSymbols));
if (strKey.IsEmpty())
{
TRACE1("Warning: skipping empty key '%s'.\n", lpszKey);
continue;
}
CKey key;
VERIFY(key.Create(HKEY_CLASSES_ROOT, strKey));
if (!bReplace)
{
CString str;
if (key.GetStringValue(str, strValueName) && !str.IsEmpty())
continue;
}
if (!key.SetStringValue(strValue, strValueName))
{
TRACE2("Error: failed setting key '%s' to value '%s'.\n",
(LPCTSTR)strKey, (LPCTSTR)strValue);
bResult = FALSE;
break;
}
}
return bResult;
}
void CWordPadApp::WinHelp(DWORD dwData, UINT nCmd)
{
if (nCmd == HELP_INDEX || nCmd == HELP_CONTENTS)
nCmd = HELP_FINDER;
CWinApp::WinHelp(dwData, nCmd);
}
BOOL CWordPadApp::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_PAINT)
return FALSE;
// CWinApp::PreTranslateMessage does nothing but call base
return CWinThread::PreTranslateMessage(pMsg);
}
void CWordPadApp::NotifyPrinterChanged(BOOL bUpdatePrinterSelection)
{
if (bUpdatePrinterSelection)
UpdatePrinterSelection(FALSE);
POSITION pos = m_listPrinterNotify.GetHeadPosition();
while (pos != NULL)
{
HWND hWnd = m_listPrinterNotify.GetNext(pos);
::SendMessage(hWnd, m_nPrinterChangedMsg, 0, 0);
}
}
BOOL CWordPadApp::IsIdleMessage(MSG* pMsg)
{
if (pMsg->message == WM_MOUSEMOVE || pMsg->message == WM_NCMOUSEMOVE)
return FALSE;
return CWinApp::IsIdleMessage(pMsg);
}
HGLOBAL CWordPadApp::CreateDevNames()
{
HGLOBAL hDev = NULL;
if (!cmdInfo.m_strDriverName.IsEmpty() && !cmdInfo.m_strPrinterName.IsEmpty() &&
!cmdInfo.m_strPortName.IsEmpty())
{
hDev = GlobalAlloc(GPTR, 4*sizeof(WORD)+
cmdInfo.m_strDriverName.GetLength() + 1 +
cmdInfo.m_strPrinterName.GetLength() + 1 +
cmdInfo.m_strPortName.GetLength()+1);
LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(hDev);
lpDev->wDriverOffset = sizeof(WORD)*4;
lstrcpy((TCHAR*)lpDev + lpDev->wDriverOffset, cmdInfo.m_strDriverName);
lpDev->wDeviceOffset = (WORD)(lpDev->wDriverOffset + cmdInfo.m_strDriverName.GetLength()+1);
lstrcpy((TCHAR*)lpDev + lpDev->wDeviceOffset, cmdInfo.m_strPrinterName);
lpDev->wOutputOffset = (WORD)(lpDev->wDeviceOffset + cmdInfo.m_strPrinterName.GetLength()+1);
lstrcpy((TCHAR*)lpDev + lpDev->wOutputOffset, cmdInfo.m_strPortName);
lpDev->wDefault = 0;
}
return hDev;
}
void CWordPadApp::_OpenPaper(CString PaperFile)
{
// prompt the user (with all document templates)
int nType = RD_RICHTEXT;
if (nType == RD_OEMTEXT)
m_bForceOEM = TRUE;
OpenDocumentFile(PaperFile);
m_bForceOEM = FALSE;
}
//获取当前应用程序路径
CString p_GetBinDir()
{
char szModuleName[MAX_PATH];
char szDrive[4], szPath[MAX_PATH];
GetModuleFileName(AfxGetApp()->m_hInstance,(LPSTR)szModuleName,sizeof(szModuleName));
_splitpath (szModuleName,szDrive,szPath,NULL,NULL);
CString str;
str.Empty();
str = (LPSTR)szDrive;
str += (LPSTR)szPath;
str = str.Left(str.GetLength() - 1);
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -