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 /*=NULL*/)
 : 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;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章