AutoFont.cpp
上传用户:guanx8y8
上传日期:2007-07-30
资源大小:326k
文件大小:9k
开发平台:

Visual C++

  1. //CAutoFont class implementation
  2. #include "stdafx.h"
  3. #include "AutoFont.h"
  4. CAutoFont::CAutoFont()
  5. {
  6. lf.lfHeight=16;
  7. lf.lfWidth=0;
  8. lf.lfEscapement=0;
  9. lf.lfOrientation=0;
  10. lf.lfWeight=FW_NORMAL;
  11. lf.lfItalic=0;
  12. lf.lfUnderline=0;
  13. lf.lfStrikeOut=0;
  14. lf.lfCharSet=ANSI_CHARSET;
  15. lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
  16. lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
  17. lf.lfQuality=PROOF_QUALITY;
  18. lf.lfPitchAndFamily=VARIABLE_PITCH | FF_ROMAN;
  19. strcpy(lf.lfFaceName, "Times New Roman");
  20. CreateFontIndirect(&lf);
  21. }
  22. CAutoFont::CAutoFont(CString facename)
  23. {
  24. lf.lfHeight=16;
  25. lf.lfWidth=0;
  26. lf.lfEscapement=0;
  27. lf.lfOrientation=0;
  28. lf.lfWeight=FW_NORMAL;
  29. lf.lfItalic=0;
  30. lf.lfUnderline=0;
  31. lf.lfStrikeOut=0;
  32. lf.lfCharSet=ANSI_CHARSET;
  33. lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
  34. lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
  35. lf.lfQuality=PROOF_QUALITY;
  36. lf.lfPitchAndFamily=VARIABLE_PITCH | FF_ROMAN;
  37. strcpy(lf.lfFaceName, (LPCTSTR)facename);
  38. CreateFontIndirect(&lf);
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  41. CAutoFont& CAutoFont::operator =(CAutoFont &rhs)
  42. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. {
  44. GetLogFont(&rhs.lf);
  45. SetColour(rhs.GetColour());
  46. return *this;
  47. }
  48. CAutoFont::CAutoFont(LOGFONT& logfont)
  49. {
  50. lf=logfont;
  51. CreateFontIndirect(&lf);
  52. SetColour(0,0,0);
  53. }
  54. CAutoFont::CAutoFont(CFont font)
  55. {
  56. HFONT hFont=(HFONT)font;
  57. Attach((HFONT)hFont);
  58. GetLogFont(&lf);
  59. SetColour(0,0,0);
  60. }
  61. CAutoFont::~CAutoFont()
  62. {
  63. }
  64. LONG CAutoFont::SetHeight(LONG height)
  65. {
  66. LONG l=lf.lfHeight;
  67. DeleteObject();
  68. lf.lfHeight=height;
  69. CreateFontIndirect(&lf);
  70. return l;
  71. }
  72. LONG CAutoFont::SetWidth(LONG width)
  73. {
  74. LONG l=lf.lfWidth;
  75. DeleteObject();
  76. lf.lfWidth=width;
  77. CreateFontIndirect(&lf);
  78. return l;
  79. }
  80. LONG CAutoFont::SetEscapement(LONG esc)
  81. {
  82. LONG l=lf.lfEscapement;
  83. DeleteObject();
  84. lf.lfEscapement=esc;
  85. CreateFontIndirect(&lf);
  86. return l;
  87. }
  88. LONG CAutoFont::SetOrientation(LONG or)
  89. {
  90. LONG l=lf.lfOrientation;
  91. DeleteObject();
  92. lf.lfOrientation=or;
  93. CreateFontIndirect(&lf);
  94. return l;
  95. }
  96. LONG CAutoFont::SetWeight(LONG weight)
  97. {
  98. LONG l=lf.lfWeight;
  99. DeleteObject();
  100. lf.lfWeight=weight;
  101. CreateFontIndirect(&lf);
  102. return l;
  103. }
  104. BYTE CAutoFont::SetCharset(BYTE charset)
  105. {
  106. BYTE b=lf.lfCharSet;
  107. DeleteObject();
  108. lf.lfCharSet=charset;
  109. CreateFontIndirect(&lf);
  110. return b;
  111. }
  112. BYTE CAutoFont::SetOutPrecision(BYTE op)
  113. {
  114. BYTE b=lf.lfOutPrecision;
  115. DeleteObject();
  116. lf.lfOutPrecision=op;
  117. CreateFontIndirect(&lf);
  118. return b;
  119. }
  120. BYTE CAutoFont::SetClipPrecision(BYTE cp)
  121. {
  122. BYTE b=lf.lfClipPrecision;
  123. DeleteObject();
  124. lf.lfClipPrecision=cp;
  125. CreateFontIndirect(&lf);
  126. return b;
  127. }
  128. BYTE CAutoFont::SetQuality(BYTE qual)
  129. {
  130. BYTE b=lf.lfQuality;
  131. DeleteObject();
  132. lf.lfQuality=qual;
  133. CreateFontIndirect(&lf);
  134. return b;
  135. }
  136. BYTE CAutoFont::SetPitchAndFamily(BYTE paf)
  137. {
  138. BYTE b=lf.lfPitchAndFamily;
  139. DeleteObject();
  140. lf.lfPitchAndFamily=paf;
  141. CreateFontIndirect(&lf);
  142. return b;
  143. }
  144. CString CAutoFont::SetFaceName(CString facename)
  145. {
  146. CString str=lf.lfFaceName;
  147. DeleteObject();
  148. strcpy(lf.lfFaceName, (LPCTSTR)facename);
  149. CreateFontIndirect(&lf);
  150. return str;
  151. }
  152. LPCTSTR CAutoFont::SetFaceName(LPCTSTR facename)
  153. {
  154. LPCTSTR str=lf.lfFaceName;
  155. DeleteObject();
  156. strcpy(lf.lfFaceName, facename);
  157. CreateFontIndirect(&lf);
  158. return str;
  159. }
  160. BOOL CAutoFont::SetBold(BOOL B)
  161. {
  162. BOOL b;
  163. if (B)
  164. b=SetWeight(FW_BOLD);
  165. else
  166. b=SetWeight(FW_NORMAL);
  167. if (b >= FW_MEDIUM)
  168. return TRUE;
  169. else
  170. return FALSE;
  171. }
  172. BOOL CAutoFont::SetItalic(BOOL i)
  173. {
  174. BOOL b=(BOOL)lf.lfItalic;
  175. DeleteObject();
  176. lf.lfItalic=i;
  177. CreateFontIndirect(&lf);
  178. return b;
  179. }
  180. BOOL CAutoFont::SetUnderline(BOOL u)
  181. {
  182. BOOL b=(BOOL)lf.lfUnderline;
  183. DeleteObject();
  184. lf.lfUnderline=u;
  185. CreateFontIndirect(&lf);
  186. return b;
  187. }
  188. BOOL CAutoFont::SetStrikeOut(BOOL s)
  189. {
  190. BOOL b=(BOOL)lf.lfStrikeOut;
  191. DeleteObject();
  192. lf.lfStrikeOut=s;
  193. CreateFontIndirect(&lf);
  194. return b;
  195. }
  196. void CAutoFont::SetLogFont(LOGFONT& logfont)
  197. {
  198. lf=logfont;
  199. DeleteObject();
  200. CreateFontIndirect(&lf);
  201. }
  202. LONG CAutoFont::GetHeight()
  203. {
  204. return lf.lfHeight;
  205. }
  206. LONG CAutoFont::GetWidth()
  207. {
  208. return lf.lfWidth;
  209. }
  210. LONG CAutoFont::GetEscapement()
  211. {
  212. return lf.lfEscapement;
  213. }
  214. LONG CAutoFont::GetOrientation()
  215. {
  216. return lf.lfEscapement;
  217. }
  218. LONG CAutoFont::GetWeight()
  219. {
  220. return lf.lfWeight;
  221. }
  222. BYTE CAutoFont::GetCharset()
  223. {
  224. return lf.lfCharSet;
  225. }
  226. BYTE CAutoFont::GetOutPrecision()
  227. {
  228. return lf.lfOutPrecision;
  229. }
  230. BYTE CAutoFont::GetClipPrecision()
  231. {
  232. return lf.lfClipPrecision;
  233. }
  234. BYTE CAutoFont::GetQuality()
  235. {
  236. return lf.lfQuality;
  237. }
  238. BYTE CAutoFont::GetPitchAndFamily()
  239. {
  240. return lf.lfPitchAndFamily;
  241. }
  242. LPCTSTR CAutoFont::GetFaceName()
  243. {
  244. return lf.lfFaceName;
  245. }
  246. BOOL CAutoFont::GetBold()
  247. {
  248. return lf.lfWeight >= FW_MEDIUM ? TRUE : FALSE;
  249. }
  250. BOOL CAutoFont::GetItalic()
  251. {
  252. return (BOOL)lf.lfItalic;
  253. }
  254. BOOL CAutoFont::GetUnderline()
  255. {
  256. return (BOOL)lf.lfUnderline;
  257. }
  258. BOOL CAutoFont::GetStrikeOut()
  259. {
  260. return (BOOL)lf.lfStrikeOut;
  261. }
  262. CString CAutoFont::ContractFont()
  263. {
  264. CString str;
  265. str.Format("%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%s",
  266. lf.lfHeight,
  267. lf.lfWidth,
  268. lf.lfEscapement,
  269. lf.lfOrientation,
  270. lf.lfWeight,
  271. lf.lfItalic,
  272. lf.lfUnderline,
  273. lf.lfStrikeOut,
  274. lf.lfCharSet,
  275. lf.lfOutPrecision,
  276. lf.lfClipPrecision,
  277. lf.lfQuality,
  278. lf.lfPitchAndFamily,
  279. GetRValue(m_crColour),
  280. GetGValue(m_crColour),
  281. GetBValue(m_crColour),
  282. lf.lfFaceName);
  283. return str;
  284. }
  285. void CAutoFont::ExtractFont(CString& str)
  286. {
  287. lf.lfHeight=atol((LPCTSTR)GetToken(str, ","));
  288. lf.lfWidth=atol((LPCTSTR)GetToken(str, ","));
  289. lf.lfEscapement=atol((LPCTSTR)GetToken(str, ","));
  290. lf.lfOrientation=atol((LPCTSTR)GetToken(str, ","));
  291. lf.lfWeight=atol((LPCTSTR)GetToken(str, ","));
  292. lf.lfItalic=atoi((LPCTSTR)GetToken(str, ","));
  293. lf.lfUnderline=atoi((LPCTSTR)GetToken(str, ","));
  294. lf.lfStrikeOut=atoi((LPCTSTR)GetToken(str, ","));
  295. lf.lfCharSet=atoi((LPCTSTR)GetToken(str, ","));
  296. lf.lfOutPrecision=atoi((LPCTSTR)GetToken(str, ","));
  297. lf.lfClipPrecision=atoi((LPCTSTR)GetToken(str, ","));
  298. lf.lfQuality=atoi((LPCTSTR)GetToken(str, ","));
  299. lf.lfPitchAndFamily=atoi((LPCTSTR)GetToken(str, ","));
  300. int r = atoi((LPCTSTR)GetToken(str, ","));
  301. int g = atoi((LPCTSTR)GetToken(str, ","));
  302. int b = atoi((LPCTSTR)GetToken(str, ","));
  303. SetColour(r,g,b);
  304. strcpy(lf.lfFaceName, (LPCTSTR)str);
  305. DeleteObject();
  306. CreateFontIndirect(&lf);
  307. }
  308. CString CAutoFont::GetToken(CString& str, LPCTSTR c)
  309. {
  310. int pos;
  311. CString token;
  312. pos=str.Find(c);
  313. token=str.Left(pos);
  314. str=str.Mid(pos+1);
  315. return token;
  316. }
  317. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  318. void CAutoFont::SetAntialiasQuality(int nQuality)
  319. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  320. // Undocumented feature - max quality is 4
  321. { lf.lfQuality = nQuality; }
  322. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  323. void CAutoFont::SetNonAntialiasQuality(int nQuality)
  324. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  325. // Undocumented feature - max quality is 3
  326. { lf.lfQuality = nQuality; }
  327. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  328. void CAutoFont::SetColour(COLORREF crColour) { m_crColour = crColour; }
  329. void CAutoFont::SetColour(int r, int g, int b) { m_crColour = RGB(r,g,b); }
  330. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  331. COLORREF CAutoFont::GetColour() { return m_crColour; }
  332. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  333. int CAutoFont::GetRedValue(){ return GetRValue(m_crColour); }
  334. int CAutoFont::GetGreenValue(){ return GetGValue(m_crColour); }
  335. int CAutoFont::GetBlueValue(){ return GetBValue(m_crColour); }
  336. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////