AutoFont.cpp
上传用户:wangdan
上传日期:2022-06-30
资源大小:739k
文件大小:7k
源码类别:

3D图形编程

开发平台:

Visual C++

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