?? ddxm.cpp
字號:
// ddxm.cpp : implementation file
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "ddxm.h"
#include "resource.h"
CMTextUnit CMTextDDXM::m_units[7] =
{
// TPU, SmallDiv, MedDiv, LargeDiv, MinMove, szAbbrev, bSpace
CMTextUnit(568, 142, 284, 568, 142, IDS_CM_ABBREV, TRUE),//centimeters
CMTextUnit(1440, 180, 720, 1440, 90, IDS_CM_ABBREV, FALSE),//inches
CMTextUnit(20, 120, 720, 720, 100, IDS_CM_ABBREV, TRUE),//points
CMTextUnit(240, 240, 1440, 1440, 120, IDS_CM_ABBREV, TRUE),//picas
CMTextUnit(1440, 180, 720, 1440, 90, IDS_CM_ABBREV, FALSE),//in
CMTextUnit(1440, 180, 720, 1440, 90, IDS_CM_ABBREV, FALSE),//inch
CMTextUnit(1440, 180, 720, 1440, 90, IDS_CM_ABBREV, FALSE)//inches
};
int CMTextDDXM::m_nUnits=0;
const int CMTextDDXM::m_nNumUnits = 7;
double CMTextDDXM::m_FontSizeScale=1.0;
// this routine prints a floatingpoint number with 2 digits after the decimal
void CMTextDDXM::DDX_Twips(CDataExchange* pDX, int nIDC, int& value)
{
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
TCHAR szT[64];
if (pDX->m_bSaveAndValidate)
{
::GetWindowText(hWndCtrl, szT, sizeof(szT));
if (szT[0] != NULL) // not empty
{
if (!ParseMeasurement(szT, value))
{
AfxMessageBox(IDS_INVALID_MEASUREMENT,MB_OK|MB_ICONINFORMATION);
pDX->Fail(); // throws exception
}
PrintTwips(szT, value, 2);
ParseMeasurement(szT, value);
}
else // empty
value = INT_MAX;
}
else
{
// convert from twips to default units
if (value != INT_MAX)
{
PrintTwips(szT, value, 2);
SetWindowText(hWndCtrl, szT);
}
}
}
void CMTextDDXM::DDV_MinMaxTwips(CDataExchange* pDX, int value, int minVal, int maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
{
// "The measurement must be between %1 and %2."
if (!pDX->m_bSaveAndValidate)
{
TRACE0("Warning: initial dialog data is out of range.\n");
return; // don't stop now
}
TCHAR szMin[32];
TCHAR szMax[32];
PrintTwips(szMin, minVal, 2);
PrintTwips(szMax, maxVal, 2);
CString prompt;
AfxFormatString2(prompt, IDS_MEASUREMENT_RANGE, szMin, szMax);
AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDS_APP_TITLE);
prompt.Empty(); // exception prep
pDX->Fail();
}
}
BOOL CMTextDDXM::ParseMeasurement(LPTSTR buf, int& lVal)
{
TCHAR* pch;
if (buf[0] == NULL)
return FALSE;
float f = (float)_tcstod(buf,&pch);
// eat white space, if any
while (isspace(*pch))
pch++;
if (pch[0] == NULL) // default
{
lVal = (f < 0.f) ? (int)(f*GetTPU()-0.5f) : (int)(f*GetTPU()+0.5f);
return TRUE;
}
for (int i=0;i<m_nNumUnits;i++)
{
if (lstrcmpi(pch, GetAbbrev(i)) == 0)
{
lVal = (f < 0.f) ? (int)(f*GetTPU(i)-0.5f) : (int)(f*GetTPU(i)+0.5f);
return TRUE;
}
}
return FALSE;
}
int CMTextDDXM::GetUnits() {return CMTextDDXM::m_nUnits;}
int CMTextDDXM::GetTPU() { return GetTPU(CMTextDDXM::m_nUnits);}
int CMTextDDXM::GetTPU(int n) { return CMTextDDXM::m_units[n].m_nTPU;}
LPCTSTR CMTextDDXM::GetAbbrev() { return CMTextDDXM::m_units[m_nUnits].m_strAbbrev;}
LPCTSTR CMTextDDXM::GetAbbrev(int n) { return CMTextDDXM::m_units[n].m_strAbbrev;}
const CMTextUnit& CMTextDDXM::GetUnit() {return CMTextDDXM::m_units[m_nUnits];}
BOOL CMTextDDXM::Initial()
{
for (int i=0;i<m_nNumUnits;i++)
CMTextDDXM::m_units[i].m_strAbbrev.LoadString(CMTextDDXM::m_units[i].m_nAbbrevID);
return TRUE;
}
void CMTextDDXM::PrintTwips(TCHAR* buf, int nValue, int nDec)
{
ASSERT(nDec == 2);
int div = GetTPU();
int lval = nValue;
BOOL bNeg = FALSE;
int* pVal = new int[nDec+1];
if (lval < 0)
{
bNeg = TRUE;
lval = -lval;
}
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 (CDDXM::m_units[m_nUnits].m_bSpaceAbbrev)
// lstrcat(buf, _T(" "));
// lstrcat(buf, GetAbbrev());
delete []pVal;
}
const CMTextUnit& CMTextUnit::operator=(const CMTextUnit& unit)
{
m_nTPU = unit.m_nTPU;
m_nSmallDiv = unit.m_nSmallDiv;
m_nMediumDiv = unit.m_nMediumDiv;
m_nLargeDiv = unit.m_nLargeDiv;
m_nMinMove = unit.m_nMinMove;
m_nAbbrevID = unit.m_nAbbrevID;
m_bSpaceAbbrev = unit.m_bSpaceAbbrev;
m_strAbbrev = unit.m_strAbbrev;
return *this;
}
CMTextUnit::CMTextUnit(int nTPU, int nSmallDiv, int nMediumDiv, int nLargeDiv,
int nMinMove, UINT nAbbrevID, BOOL bSpaceAbbrev)
{
m_nTPU = nTPU;
m_nSmallDiv = nSmallDiv;
m_nMediumDiv = nMediumDiv;
m_nLargeDiv = nLargeDiv;
m_nMinMove = nMinMove;
m_nAbbrevID = nAbbrevID;
m_bSpaceAbbrev = bSpaceAbbrev;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -