PropertyGridItemFont.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "StdAfx.h"
  2. #include "PropertyGridInplaceEdit.h"
  3. #include "PropertyGridInplaceButton.h"
  4. #include "PropertyGridInplaceList.h"
  5. #include "PropertyGridItem.h"
  6. #include "PropertyGridItemFont.h"
  7. #include "PropertyGrid.h"
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CPropertyGridItemFont
  10. CPropertyGridItemFont::CPropertyGridItemFont(CString strCaption, LOGFONT& font)
  11. : CPropertyGridItem(strCaption)
  12. {
  13. m_pBindFont = NULL;
  14. SetFont(font);
  15. m_nFlags = pgitemHasExpandButton;
  16. m_nFlags = pgitemHasEdit | pgitemHasExpandButton;
  17. }
  18. CPropertyGridItemFont::CPropertyGridItemFont(UINT nID, LOGFONT& font)
  19. : CPropertyGridItem(nID)
  20. {
  21. m_pBindFont = NULL;
  22. SetFont(font);
  23. m_nFlags = pgitemHasExpandButton;
  24. m_nFlags = pgitemHasEdit | pgitemHasExpandButton;
  25. }
  26. CPropertyGridItemFont::~CPropertyGridItemFont(void)
  27. {
  28. }
  29. /////////////////////////////////////////////////////////////////////////////
  30. //
  31. void CPropertyGridItemFont::SetFont(LOGFONT& font)
  32. {
  33. memcpy(&m_lfValue, &font, sizeof(LOGFONT));
  34. SetValue( FontToString(m_lfValue) );
  35. if (m_pBindFont)
  36. memcpy(m_pBindFont, &m_lfValue, sizeof(LOGFONT));
  37. }
  38. CString CPropertyGridItemFont::FontToString(LOGFONT& font)
  39. {
  40. CWindowDC dc(CWnd::GetDesktopWindow());
  41. int nHeight = -MulDiv(font.lfHeight, 72, dc.GetDeviceCaps(LOGPIXELSY));
  42. CString strFont;
  43. strFont.Format(_T("%s; %ipt"), font.lfFaceName, nHeight);
  44. return strFont;
  45. }
  46. void CPropertyGridItemFont::OnInplaceButtonDown()
  47. {
  48. LOGFONT lf = m_lfValue;
  49. CFontDialog dlg(&lf, CF_EFFECTS | CF_SCREENFONTS | CF_ENABLEHOOK);
  50. dlg.m_cf.lpfnHook = FontDlgProc;
  51. if (dlg.DoModal() == IDOK)
  52. {
  53. dlg.GetCurrentFont(&lf);
  54. SetFont(lf);
  55. OnValueChanged(m_strValue);
  56. }
  57. }
  58. UINT CALLBACK CPropertyGridItemFont::FontDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  59. {
  60. if (message == WM_INITDIALOG)
  61. {
  62. // 初始化对话框时,隐藏跟字体颜色相关的两个控件:
  63. // ID为0x443的静态文本和ID为0x473的组合框
  64. HWND hWndStatic = ::GetDlgItem(hDlg, 0x443);
  65. HWND hWndCombo = ::GetDlgItem(hDlg, 0x473);
  66. if (hWndStatic)
  67. ::ShowWindow(hWndStatic, SW_HIDE);
  68. if (hWndCombo)
  69. ::ShowWindow(hWndCombo, SW_HIDE);
  70. }
  71. return 0;
  72. }