Source code of Windows XP (NT5)
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.

229 lines
7.4 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: MISCUTIL.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 5/28/1998
  12. *
  13. * DESCRIPTION: Various utility functions we use in more than one place
  14. *
  15. *******************************************************************************/
  16. #ifndef __MISCUTIL_H_INCLUDED
  17. #define __MISCUTIL_H_INCLUDED
  18. #include <windows.h>
  19. #include "simstr.h"
  20. #include "wia.h"
  21. #include "resid.h"
  22. #if !defined(ARRAYSIZE)
  23. #define ARRAYSIZE(x) (sizeof((x))/sizeof((x)[0]))
  24. #endif
  25. #if !defined(SETFormatEtc)
  26. #define SETFormatEtc(fe, cf, asp, td, med, li) \
  27. {\
  28. (fe).cfFormat=cf;\
  29. (fe).dwAspect=asp;\
  30. (fe).ptd=td;\
  31. (fe).tymed=med;\
  32. (fe).lindex=li;\
  33. };
  34. #endif
  35. #if !defined(SETDefFormatEtc)
  36. #define SETDefFormatEtc(fe, cf, med) \
  37. {\
  38. (fe).cfFormat=cf;\
  39. (fe).dwAspect=DVASPECT_CONTENT;\
  40. (fe).ptd=NULL;\
  41. (fe).tymed=med;\
  42. (fe).lindex=-1;\
  43. };
  44. #endif
  45. #define PROP_SHEET_ERROR_NO_PAGES MAKE_HRESULT(SEVERITY_ERROR,FACILITY_NULL,1)
  46. namespace WiaUiUtil
  47. {
  48. template <class T>
  49. T Absolute( const T &m )
  50. {
  51. return((m < 0) ? -m : m);
  52. }
  53. template <class T>
  54. T Max( const T &m, const T &n )
  55. {
  56. return((m > n) ? m : n);
  57. }
  58. template <class T>
  59. T Min( const T &m, const T &n )
  60. {
  61. return((m < n) ? m : n);
  62. }
  63. template <class T>
  64. T GetMinimum( const T& nDesired, const T& nMin, const T& nStep )
  65. {
  66. T nResult = Max<T>( nMin, nDesired );
  67. if (nStep)
  68. nResult = nResult + (nResult - nMin) % nStep;
  69. return nResult;
  70. }
  71. inline bool ScreenToClient( HWND hwnd, RECT *prc )
  72. {
  73. return (::MapWindowPoints( NULL, hwnd, reinterpret_cast<POINT*>(prc), 2 ) != 0);
  74. }
  75. inline bool ClientToScreen( HWND hwnd, RECT *prc )
  76. {
  77. return (::MapWindowPoints( hwnd, NULL, reinterpret_cast<POINT*>(prc), 2 ) != 0);
  78. }
  79. inline bool ScreenToClient( HWND hwnd, RECT &rc )
  80. {
  81. return ScreenToClient( hwnd, &rc );
  82. }
  83. inline bool ClientToScreen( HWND hwnd, RECT &rc )
  84. {
  85. return ClientToScreen( hwnd, &rc );
  86. }
  87. inline int RectWidth( const RECT &rc )
  88. {
  89. return (rc.right - rc.left);
  90. }
  91. inline int RectHeight( const RECT &rc )
  92. {
  93. return (rc.bottom - rc.top);
  94. }
  95. inline LONGLONG PowerOfTwo( int nCount )
  96. {
  97. return(LONGLONG)1 << nCount;
  98. }
  99. inline int MulDivNoRound( int nNumber, int nNumerator, int nDenominator )
  100. {
  101. return(int)(((LONGLONG)nNumber * nNumerator) / nDenominator);
  102. }
  103. inline SIZE ScalePreserveAspectRatio( int nAvailX, int nAvailY, int nItemX, int nItemY )
  104. {
  105. SIZE sizeResult = { nAvailX, nAvailY };
  106. if (nItemX && nItemY)
  107. {
  108. //
  109. // Width is greater than height. X is the constraining factor
  110. //
  111. if (nAvailY*nItemX > nAvailX*nItemY)
  112. {
  113. sizeResult.cy = MulDivNoRound(nItemY,nAvailX,nItemX);
  114. }
  115. //
  116. // Height is greater than width. Y is the constraining factor
  117. //
  118. else
  119. {
  120. sizeResult.cx = MulDivNoRound(nItemX,nAvailY,nItemY);
  121. }
  122. }
  123. return sizeResult;
  124. }
  125. inline void Enable( HWND hWnd, bool bEnable )
  126. {
  127. if (hWnd && IsWindow(hWnd))
  128. {
  129. if (!IsWindowEnabled(hWnd) && bEnable)
  130. {
  131. ::EnableWindow( hWnd, TRUE );
  132. }
  133. else if (IsWindowEnabled(hWnd) && !bEnable)
  134. {
  135. ::EnableWindow( hWnd, FALSE );
  136. }
  137. }
  138. }
  139. inline void Enable( HWND hWnd, int nChildId, bool bEnable )
  140. {
  141. if (hWnd && IsWindow(hWnd))
  142. {
  143. Enable(GetDlgItem(hWnd,nChildId),bEnable);
  144. }
  145. }
  146. inline UINT GetDisplayColorDepth()
  147. {
  148. UINT nColorDepth = 0;
  149. HDC hDC = GetDC( NULL );
  150. if (hDC)
  151. {
  152. nColorDepth = GetDeviceCaps( hDC, BITSPIXEL ) * GetDeviceCaps( hDC, PLANES );
  153. ReleaseDC( NULL, hDC );
  154. }
  155. return nColorDepth;
  156. }
  157. LONG Align( LONG n , LONG m );
  158. LONG StringToLong( LPCTSTR pszStr );
  159. SIZE MapDialogSize( HWND hwnd, const SIZE &size );
  160. LONG GetBmiSize(PBITMAPINFO pbmi);
  161. bool MsgWaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds = INFINITE );
  162. void CenterWindow( HWND hWnd, HWND hWndParent=NULL );
  163. bool FlipImage( PBYTE pBits, LONG nWidth, LONG nHeight, LONG nBitDepth );
  164. HRESULT InstallInfFromResource( HINSTANCE hInstance, LPCSTR pszSectionName );
  165. HRESULT DeleteItemAndChildren (IWiaItem *pItem);
  166. LONG ItemAndChildrenCount(IWiaItem *pItem );
  167. HRESULT WriteDIBToFile( HBITMAP hDib, HANDLE hFile );
  168. HFONT CreateFontWithPointSizeFromWindow( HWND hWnd, int nPointSize, bool bBold, bool bItalic );
  169. HFONT ChangeFontFromWindow( HWND hWnd, int nPointSizeDelta );
  170. HFONT GetFontFromWindow( HWND hWnd );
  171. HRESULT SystemPropertySheet( HINSTANCE hInstance, HWND hwndParent, IWiaItem *pWiaItem, LPCTSTR pszCaption );
  172. int FindLowestNumberedFile( LPCTSTR pszFileAndPathnameMask, int nCount=1, int nMax=65545 );
  173. int FindLowestNumberedFile( LPCTSTR pszFileAndPathnameMaskPrefix, LPCTSTR pszFormatString, LPCTSTR pszFileAndPathnameMaskSuffix, int nCount=1, int nMax=65535 );
  174. HRESULT GetDeviceTypeFromId( LPCWSTR pwszDeviceId, LONG *pnDeviceType );
  175. HRESULT GetDeviceInfoFromId( LPCWSTR pwszDeviceId, IWiaPropertyStorage **ppWiaPropertyStorage );
  176. HRESULT GetDefaultEventHandler (IWiaItem *pItem, const GUID &guidEvent, WIA_EVENT_HANDLER *pwehHandler);
  177. CSimpleString FitTextInStaticWithEllipsis( LPCTSTR pszString, HWND hWndStatic, UINT nDrawTextStyle );
  178. CSimpleString TruncateTextToFitInRect( HWND hFontWnd, LPCTSTR pszString, RECT rectTarget, UINT nDrawTextFormat );
  179. SIZE GetTextExtentFromWindow( HWND hFontWnd, LPCTSTR pszString );
  180. bool GetIconSize( HICON hIcon, SIZE &sizeIcon );
  181. HBITMAP CreateIconThumbnail( HWND hWnd, int nWidth, int nHeight, HICON hIcon, LPCTSTR pszText );
  182. HBITMAP CreateIconThumbnail( HWND hWnd, int nWidth, int nHeight, HINSTANCE hIconInstance, const CResId &resIconId, LPCTSTR pszText );
  183. HRESULT MoveOrCopyFile( LPCTSTR pszSrc, LPCTSTR pszTgt );
  184. CSimpleString CreateTempFileName( UINT nId = 0 );
  185. HRESULT StampItemTimeOnFile( IWiaItem *pWiaItem, LPCTSTR pszFilename );
  186. HRESULT SaveWiaItemAudio( IWiaItem *pWiaItem, LPCTSTR pszBaseFilename, CSimpleString &strAudioFilename );
  187. bool IsDeviceCommandSupported( IWiaItem *pWiaItem, const GUID &guidCommand );
  188. void PreparePropertyPageForFusion( PROPSHEETPAGE *pPropSheetPage );
  189. bool CanWiaImageBeSafelyRotated( const GUID &guidFormat, LONG nImageWidth, LONG nImageHeight );
  190. HRESULT ExploreWiaDevice( LPCWSTR pszDeviceId );
  191. BOOL ModifyComboBoxDropWidth( HWND hwndControl );
  192. void SubclassComboBoxEx( HWND hWnd );
  193. HRESULT IssueWiaCancelIO( IUnknown *pUnknown );
  194. HRESULT VerifyScannerProperties( IUnknown *pUnknown );
  195. CSimpleString GetErrorTextFromHResult( HRESULT hr );
  196. //
  197. // End namespace WiaUiUtil
  198. //
  199. }
  200. #endif // __MISCUTIL_H_INCLUDED