MFCool.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // MFCool.CPP
  20. //
  21. // 04/10/96 JMI Started.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Cool stuff for MFC.
  26. //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Includes.
  30. ///////////////////////////////////////////////////////////////////////////////
  31. #include "stdafx.h"
  32. #include "MFC_CNTL/MFCool.h"
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // Internal functions.
  35. ///////////////////////////////////////////////////////////////////////////////
  36. static BOOL CALLBACK EnumChildIconizeProc(HWND hWnd, long)
  37. {
  38. BOOL bRes = TRUE; // Assume we will continue enumeration.
  39. // Get class name . . .
  40. char szName[256];
  41. if (GetClassName(hWnd, szName, sizeof(szName)) > 0)
  42. {
  43. // If it is a button . . .
  44. if (stricmp(szName, "BUTTON") == 0)
  45. {
  46. // If it is an icon button . . .
  47. if ((GetWindowLong(hWnd, GWL_STYLE) & BS_ICON) > 0)
  48. {
  49. // Get the button text . . .
  50. if (GetWindowText(hWnd, szName, sizeof(szName)) > 0)
  51. {
  52. // Get the icon . . .
  53. HICON hIcon = LoadIcon(AfxGetResourceHandle(), szName);
  54. if (hIcon != NULL)
  55. {
  56. // Set the icon.
  57. SendMessage(hWnd, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon);
  58. }
  59. else
  60. {
  61. TRACE("EnumChildIconizeProc(): LoadIcon failed.\n");
  62. }
  63. }
  64. else
  65. {
  66. TRACE("EnumChildIconizeProc(): GetWindowText failed.\n");
  67. }
  68. }
  69. }
  70. }
  71. else
  72. {
  73. TRACE("EnumChildIconizeProc(): Unable to get class name.\n");
  74. }
  75. return bRes;
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // External functions.
  79. ///////////////////////////////////////////////////////////////////////////////
  80. ///////////////////////////////////////////////////////////////////////////////
  81. //
  82. // Associates all icon style buttons that are children of pdlg with the icon
  83. // specified by the window text of that button.
  84. // Returns 0 on success.
  85. //
  86. ///////////////////////////////////////////////////////////////////////////////
  87. extern short Iconize(CDialog* pdlg)
  88. {
  89. short sRes = 0; // Assume success.
  90. if (EnumChildWindows(pdlg->GetSafeHwnd(), EnumChildIconizeProc, 0L) != FALSE)
  91. {
  92. // Success.
  93. }
  94. else
  95. {
  96. TRACE("Iconize(): EnumChildWindows failed.\n");
  97. sRes = -1;
  98. }
  99. return sRes;
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////
  102. //
  103. // Store position of this window in the module's INI.
  104. // Returns 0 on success.
  105. //
  106. ///////////////////////////////////////////////////////////////////////////////
  107. extern short StorePosition(CWnd* pwnd)
  108. {
  109. short sRes = 0; // Assume success.
  110. // Construct section name.
  111. char szSection[512];
  112. pwnd->GetWindowText(szSection, sizeof(szSection));
  113. strcat(szSection, " Position");
  114. RECT rcWindow;
  115. pwnd->GetWindowRect(&rcWindow);
  116. AfxGetApp()->WriteProfileInt(szSection, "left", rcWindow.left);
  117. AfxGetApp()->WriteProfileInt(szSection, "top", rcWindow.top);
  118. AfxGetApp()->WriteProfileInt(szSection, "right", rcWindow.right);
  119. AfxGetApp()->WriteProfileInt(szSection, "bottom", rcWindow.bottom);
  120. return sRes;
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //
  124. // Restore position of this window from the module's INI. No change if no
  125. // settings stored.
  126. // Returns 0 on success.
  127. //
  128. ///////////////////////////////////////////////////////////////////////////////
  129. extern short RestorePosition(CWnd* pwnd)
  130. {
  131. short sRes = 0; // Assume success.
  132. // Construct section name.
  133. char szSection[512];
  134. pwnd->GetWindowText(szSection, sizeof(szSection));
  135. strcat(szSection, " Position");
  136. RECT rcWindow;
  137. pwnd->GetWindowRect(&rcWindow);
  138. rcWindow.left = AfxGetApp()->GetProfileInt(szSection, "left", rcWindow.left);
  139. rcWindow.top = AfxGetApp()->GetProfileInt(szSection, "top", rcWindow.top);
  140. rcWindow.right = AfxGetApp()->GetProfileInt(szSection, "right", rcWindow.right);
  141. rcWindow.bottom = AfxGetApp()->GetProfileInt(szSection, "bottom", rcWindow.bottom);
  142. if (rcWindow.top < 0)
  143. {
  144. rcWindow.bottom -= rcWindow.top;
  145. rcWindow.top = 0;
  146. }
  147. if (rcWindow.left < 0)
  148. {
  149. rcWindow.right -= rcWindow.left;
  150. rcWindow.left = 0;
  151. }
  152. CDC* pdc = pwnd->GetDC();
  153. if (pdc != NULL)
  154. {
  155. long lScrWidth = pdc->GetDeviceCaps(HORZRES);
  156. long lScrHeight = pdc->GetDeviceCaps(VERTRES);
  157. if (rcWindow.bottom > lScrHeight)
  158. {
  159. rcWindow.top -= (rcWindow.bottom - lScrHeight);
  160. rcWindow.bottom -= (rcWindow.bottom - lScrHeight);
  161. }
  162. if (rcWindow.right > lScrWidth)
  163. {
  164. rcWindow.left -= (rcWindow.right - lScrWidth);
  165. rcWindow.right -= (rcWindow.right - lScrWidth);
  166. }
  167. pwnd->ReleaseDC(pdc);
  168. }
  169. else
  170. {
  171. TRACE("RestorePosition(): Unable to get CDC. Cannot bound window by "
  172. "desktop width & height.\n");
  173. }
  174. pwnd->MoveWindow( rcWindow.left, rcWindow.top,
  175. rcWindow.right - rcWindow.left,
  176. rcWindow.bottom - rcWindow.top);
  177. return sRes;
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////
  180. // EOF
  181. ///////////////////////////////////////////////////////////////////////////////