AutoFont.cpp
上传用户:xiuanze55
上传日期:2013-06-16
资源大小:85k
文件大小:8k
源码类别:

其他数据库

开发平台:

Visual C++

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