天天看點

VC技巧

Ascii和Unicode的互轉

  1. //-------------------------------------------------------------------------------------
  2. //Description:
  3. // This function maps a character string to a wide-character (Unicode) string
  4. //
  5. //Parameters:
  6. // lpcszStr: [in] Pointer to the character string to be converted 
  7. // lpwszStr: [out] Pointer to a buffer that receives the translated string. 
  8. // dwSize: [in] Size of the buffer
  9. //
  10. //Return Values:
  11. // TRUE: Succeed
  12. // FALSE: Failed
  13. // 
  14. //Example:
  15. // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  16. //---------------------------------------------------------------------------------------
  17. BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  18. {
  19.     // Get the required size of the buffer that receives the Unicode 
  20.     // string. 
  21.     DWORD dwMinSize;
  22.     dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
  23.     if(dwSize < dwMinSize)
  24.     {
  25.         return FALSE;
  26.     }
  27.     // Convert headers from ASCII to Unicode.
  28.     MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); 
  29.     return TRUE;
  30. }
  31. //-------------------------------------------------------------------------------------
  32. //Description:
  33. // This function maps a wide-character string to a new character string
  34. //
  35. //Parameters:
  36. // lpcwszStr: [in] Pointer to the character string to be converted 
  37. // lpszStr: [out] Pointer to a buffer that receives the translated string. 
  38. // dwSize: [in] Size of the buffer
  39. //
  40. //Return Values:
  41. // TRUE: Succeed
  42. // FALSE: Failed
  43. // 
  44. //Example:
  45. // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  46. //---------------------------------------------------------------------------------------
  47. BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  48. {
  49.     DWORD dwMinSize;
  50.     dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
  51.     if(dwSize < dwMinSize)
  52.     {
  53.         return FALSE;
  54.     }
  55.     WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
  56.     return TRUE;
  57. }

//讀取程式設定

SetRegistryKey(TEXT("本地程式"));//PsMonitor

OpCom::dwWaitTime = GetProfileInt(TEXT("Setting"), TEXT("參數"), 預設值);

OpCom::sExePath = GetProfileString(TEXT("Setting"), TEXT("預設值"));

//隻運作一個執行個體

HANDLE hMutex;

hMutex=CreateMutex(NULL,TRUE,TEXT("MutexName"));    //MutexName使用你自己的名字

if(hMutex)

{

 if(ERROR_ALREADY_EXISTS == GetLastError())

 {

  return FALSE;

 }

}

//啟動隐藏主視窗和工作列

ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); //隐藏工作列

WINDOWPLACEMENT wp;

wp.length=sizeof(WINDOWPLACEMENT);

//GetWindowPlacement(&wp);

wp.flags=WPF_RESTORETOMAXIMIZED;

wp.showCmd=SW_HIDE;

SetWindowPlacement(&wp);

//設定自動運作

#define EP_REG_AUTORUN   TEXT("SOFTWARE//Microsoft//Windows//CurrentVersion//Run")

#define EP_REG_AUTORUN_NAME  TEXT("運作項名字")

TCHAR sPath[MAX_PATH];

GetModuleFileName(NULL, sPath, sizeof(sPath));

OpCom::SetRegValue(HKEY_LOCAL_MACHINE, EP_REG_AUTORUN, EP_REG_AUTORUN_NAME, sPath);

//查詢系統資料庫string

BOOL OpCom::QueryRegValue(HKEY hKeyParent,

        LPCTSTR sRegKey,

        LPCTSTR sKeyName,

        LPTSTR sValue,

        ULONG iMaxLength,

        LPCTSTR sDefaultValue)

{

 CRegKey regKey;

 if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))

 {

  if(lstrcpyn(sValue, sDefaultValue, static_cast<int>(iMaxLength)))

   return TRUE;

  return FALSE;

 }

 ULONG iMaxLength2 = iMaxLength;

 if(ERROR_SUCCESS != regKey.QueryStringValue(sKeyName,sValue,&iMaxLength2))

 {

  regKey.Close();

  if (sDefaultValue)

  {

   if(lstrcpyn(sValue, sDefaultValue, static_cast<int>(iMaxLength)))

    return TRUE;

  }

  else

  {

   sValue[0] = TEXT('/0');

  }

  return FALSE;

 }

 regKey.Close();

 return TRUE;

}

//查詢系統資料庫DWORD

BOOL OpCom::QueryRegDwordValue(HKEY hKeyParent,

          LPCTSTR sRegKey,

          LPCTSTR sKeyName,

          DWORD &dwValue,

          DWORD dwDefaultValue)

{

 CRegKey regKey;

 if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))

 {

  dwValue = dwDefaultValue;

  return FALSE;

 }

 if(ERROR_SUCCESS != regKey.QueryDWORDValue(sKeyName,dwValue))

 {

  dwValue = dwDefaultValue;

  regKey.Close();

  return FALSE;

 }

 regKey.Close();

 return TRUE;

}

//修改系統資料庫string

BOOL OpCom::SetRegValue(HKEY hKeyParent,

      LPCTSTR sRegKey,

      LPCTSTR sKeyName,

      LPCTSTR sValue)

{

 CRegKey regKey;

 if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))

 {

  if(ERROR_SUCCESS != regKey.Create(hKeyParent,sRegKey))

   return FALSE;

 }

 if(ERROR_SUCCESS != regKey.SetStringValue(sKeyName,sValue))

 {

  regKey.Close();

  return FALSE;

 }

 regKey.Close();

 return TRUE;

}

//修改系統資料庫DWORD

BOOL OpCom::SetRegDwordValue(HKEY hKeyParent,

        LPCTSTR sRegKey,

        LPCTSTR sKeyName,

        DWORD dwValue)

{

 CRegKey regKey;

 if(ERROR_SUCCESS != regKey.Open(hKeyParent,sRegKey))

 {

  if(ERROR_SUCCESS != regKey.Create(hKeyParent,sRegKey))

   return FALSE;

 }

 if(ERROR_SUCCESS != regKey.SetDWORDValue(sKeyName,dwValue))

 {

  regKey.Close();

  return FALSE;

 }

 regKey.Close();

 return TRUE;

}

//檔案是否存在

BOOL OpCom::FileExist(LPCTSTR sFilePath)

{

 //PathFileExists( sFilePath )

 WIN32_FIND_DATA w32fd;

 HANDLE hFile=FindFirstFile(sFilePath,&w32fd);

 if(hFile!=INVALID_HANDLE_VALUE)

 {

  FindClose(hFile);

  return TRUE;

 }

 return FALSE;

}

//建立一個非模态對話框

//添加一個對話框資源,并添加對話框類class CProcDlg:CDialog;

//頭檔案

CProcDlg *m_pProcDlg;

//CPP檔案

//初始化 m_pProcDlg(NULL)

//顯示對話框

if(!m_pProcDlg)

{

 m_pProcDlg = new CProcDlg(this);

 m_pProcDlg->Create(CProcDlg::IDD, this);

}

m_pProcDlg->ShowWindow(SW_SHOW);

//關閉對話框

if(m_pProcDlg)

{

 //m_pProcDlg->CloseWindow();

 m_pProcDlg->DestroyWindow();

 delete m_pProcDlg;

 m_pProcDlg=NULL;

}

//當主視窗隐藏時,顯示子對話框的方法

CProcDlg::CProcDlg(CPsMonitorDlg* pParent )

 : CDialog(CProcDlg::IDD, (CWnd *)NULL) //注意:這行的pParent被修改為NULL了

//設定視窗置頂

::SetWindowPos(this->m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);

//設定視窗螢幕居中

CenterWindow(GetDesktopWindow());

//時間差類 CTimeSpan

CTimeSpan m_tsYysj

m_tsYysj.Format("%D天 %H:%M:%S");

//增加1秒

CTimeSpan ts(1); // 1 seconds

m_tsYysj += ts;