Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.3 KiB

  1. // Copyright (c) 2002 Microsoft Corporation
  2. //
  3. // File: uiutil.cpp
  4. //
  5. // Synopsis: Commonly used UI functions
  6. //
  7. // History: 01/22/2002 JeffJon Created
  8. #include "pch.h"
  9. #include "resource.h"
  10. // Creates the fonts for setLargeFonts().
  11. //
  12. // hDialog - handle to a dialog to be used to retrieve a device
  13. // context.
  14. //
  15. // bigBoldFont - receives the handle of the big bold font created.
  16. void
  17. InitFonts(
  18. HWND hDialog,
  19. HFONT& bigBoldFont)
  20. {
  21. ASSERT(Win::IsWindow(hDialog));
  22. HRESULT hr = S_OK;
  23. do
  24. {
  25. NONCLIENTMETRICS ncm;
  26. memset(&ncm, 0, sizeof(ncm));
  27. ncm.cbSize = sizeof(ncm);
  28. hr = Win::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
  29. BREAK_ON_FAILED_HRESULT(hr);
  30. LOGFONT bigBoldLogFont = ncm.lfMessageFont;
  31. bigBoldLogFont.lfWeight = FW_BOLD;
  32. String fontName = String::load(IDS_BIG_BOLD_FONT_NAME);
  33. // ensure null termination 260237
  34. memset(bigBoldLogFont.lfFaceName, 0, LF_FACESIZE * sizeof(TCHAR));
  35. size_t fnLen = fontName.length();
  36. fontName.copy(
  37. bigBoldLogFont.lfFaceName,
  38. // don't copy over the last null
  39. min(LF_FACESIZE - 1, fnLen));
  40. unsigned fontSize = 0;
  41. String::load(IDS_BIG_BOLD_FONT_SIZE).convert(fontSize);
  42. ASSERT(fontSize);
  43. HDC hdc = 0;
  44. hr = Win::GetDC(hDialog, hdc);
  45. BREAK_ON_FAILED_HRESULT(hr);
  46. bigBoldLogFont.lfHeight =
  47. - ::MulDiv(
  48. static_cast<int>(fontSize),
  49. Win::GetDeviceCaps(hdc, LOGPIXELSY),
  50. 72);
  51. hr = Win::CreateFontIndirect(bigBoldLogFont, bigBoldFont);
  52. BREAK_ON_FAILED_HRESULT(hr);
  53. Win::ReleaseDC(hDialog, hdc);
  54. }
  55. while (0);
  56. }
  57. void
  58. InitBoldFont(
  59. HWND hDialog,
  60. HFONT& boldFont)
  61. {
  62. ASSERT(Win::IsWindow(hDialog));
  63. HRESULT hr = S_OK;
  64. do
  65. {
  66. NONCLIENTMETRICS ncm;
  67. memset(&ncm, 0, sizeof(ncm));
  68. ncm.cbSize = sizeof(ncm);
  69. hr = Win::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
  70. BREAK_ON_FAILED_HRESULT(hr);
  71. LOGFONT boldLogFont = ncm.lfMessageFont;
  72. boldLogFont.lfWeight = FW_BOLD;
  73. HDC hdc = 0;
  74. hr = Win::GetDC(hDialog, hdc);
  75. BREAK_ON_FAILED_HRESULT(hr);
  76. hr = Win::CreateFontIndirect(boldLogFont, boldFont);
  77. BREAK_ON_FAILED_HRESULT(hr);
  78. Win::ReleaseDC(hDialog, hdc);
  79. }
  80. while (0);
  81. }
  82. void
  83. SetControlFont(HWND parentDialog, int controlID, HFONT font)
  84. {
  85. ASSERT(Win::IsWindow(parentDialog));
  86. ASSERT(controlID);
  87. ASSERT(font);
  88. HWND control = Win::GetDlgItem(parentDialog, controlID);
  89. if (control)
  90. {
  91. Win::SetWindowFont(control, font, true);
  92. }
  93. }
  94. void
  95. SetLargeFont(HWND dialog, int bigBoldResID)
  96. {
  97. ASSERT(Win::IsWindow(dialog));
  98. ASSERT(bigBoldResID);
  99. static HFONT bigBoldFont = 0;
  100. if (!bigBoldFont)
  101. {
  102. InitFonts(dialog, bigBoldFont);
  103. }
  104. SetControlFont(dialog, bigBoldResID, bigBoldFont);
  105. }
  106. void
  107. SetBoldFont(HWND dialog, int boldResID)
  108. {
  109. ASSERT(Win::IsWindow(dialog));
  110. ASSERT(boldResID);
  111. static HFONT boldFont = 0;
  112. if (!boldFont)
  113. {
  114. InitBoldFont(dialog, boldFont);
  115. }
  116. SetControlFont(dialog, boldResID, boldFont);
  117. }