PropertyGridItemFont.cpp 2.0 KB

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