WTL Controls
Back to the WTL Controls Home Page.
MiscFunctions.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 //--------------------------------------------------------------------------------------
3 //--------------------------------------------------------------------------------------
4 //
5 //
6 // WTL Control Library
7 // by
8 // Bright Ideas Software®
9 // Copyright © 2018 by Bright Ideas Software.
10 //
11 //
12 //
13 //--------------------------------------------------------------------------------------
14 //--------------------------------------------------------------------------------------
15 
16 
17 
18 
19 namespace BIS_WTL_CONTROLS
20 {
21  //------------------------------------------------------------------------------------------------
34  BOOL OutlineRect( HDC HDestDC, const RECT& RDest)
35  {
36  ATLASSERT(HDestDC != NULL);
37 
38  // select a hollow brush into the target DC
39  HBRUSH HOldBrush = static_cast<HBRUSH>(
40  SelectObject(HDestDC, GetStockObject(HOLLOW_BRUSH))
41  );
42 
43  // render the outline of the rectangle
44  BOOL result =
45  Rectangle(
46  HDestDC,
47  RDest.left, RDest.top,
48  RDest.right, RDest.bottom
49  );
50 
51  // restore the original brush
52  SelectObject(HDestDC, HOldBrush);
53 
54  return result;
55  }
56  //------------------------------------------------------------------------------------------------
68  CString Int_To_String(int value, int max_digits)
69  {
70  CString retval;
71 
72  _itot_s(value, retval.GetBufferSetLength(max_digits + 1), max_digits + 1, 10);
73  retval.ReleaseBuffer();
74 
75  for (int i = 0; i < (max_digits + 1); i++) // trim leading zeros
76  {
77  if (retval.GetAt(i) == 0)
78  {
79  retval = retval.Left(i);
80  i = (max_digits + 2);
81  }
82  }
83 
84  return retval;
85  }
86  //------------------------------------------------------------------------------------------------
98  CString Double_To_String(double number, int precision)
99  {
100  CString retval;
101 
102  retval.Format(_T("%.*g"), precision, number);
103  retval.MakeUpper ();
104 
105  return(retval);
106  }
107  //------------------------------------------------------------------------------------------------
119  double Sgn(double value)
120  {
121  if (value > 0.0)
122  return 1.0;
123  else if (value < 0.0)
124  return -1.0;
125  else
126  return (0.0);
127  }
128  //------------------------------------------------------------------------------------------------
129  //------------------------------------------------------------------------------------------------
138  {
139  DWORD error = GetLastError();
140 
141  LPVOID lpMsgBuf;
142  FormatMessage(
143  FORMAT_MESSAGE_ALLOCATE_BUFFER |
144  FORMAT_MESSAGE_FROM_SYSTEM |
145  FORMAT_MESSAGE_IGNORE_INSERTS,
146  NULL,
147  error,
148  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
149  (LPTSTR) &lpMsgBuf,
150  0,
151  NULL
152  );
153  // Process any inserts in lpMsgBuf.
154  // ...
155  // Display the string.
156  MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("Error"), MB_OK | MB_ICONINFORMATION );
157  // Free the buffer.
158  LocalFree( lpMsgBuf );
159  }
160  //------------------------------------------------------------------------------------------------
161  //------------------------------------------------------------------------------------------------
173  int GetCharHeight(HDC dc)
174  {
175  TEXTMETRIC tm{ 0 };
176  GetTextMetrics(dc, &tm);
177 
178  int char_height = tm.tmHeight + tm.tmExternalLeading;
179 
180  return (char_height);
181  }
182  //------------------------------------------------------------------------------------------------
197  int GetTextLineCount(HDC dc, CString text, int width, BOOL IsEditControl)
198  {
199 
200  int lines = 0;
201  CRect rect;
202 
203  rect.top = rect.left = 0;
204  int char_height = GetCharHeight(dc);
205 
206  rect.bottom = char_height;
207  rect.right = width;
208 
209  UINT format = DT_WORDBREAK | DT_LEFT | DT_CALCRECT;
210  if (IsEditControl)
211  format |= DT_EDITCONTROL;
212 
213  int box_height = ::DrawText(dc, text, 1, &rect, format);
214 
215  if (char_height != 0)
216  lines = (int)ceil(((float)box_height)/((float)char_height));
217 
218  return (lines);
219  }
220  //------------------------------------------------------------------------------------------------
233  int GetTextLength(CString str, HFONT font)
234  {
235  int length = 0;
236 
237  HDC dc = ::CreateCompatibleDC(NULL);
238 
239  if (dc != NULL)
240  {
241  HFONT old_font = (HFONT)::SelectObject(dc, font);
242 
243  SIZE sz;
244  if (::GetTextExtentPoint32(dc, str, str.GetLength(), &sz) != 0)
245  length = sz.cx;
246 
247  ::SelectObject(dc, old_font);
248  ::DeleteDC(dc);
249  }
250 
251  return(length);
252  }
253  //------------------------------------------------------------------------------------------------
264  BOOL FilePresent(LPCTSTR FullFileName)
265  {
266  TCHAR TRUEpath[MAX_PATH];
267 
268  LPCTSTR lpPath = NULL;//address of search path
269  LPCTSTR lpFileName = FullFileName;// address of filename
270  LPCTSTR lpExtension = NULL; // address of extension
271  DWORD nBufferLength = MAX_PATH; // size, in characters, of buffer
272  LPTSTR lpBuffer = &TRUEpath[0];//address of buffer for found filename
273  LPTSTR *lpFilePart = NULL; // address of pointer to file component
274 
275  DWORD chars = SearchPath(lpPath,lpFileName,lpExtension,nBufferLength,
276  lpBuffer,lpFilePart);
277 
278  if (chars > 0)
279  return(TRUE);
280  else
281  return(FALSE);
282 
283  }
284  //------------------------------------------------------------------------------------------------
297  void DlgComboInit(int nDlgID, HWND DlgHwnd)
298  { // fixes ATL bug which prevent combo strings
299  HINSTANCE hInstance = _Module.GetResourceInstance(); // from loading from the resource file
300  HRSRC hResource = ::FindResource(hInstance, MAKEINTRESOURCE(nDlgID), RT_DLGINIT);
301 
302  // If not DLGINIT, that's not an error
303  if (hResource == NULL)
304  return;
305 
306  HGLOBAL hGlobal = ::LoadResource(hInstance, hResource);
307  if (hGlobal == NULL)
308  return;
309  LPVOID lpResource = ::LockResource(hGlobal);
310  if (lpResource == NULL)
311  return;
312 
313  BOOL bSuccess = TRUE;
314  // Now walk the loaded resource
315  UNALIGNED WORD* lpnRes = (WORD*)lpResource;
316  while (bSuccess && *lpnRes != 0)
317  {
318  WORD nIDC = *lpnRes++;
319  WORD nMsg = *lpnRes++;
320  DWORD dwLen = *((UNALIGNED DWORD*&)lpnRes)++;
321 
323  #define WIN16_LB_ADDSTRING 0x0401
324  #define WIN16_CB_ADDSTRING 0x0403
325  #define AFX_CB_ADDSTRING 0x1234
326 
328  if (nMsg == AFX_CB_ADDSTRING)
329  nMsg = CBEM_INSERTITEM;
330  else if (nMsg == WIN16_LB_ADDSTRING)
331  nMsg = LB_ADDSTRING;
332  else if (nMsg == WIN16_CB_ADDSTRING)
333  nMsg = CB_ADDSTRING;
334 
335  ATLASSERT(nMsg == LB_ADDSTRING || nMsg == CB_ADDSTRING || nMsg == CBEM_INSERTITEM);
336 
337 
338  if (nMsg == CBEM_INSERTITEM)
339  {
340  COMBOBOXEXITEM item;
341  item.mask = CBEIF_TEXT;
342  item.iItem = -1;
343  item.pszText = CA2T(LPSTR(lpnRes)).m_szBuffer;
344 
345  if (::SendDlgItemMessage(DlgHwnd, nIDC, nMsg, 0, (LPARAM) &item) == -1)
346  bSuccess = FALSE;
347  }
348  else if (nMsg == LB_ADDSTRING || nMsg == CB_ADDSTRING)
349  {
350  // ListCombobox returns -1 for error
351 
352  int result = ::SendDlgItemMessage(DlgHwnd, nIDC, nMsg, 0, (LPARAM)CA2T(LPSTR(lpnRes)).m_szBuffer);
353  if (result == -1)
354  bSuccess = FALSE;
355  }
356 
357  // skip past data
358  lpnRes = (WORD*)((LPBYTE)lpnRes + (UINT)dwLen);
359  }
360  }
361  //------------------------------------------------------------------------------------------------
362 };
CString Double_To_String(double number, int precision)
Convert a double to a CString, with a given number of significant digits.
int GetTextLineCount(HDC dc, CString text, int width, BOOL IsEditControl)
Returns the number of lines of text required to fit a string in a given width.
BOOL FilePresent(LPCTSTR FullFileName)
Tests for the presence of a file using SearchPath.
int GetTextLength(CString str, HFONT font)
Measure the length, in pixels, of a string using a given font.
BOOL OutlineRect(HDC HDestDC, const RECT &RDest)
Draw a rectangular outline with the current pen.
void DlgComboInit(int nDlgID, HWND DlgHwnd)
Load combo boxes with strings stored in a resources file.
int GetCharHeight(HDC dc)
Returns the height of a standard test string, in pixels.
double Sgn(double value)
Test the sign of a double.
void GetLastErrorString()
Show a Message Box containing human-friendly information about the last failed system API call...
CString Int_To_String(int value, int max_digits)
Convert a multi-digit integer into a CString.