方法积累

来源:互联网 发布:欧阳坤 知乎 编辑:程序博客网 时间:2024/05/16 01:04

//////////////////////////////////////////////////////////////////////////////////////////////////

///启动文件/////////////////////////////////////////////////////////////////////////////启动文件

//获得文件路径

GetCurrentDirectory(MAX_PATH, szFilePath);
 strcat(szFilePath, _T("//"));
 strcat(szFilePath, m_szFileName);

 

//申明结构体

 SHELLEXECUTEINFO ShExecInfo = {0};
 ShExecInfo.cbSize           = sizeof(SHELLEXECUTEINFO);
 ShExecInfo.fMask            = SEE_MASK_NOCLOSEPROCESS;
 ShExecInfo.hwnd             = NULL;
 ShExecInfo.lpVerb           = NULL;
 ShExecInfo.lpFile           =  szFilePath;   //路径
 ShExecInfo.lpParameters     = ""; 
 ShExecInfo.lpDirectory      = NULL;
 ShExecInfo.nShow            = SW_SHOW;
 ShExecInfo.hInstApp         = NULL; 

//启动

 ShellExecuteEx(&ShExecInfo);

 

 

/////////////////////////////////////////////////////////////////////////////////////////////////////

///下载文件cstrUrl是文件地址url//////////////////////////////////////////////////下载文件/////

BOOL CRequestFile::DownloadFile(CString cstrUrl)
{
 //解析文件名
 char *pszUrl = NULL;
 char *pszFileName = NULL;
 string strUrl = cstrUrl.GetBuffer();
 //pszUrl = strUrl.c_str();
 //memset(pszUrl, 0, MAX_PATH * 2);
 pszUrl = new char[MAX_PATH];
 sprintf_s(pszUrl, MAX_PATH, "%s", strUrl.c_str());
 string::size_type sizeType;
 sizeType = strUrl.find_last_of('/');
 pszFileName = &pszUrl[sizeType + 1];
 

 CInternetSession cis;
 CHttpConnection *pHttpCon;
 DWORD dwType;
 CString cstrFilePath;
 CString cstrServer;
 INTERNET_PORT INTERPort;

 

 AfxParseURL((LPCTSTR)cstrUrl, dwType
  , cstrServer, cstrFilePath, INTERPort);
 cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000);

 if (cstrServer.IsEmpty())
 {
  return FALSE;
 }

 

 pHttpCon = cis.GetHttpConnection(cstrServer, INTERPort);

 CHttpFile *pHttpFile = pHttpCon->OpenRequest(CHttpConnection::HTTP_VERB_GET, /
         cstrFilePath, /
         NULL, /
         1, /
         NULL, /
         NULL, /
         HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT
         | INTERNET_FLAG_NO_AUTO_REDIRECT);
 
 try
 {
  m_bSendRet = pHttpFile->SendRequest();
 }
 catch (CInternetException* pEx)
 {
  TCHAR tszError[MAX_PATH] = {0};
  pEx->GetErrorMessage(tszError, MAX_PATH);
  MessageBox(tszError, "Error");
 }

 

 if (pHttpFile)
 {
  CString cstrQuery;
  pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, cstrQuery);
  if (atol(cstrQuery) != DOWNLOAD_SUCCESS)
  {
   pHttpFile->Close();
   return FALSE;
  }

 

  int n;
  char szDownFileLocalPath[MAX_PATH] = {0};
  GetCurrentDirectory(MAX_PATH, szDownFileLocalPath);
  strcat(szDownFileLocalPath, _T("//"));
  strcat(szDownFileLocalPath, pszFileName);

  CStdioFile csf;
  if (!csf.Open(szDownFileLocalPath, CFile::modeCreate | CFile::modeWrite
   | CFile::typeBinary | CFile::shareDenyWrite))
  {
   pHttpFile->Close();
   return FALSE;
  }

 

  char szBuff[MAX_PATH * 2] = {0};

  DWORD dwRead = 0;
  DWORD SendRead = 0;
  while ((n = pHttpFile->Read(szBuff, sizeof(szBuff))) > 0)
  {
   dwRead += (DWORD)n;
   csf.Write(szBuff, n);
   memset(szBuff, 0, MAX_PATH * 2);
   SendRead = dwRead / MAX_PATH;
   ::SendMessage(m_hWndProDlg, WM_UPDATE, dwRead, 0);
  }
  MessageBox("completed");

  csf.Close();
 }
 else
 {
  return FALSE;
 }

 

 try
   {
  m_bSendRet = pHttpFile->SendRequest();
   }
 catch (CInternetException* pEx)
   {
  TCHAR tszError[MAX_PATH] = {0};
  pEx->GetErrorMessage(tszError, MAX_PATH);
  pHttpFile->Close();
  return FALSE;
   }

 

 pHttpFile->Close();

 delete []pszUrl;
 pszUrl = NULL;
 return TRUE;
}

 

 

////////////////////////////////////////////////////////////////////////////////////////

///使窗体半透明//////////////////////////////////////////////////////////////使窗体半透明/////

void CTestrDlg::SetLayeredWndAttr(int iSetTransprc, int iSetTrancAttr)
{
 //加入WS_EX_LAYERED扩展属性
 SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE,
  GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE)^0x80000);
 HINSTANCE hInst = LoadLibrary("User32.DLL");
 if (hInst)
  {
     typedef BOOL (WINAPI *MYFUNC)(HWND, COLORREF, BYTE, DWORD);
     MYFUNC fun = NULL;
     //取得SetLayeredWindowAttributes函数指针
     fun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
     if (fun)
     {
        fun(this->GetSafeHwnd(), 0, iSetTransprc, iSetTrancAttr);
        FreeLibrary(hInst);
     }
   }
}

 

 

////////////////////////////////////////////////////////////////////////////////////////

///如何弹出鼠标提示框///////////////////////////////////////////////////如何弹出鼠标提示框/////

原创粉丝点击