2017年11月26日 星期日

[ Windows | MFC ] show select Color dialog ( use CColorDialog )

void ClassName::showSelectColorDlgAndGetColor(COLORREF * color, BYTE *value_r, BYTE *value_g, BYTE *value_b)
{
     TRACE(_T("%s:%d\n"), __FUNCTIONW__, __LINE__);

     CColorDialog dlg(RGB(255, 0, 0), CC_FULLOPEN);
     if (dlg.DoModal() == IDOK)
     {
         *color = dlg.GetColor();
         TRACE(_T("%s:%d, 0x%06X\n"), __FUNCTIONW__, __LINE__, *color);

         if (value_r && value_g && value_b)
         {
              *value_r = GetRValue(*color);
              *value_g = GetGValue(*color);
              *value_b = GetBValue(*color);
              TRACE(_T("%s:%d, R:%d, G:%d, B:%d\n"), __FUNCTIONW__, __LINE__, *value_r, *value_g, *value_b);
         }
     }
}


[ Windows | MFC ] show select file diaolg ( use CFileDialog )

void ClassName::showSelectFileDlgAndGetPath(CString &returnPathName, CString &returnFileName, CString fileExtension, CString initPath)
{
     TRACE(_T("%s:%d, initPath:%s\n"), __FUNCTIONW__, __LINE__, initPath);

     CString csType;
     if (fileExtension.GetLength() > 0)
     {
         csType.Format(_T("%s file (*.%s)|*.%s|All (*.*)|*.*||"), fileExtension, fileExtension, fileExtension);
     }
     else
     {
         csType = _T("All (*.*)|*.*||");
     }

     CFileDialog fOpenDlg(
         TRUE, fileExtension, _T(""), OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
         csType, NULL);

     fOpenDlg.m_pOFN->lpstrTitle = _T("Select File Dialog");
     CString csTemp;
     csTemp.Format(_T("%s"), initPath);
     fOpenDlg.m_pOFN->lpstrInitialDir = csTemp;

     if (fOpenDlg.DoModal() == IDOK)
     {
         TRACE(_T("%s:%d, GetPathName:%s\n"), __FUNCTIONW__, __LINE__, fOpenDlg.GetPathName());
         TRACE(_T("%s:%d, GetFileName:%s\n"), __FUNCTIONW__, __LINE__, fOpenDlg.GetFileName());

         returnPathName = fOpenDlg.GetPathName();
         returnFileName = fOpenDlg.GetFileName();
     }
}


[ Windows | MFC ] run DOS command in MFC

void Utility::runDosCmdAndWait(CString command, int maxWaitMS, CString path, int isShow)
{
     CString final_cmd = _T(" /C ") + command;

     SHELLEXECUTEINFO ShExecInfo = { 0 };
     ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
     ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
     ShExecInfo.hwnd = NULL;
     ShExecInfo.lpVerb = NULL;
     ShExecInfo.lpFile = _T("cmd");
     ShExecInfo.lpParameters = final_cmd;
     ShExecInfo.lpDirectory = path;
     ShExecInfo.nShow = isShow;
     ShExecInfo.hInstApp = NULL;
     ShellExecuteEx(&ShExecInfo);
     WaitForSingleObject(ShExecInfo.hProcess, maxWaitMS);
}


[ Windows | MFC ] get system COM port list

void ClassName::getSystemComPortList(CStringArray &comPortList)
{
     comPortList.RemoveAll();

     HKEY hKey;
     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
         _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
         0,
         KEY_READ,
         &hKey) == ERROR_SUCCESS
         )
     {
         TCHAR    achKey[MAX_PATH];   // buffer for subkey name
         DWORD    cbName;                   // size of name string
         TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name
         DWORD    cchClassName = MAX_PATH// size of class string
         DWORD    cSubKeys = 0;               // number of subkeys
         DWORD    cbMaxSubKey;              // longest subkey size
         DWORD    cchMaxClass;              // longest class string
         DWORD    cValues;              // number of values for key
         DWORD    cchMaxValue;          // longest value name
         DWORD    cbMaxValueData;       // longest value data
         DWORD    cbSecurityDescriptor; // size of security descriptor
         FILETIME ftLastWriteTime;      // last write time

         DWORD i, retCode;

         TCHAR  achValue[MAX_PATH];
         DWORD cchValue = MAX_PATH;
         TCHAR  achValue2[MAX_PATH];
         DWORD cchValue2 = MAX_PATH;

         // Get the class name and the value count.
         retCode = RegQueryInfoKey(
              hKey,                    // key handle
              achClass,                // buffer for class name
              &cchClassName,           // size of class string
              NULL,                    // reserved
              &cSubKeys,               // number of subkeys
              &cbMaxSubKey,            // longest subkey size
              &cchMaxClass,            // longest class string
              &cValues,                // number of values for this key
              &cchMaxValue,            // longest value name
              &cbMaxValueData,         // longest value data
              &cbSecurityDescriptor,   // security descriptor
              &ftLastWriteTime);       // last write time

                                          // Enumerate the subkeys, until RegEnumKeyEx fails.
         if (cSubKeys)
         {
              TRACE(_T("Number of subkeys: %d\n"), cSubKeys);

              for (i = 0; i
              {
                  cbName = MAX_PATH;
                  retCode = RegEnumKeyEx(hKey, i,
                       achKey,
                       &cbName,
                       NULL,
                       NULL,
                       NULL,
                       &ftLastWriteTime);
                  if (retCode == ERROR_SUCCESS)
                  {
                       _tprintf(TEXT("(%d) %s\n"), i + 1, achKey);
                  }
              }
         }

         // Enumerate the key values.
         if (cValues)
         {
              //TRACE(_T("Number of values: %d\n"), cValues);
              for (i = 0, retCode = ERROR_SUCCESS; i
              {
                  cchValue = MAX_PATH;
                  achValue[0] = '\0';
                  cchValue2 = MAX_PATH;
                  achValue2[0] = '\0';
                  retCode = RegEnumValue(hKey, i,
                       achValue,
                       &cchValue,
                       NULL,
                       NULL,
                       (UCHAR*)achValue2,
                       &cchValue2);

                  if (retCode == ERROR_SUCCESS)
                  {
                       //TRACE(_T("i:%d, achValue:%s, achValue2:%s\n"), i, achValue, achValue2);
                       comPortList.Add(achValue2);
                  }
              }
         }
     }
     RegCloseKey(hKey);

}


[ Windows | MFC ] get full path of application's exe file

CString ClassName::getAppExePath()
{
     CString path;
     GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
     path.ReleaseBuffer();
     int nPos = path.ReverseFind('\\');
     return path.Left(nPos + 1);
}


[ Windows | MFC ] get application name

CString ClassName::getAppName()
{
     return   AfxGetApp()->m_pszExeName;
}


[ Windows | MFC ] get appliation version

CString ClassName::getAppVersion()
{
     int major, minor, build, revision;
     DWORD verBufferSize;
     char verBuffer[2048];
     CString version;

     TCHAR AppName[MAX_PATH];
     GetModuleFileName(NULL, AppName, MAX_PATH);

     //  Get the size of the version info block in the file
     verBufferSize = GetFileVersionInfoSize(AppName, NULL);
     if (verBufferSize > 0 && verBufferSize <= sizeof(verBuffer))
     {
         //  get the version block from the file
         if (TRUE == GetFileVersionInfo(AppName, NULL, verBufferSize, verBuffer))
         {
              UINT length;
              VS_FIXEDFILEINFO *verInfo = NULL;

              //  Query the version information for neutral language
              if (TRUE == VerQueryValue(
                  verBuffer,
                  _T("\\"),
                  reinterpret_cast<LPVOID*>(&verInfo),
                  &length))
              {
                  //  Pull the version values.
                  major = HIWORD(verInfo->dwProductVersionMS);
                  minor = LOWORD(verInfo->dwProductVersionMS);
                  build = HIWORD(verInfo->dwProductVersionLS);
                  revision = LOWORD(verInfo->dwProductVersionLS);
              }
         }
     }

     version.Format(_T("V %d.%d.%d.%d"), major, minor, build, revision);
     return version;
}