PublicFunc.h
上传用户:shmeyee
上传日期:2015-06-28
资源大小:108k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. #pragma once
  2. #include <shlwapi.h> // for StrChrI()
  3. //==========================================================================
  4. // 常用操作宏
  5. //==========================================================================
  6. #define LENGTH(x) sizeof(x)/sizeof(x[0])
  7. #define MIN(x,y) (((DWORD)(x)<(DWORD)(y))?(x):(y))
  8. #define MAX(x,y) (((DWORD)(x)>(DWORD)(y))?(x):(y))
  9. #define ASSERT_ADDRESS(p,size) ASSERT((p)!=NULL && AfxIsValidAddress((p),(size),TRUE))
  10. #define GET_SAFE_STRING(str) ( (str)?(str):_T("") )
  11. #define GETFILESIZE(fad) (( (fad).nFileSizeHigh << sizeof(DWORD) ) | (fad).nFileSizeLow)
  12. // strlen 函数
  13. #ifdef UNICODE
  14. #define strlen_s wcslen
  15. #else
  16. #define strlen_s strlen
  17. #endif
  18. // 句柄是否有效
  19. #define HANDLE_IS_VALID(h) ( HANDLE(h) && HANDLE(h) != INVALID_HANDLE_VALUE )
  20. // 关闭句柄
  21. #define SAFE_CLOSE_HANDLE(h)
  22. {
  23. if ( HANDLE_IS_VALID ( h ) )
  24. {
  25. CloseHandle ( h );
  26. h = NULL;
  27. }
  28. }
  29. // 等待事件的 Sleep() 函数
  30. #define SLEEP_RETURN(x)
  31. {
  32. if ( ::WaitForSingleObject ( m_hEvtEndModule, x ) == WAIT_OBJECT_0 )
  33. return FALSE;
  34. }
  35. #define SLEEP_BREAK(x)
  36. {
  37. if ( ::WaitForSingleObject ( m_hEvtEndModule, x ) == WAIT_OBJECT_0 )
  38. break;
  39. }
  40. // 创建对话框实例模板函数
  41. template<class T>
  42. T* TGetInstance (
  43. T **pptDlg, // 保存对话框指针的指针
  44. UINT nIDTemplate, // 对话框资源 ID
  45. CWnd *pParentWnd=NULL, // 父窗口句柄
  46. BOOL bRecreateWhenExist=FALSE, // 对话框存在时是否重新创建,删除已经存在的
  47. BOOL bNotCreateWindow=FALSE, // 不创建窗口,仅仅 New 一个对象
  48. BYTE nShowImmediately=1 // 是否立即显示对话框。0 - 不显示;1 - 显示并激活获取焦点;2 - 仅仅显示,不获取焦点
  49. )
  50. {
  51. if ( !pptDlg ) return NULL;
  52. if ( *pptDlg )
  53. {
  54. if ( bRecreateWhenExist )
  55. {
  56. ::DeleteInstance ( (CWnd**)pptDlg );
  57. }
  58. else if ( ::IsWindow ( (*pptDlg)->m_hWnd ) )
  59. {
  60. ActiveWindowAndHoldFocus ( (*pptDlg)->m_hWnd );
  61. return (*pptDlg);
  62. }
  63. }
  64. if ( !(*pptDlg) )
  65. {
  66. *pptDlg = new T ( pParentWnd );
  67. }
  68. if ( !(*pptDlg) ) return NULL;
  69. if ( bNotCreateWindow ) return (*pptDlg);
  70. if ( !::IsWindow ( (*pptDlg)->GetSafeHwnd() ) )
  71. {
  72. if ( !(*pptDlg)->Create ( nIDTemplate, pParentWnd ) )
  73. {
  74. ::DeleteInstance ( (CWnd**)pptDlg );
  75. return NULL;
  76. }
  77. }
  78. if ( nShowImmediately != 0 )
  79. {
  80. (*pptDlg)->ShowWindow ( SW_SHOW );
  81. // (*pptDlg)->SetForegroundWindow ();
  82. if ( nShowImmediately == 1 )
  83. ActiveWindowAndHoldFocus ( (*pptDlg)->m_hWnd );
  84. }
  85. ASSERT ( (*pptDlg) && ::IsWindow ( (*pptDlg)->GetSafeHwnd() ) );
  86. return (*pptDlg);
  87. }
  88. class CPublicFunc
  89. {
  90. public:
  91. CPublicFunc(void);
  92. public:
  93. ~CPublicFunc(void);
  94. };
  95. CString hwFormatMessage ( DWORD dwErrorCode );
  96. BOOL WaitForThreadEnd ( HANDLE *phThread, DWORD dwWaitTime=5000 );
  97. CString Data2HexString ( char *data, int size, int nBytesPerLine=16 );
  98. int ConvStringToHexData(CString csOrgStr, BYTE *szOutBuffer, int nOutBufferSize);
  99. int GetMltiByteChar ( LPCTSTR lpszOrg, OUT char *buf, int size );
  100. int WriteDataToFile(LPCTSTR filename,void* data,long size,LPCTSTR mode, int nStartPos=-1 );
  101. int ReadDataFromFile(LPCTSTR filename,void* data,long size, int nStartPos=-1);
  102. void DrawCurveByDataBuffer ( CDC *pDC, CRect &rcClient, COLORREF clr, BYTE *data, int size, int nCellSize=1, int nStepX=0 );
  103. void DeleteInstance ( CWnd **ppWnd );
  104. void ActiveWindowAndHoldFocus ( HWND hWnd, BOOL bReShowWindow=TRUE );
  105. CSize GetWorkAreaSize ();
  106. void HandleSpanClick(
  107. CWnd *pOwnerWnd,
  108. UINT nEditCtrlID,
  109. NMHDR* pNMHDR,
  110. LRESULT* pResult,
  111. int nMaxValue,
  112. int nMinValue,
  113. int nInterval=1);
  114. int hwSnprintf ( LPTSTR buffer, int count, LPCTSTR *format, ... );
  115. CString StandardizationFileForPathName ( LPCTSTR lpszFileOrPathName, BOOL bIsFileName, char cReplaceChar=_T('_') );
  116. void StandardizationPathBuffer ( LPTSTR szPath, int nSize, TCHAR cFlagChar=_T('\') );
  117. CString StandardizationPathBuffer ( LPTSTR lpszPath, TCHAR cFlagChar=_T('\') );
  118. BOOL PartFileAndPathByFullPath (
  119. IN LPCTSTR lpszFilePath,
  120. OUT TCHAR *szOnlyFileName,
  121. int nFileNameSize,
  122. OUT TCHAR *szOnlyPath =NULL,
  123. int nPathSize=0
  124. );
  125. int GetProgramDir(LPTSTR lpProgram,DWORD dwSize);
  126. CString GetProgramDir();
  127. CString CommonFileDialog (
  128. LPCTSTR lpszIdentifier,
  129. BOOL bOpen,
  130. LPCTSTR lpszDefExt = NULL,
  131. LPCTSTR lpszFileName = NULL,
  132. CStringArray *pStrAry=NULL,
  133. CWnd* pParentWnd=NULL,
  134. LPCTSTR lpszTitle=NULL,
  135. LPCTSTR lpszInitialDir=NULL,
  136. LPCTSTR szFilter=NULL
  137. );
  138. int hwGetFileAttr ( LPCTSTR lpFileName, DWORD *p_dwCreateTime=NULL );
  139. DWORD GetHexDataByBitNum(int nBitNum, BOOL bReverse=FALSE);