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;

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值