vc++ – 青春部落,流年似水 http://www.youthtribe.com 青春是一场远行,总记不起来时的路。 Wed, 26 Nov 2014 07:45:46 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.1.6 vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1410 http://www.youthtribe.com/archives/1410#respond Wed, 26 Nov 2014 07:45:46 +0000 http://www.youthtribe.com/?p=1410 OpenFile(文件路径); CFile file; int DownLength=0;//已经下载了的数据长度 if(目的目录下没有指定文件)//从头开始下载 { file.Open(目的文件路径,CFile::modeCreate|CFile::modeWrite); } else//断点续传 { file.Open(目]]> //我给个FTP下载的提示吧: CInternetSession session; CFtpConnection *pFtp=session.GetFtpConnection(FTP服务器地址,用户名,密码); CInternetFile *pFtpFile=pFtp->OpenFile(文件路径); CFile file; int DownLength=0;//已经下载了的数据长度 if(目的目录下没有指定文件)//从头开始下载 { file.Open(目的文件路径,CFile::modeCreate|CFile::modeWrite); } else//断点续传 { file.Open(目的文件路径,CFile::modeWrite); file.SeekToEnd(); DownLength=file.GetLength(); pFtpFile->Seek(DownLength,CFile::begin); } int SrcLength=pFtpFile->GetLength(); while(DownLength

源方地址:
http://bbs.csdn.net/topics/50249142

]]>
http://www.youthtribe.com/archives/1410/feed 0
vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1406 http://www.youthtribe.com/archives/1406#respond Sat, 22 Nov 2014 07:33:31 +0000 http://www.youthtribe.com/?p=1406 CreateDirectory("fffff"); CString strLocalFile; CFile fUpload("test.zip",CFile::modeRead|CFile::typeBinary); ]]> 直接上代码吧:

void CftptestDlg::OnBnClickedButtonGo()
{
	// TODO: 在此添加控件通知处理程序代码
	//开始上传
	CInternetSession CIS;
	CFtpConnection *ftp = CIS.GetFtpConnection("server","username","ps",21);
	//建立文件夹
	//ftp->CreateDirectory("fffff");
	CString strLocalFile;
	CFile fUpload("test.zip",CFile::modeRead|CFile::typeBinary);
	//要上传的文件的大小
	int nFileLength = fUpload.GetLength();
	fUpload.Read(strLocalFile.GetBuffer(nFileLength),nFileLength);
	strLocalFile.ReleaseBuffer(nFileLength);
	fUpload.Close();
	//远程ftp文件
	CInternetFile *CIF = ftp->OpenFile("test.zip",GENERIC_WRITE,FTP_TRANSFER_TYPE_BINARY,1);
	CIF->Write(strLocalFile.GetBuffer(nFileLength),nFileLength);
	CIF->Close();
}
]]>
http://www.youthtribe.com/archives/1406/feed 0
vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1377 http://www.youthtribe.com/archives/1377#respond Wed, 05 Nov 2014 08:36:44 +0000 http://www.youthtribe.com/?p=1377 vc如何链接mysql数据库,这篇文章就是一个简单的示例。

mysql的头文件,lib,dll文件,我是用的wamp集成包的,msyql版本是 mysql5.6.17 。之前也试过其他集成包,发现mysql的一些文件是没有的(不是精简掉了),所以建议下wamp吧。wamp sourceforge 官网:http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.1/

  • 找到mysql文件夹下的【include】文件夹,放到我们的vc工程目录下
  • 设置vc项目属性,添加包含。如下图
  •  在用到msyql的文件中,加下以下代码
  • #include “mysql.h”

  • 声明一个mysql变量
  • MYSQL mysql;

  • 新建一个数据库,当然还有对应的用户名。我的一律是 “s”。
  • 我在对话框初始化函数中加入以下代码
  • 	//开始链接数据库
    	mysql_init(&mysql);
    	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf-8"); 
    
    	if(!mysql_real_connect(&mysql,"localhost","user","password","user_db",3306,NULL,0))
    	{
    		AfxMessageBox("数据库连接失败");
    	}
    	else
    	{
    		//MessageBox("链接数据库成功!");
    	}
    

    草,不写了,难产了。。。因为现在我用的这个wamp也是不完整的,我用了之前 的才配配置好。狗日的。。。

    ]]>
    http://www.youthtribe.com/archives/1377/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1373 http://www.youthtribe.com/archives/1373#respond Wed, 15 Oct 2014 09:23:18 +0000 http://www.youthtribe.com/?p=1373 因为我对这个cmathresult模版类不懂,所以我的方法就是,每找到一个匹配项,那就把他替换为空,这样循环下去,直到得不到正则匹配项为止。

    //just for google
    bool CURLCollectorDlg::GetMatchCString2(CString strSearch, CString strBegin, CString strEnd ,CStringArray &arr)
    {
    	CString strReturn;
    	//MessageBox("str" + strSearch);
    	std::string str(strSearch.GetBuffer());
    	//正则规则
    	std::string strbegin,strend,strreg;
    	strbegin = strBegin.GetBuffer();
    	strend = strEnd.GetBuffer();
    	strreg = "(.*?)";
    	strreg = strbegin + strreg + strend;
    	std::regex reg(strreg);
    	//flag
    	std::regex_constants::match_flag_type f = std::regex_constants::match_any;
    	//存放匹配出来字符串
    	std::match_results mr;
    
    	if (std::regex_search(str,mr,reg,f))
    	{
    		AddStatus("匹配成功");
    	} 
    	else
    	{
    		AddStatus("匹配失败");
    		return false;
    	}
            //这个while循环是核心代码
    	while(std::regex_search(str,mr,reg,f))
    	{
    		std::string s(mr.str());
    		strReturn = s.c_str();
    		//开始替换
    		CString newstr;
    		newstr = str.c_str();
    		newstr.Replace(strReturn,"");
    		str = newstr.GetBuffer(0);
    		//
    		strReturn.Replace(strBegin,"");
    		strReturn.Replace(strEnd,"");
    		strReturn.Replace("","");
    		strReturn.Replace("","");
    		//AddOneURL("匹配到的网址:" + strReturn);
    
    
    		//str = str.replace(s,"",);
    		arr.Add(strReturn);
    
    
    
    	}
    	return false;
    	
    }
    
    ]]>
    http://www.youthtribe.com/archives/1373/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1350 http://www.youthtribe.com/archives/1350#respond Tue, 16 Sep 2014 14:20:43 +0000 http://www.youthtribe.com/?p=1350 vc++[mfc]递归删除文件夹及其里边的文件

    void DeleteDirectory(LPCTSTR path)
    {
    	CFileFind findfile;
    	CString str;
    	str=path;
    	if(str.Right(1)!="\\")
    		str.Format("%s\\*.*",path);
    	else
    		str.Format("%s*.*",path);
    	BOOL find=findfile.FindFile(str);
    
    	while(find)
    	{
    		find=findfile.FindNextFile();
    		if(findfile.IsDirectory())
    		{
    			if(!findfile.IsDots())
    			{
    
    				DeleteDirectory(findfile.GetFilePath());
    			}
    		}
    		else
    		{
    			DeleteFile(findfile.GetFilePath());
    		}
    	}
    	findfile.Close();
    	if(!RemoveDirectory(path))
    	{
    		DWORD ret=::GetLastError();
    		CString strerr;
    		strerr.Format("%d",ret);
    		MessageBox(strerr,"错误代码",MB_OK);
    	}
    }
    
    ]]>
    http://www.youthtribe.com/archives/1350/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1342 http://www.youthtribe.com/archives/1342#respond Mon, 08 Sep 2014 04:03:49 +0000 http://www.youthtribe.com/?p=1342 1.添加一个右键响应消息

    	//右键就显示一个菜单;包括:1、item1 2、item2 and so on....
    	CMenu Menu;
    	//创建一个弹出式菜单
    	Menu.CreatePopupMenu();
    	//添加菜单项
    	
    	Menu.AppendMenu(MF_STRING, ID_POP_MENU_PICS_FIND_0, "◆ 单独阅此张答题卡");
    
    	//根据当前光标位置显示菜单
    	CPoint Pos;
    	GetCursorPos(&Pos); 
    	Menu.TrackPopupMenu(TPM_RIGHTBUTTON, Pos.x, Pos.y, this);
    	Menu.DestroyMenu();
    

    2.在资源那里添加对应的菜单
    3.添加meNuc响应消息函数
    h头文件,函数定义:

    afx_msg void OnFunction1();
    

    cpp文件:

    ON_COMMAND(ID_POP_MENU_PICS_FIND_0, OnFunction1)
    

    ok,that’s all.

    ]]>
    http://www.youthtribe.com/archives/1342/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1332 http://www.youthtribe.com/archives/1332#respond Sun, 31 Aug 2014 01:00:57 +0000 http://www.youthtribe.com/?p=1332 vc++-mfc-实现网站发布文章ping百度,加快文章收录

    	// http 链接变量
    	CHttpConnection * m_http;
    
    			CInternetSession CIS;
    			CString strPingBaidu;
    			strPingBaidu = "ping.baidu.com";
    			CString strPingPath;
    			strPingPath = "ping/RPC2";
    			pdlg->m_http = CIS.GetHttpConnection(strPingBaidu) ;
    
    			CHttpFile * pHttpFile = NULL;
    			
    			/*
    			CHttpFile* OpenRequest(
    				LPCTSTR pstrVerb,//get or post .A pointer to a string containing the verb to use in the request. If NULL, "GET" is used.
    				LPCTSTR pstrObjectName,//提交站点内地址
    				LPCTSTR pstrReferer = NULL,//请求参数字符串
    				DWORD_PTR dwContext = 1,//返回状态码
    				LPCTSTR* ppstrAcceptTypes = NULL,//返回的字符串
    				LPCTSTR pstrVersion = NULL,//A pointer to a string defining the HTTP version. If NULL, "HTTP/1.0" is used.
    				DWORD dwFlags = INTERNET_FLAG_EXISTING_CONNECT 
    				);
    				*/
    			pHttpFile = pdlg->m_http->OpenRequest(CHttpConnection::HTTP_VERB_POST,
    				strPingPath,//"test/test.php",
    				NULL,
    				1,
    				NULL,
    				NULL,
    				INTERNET_FLAG_EXISTING_CONNECT);
    
    			//发送header
    
    			//pHttpFile->AddRequestHeaders("Content-Type:application/x-www-form-urlencoded"); 
    			//pHttpFile->AddRequestHeaders("Accept:*/*");
    			/*
    			POST /ping/RPC2 HTTP/1.0
    			User-Agent: request
    			Host: ping.baidu.com
    			Content-Type: text/xml
    			Content-Length: 511
    			*/
    			pHttpFile->AddRequestHeaders("POST /ping/RPC2 HTTP/1.0");
    			pHttpFile->AddRequestHeaders("User-Agent: request");
    			pHttpFile->AddRequestHeaders("Host: ping.baidu.com");
    			pHttpFile->AddRequestHeaders("Content-Type: text/xml");
    
    			//发送请求
    			
    			//这个是post的
    			CString strBlogName,strBlogIndexURL,strNewArticleURL,strRssURL;
    			strBlogName =  pdlg->m_CtrlRequestURLInfo.GetItemText(j,1);
    			strBlogIndexURL = pdlg->m_CtrlRequestURLInfo.GetItemText(j,1);
    			strNewArticleURL = strURL;
    			strRssURL = "";
    
    			CString strPost;
    			strPost = GetPingCString(strBlogName,strBlogIndexURL,strNewArticleURL,strRssURL);
    			//strPost = pdlg->GetPingCString("d","d","d","d");	//post的数据
    			//strPost = "sdafdfsadf";	//post的数据
    			//pHttpFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPost,strPost.GetLength()); 	
    			
    			//if (pHttpFile->SendRequest(NULL,0,NULL,0))
    			if(pHttpFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPost,strPost.GetLength()))
    			{
    				//成功
    				DWORD   retcode; 
    				pHttpFile->QueryInfoStatusCode(retcode);
    				CString strReturn;
    				if (retcode == 200)
    				{
    					//一行一行的读出来
    					CString strLine;
    					while(pHttpFile->ReadString(strLine))
    					{
    						strReturn = strReturn + strLine + "\n";
    					
    					}
    					pdlg->AddStatus(strReturn);
    					//显示出结果
    					if (strReturn.Replace("0","ok")==1)
    					{
    						//能替换一个,说明返回的是0,也就是说提交成功了
    						pdlg->m_CtrlBaiduPing.SetItemText(pdlg->m_CtrlBaiduPing.GetItemCount()-1,2,"是");
    						pdlg->AddStatus("Ping百度成功");
    
    					}
    					else if (strReturn.Replace("1","ok")==1)
    					{
    						pdlg->AddStatus("Ping百度失败");
    					}
    					//pdlg->AddStatus(strReturn);
    					//AfxMessageBox(strReturn);
    				}
    
    			}
    			else
    			{
    				//失败
    				pdlg->AddStatus("send request 失败");		
    			}
    

    自己自定义的一个函数

    CString GetPingCString(CString blogname, CString blogindexurl, CString newarticleurl, CString rssurl)
    {
    	/*
    	POST /ping/RPC2 HTTP/1.0
    	User-Agent: request
    	Host: ping.baidu.com
    	Content-Type: text/xml
    	Content-Length: 511
    	weblogUpdates.extendedPing百度的空间http://hi.baidu.com/baidu/http://baidu.com/blog/example.html
    		http://hi.baidu.com/baidu/rss
    	
    		
    		
    	*/
    	
     CString strReturn;
     strReturn = 
     strReturn = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
     strReturn = strReturn + "<methodCall><methodName>weblogUpdates.extendedPing</methodName><params><param><value><string>" + blogname + \
     "</string></value></param><param><value><string>" + blogindexurl + "</string></value></param><param><value><string>" + newarticleurl +\
     "</string></value></param><param><value><string>" + rssurl + "</string></value></param></params></methodCall>";
     return strReturn;
    }
    
    ]]>
    http://www.youthtribe.com/archives/1332/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1282 http://www.youthtribe.com/archives/1282#respond Thu, 07 Aug 2014 12:13:19 +0000 http://www.youthtribe.com/?p=1282 vc操作mysql的典型例子
    1.包含文件

    #include "mysql.h"
    

    2.初始化数据库

    	//开始链接数据库
    	//MYSQL mysql;//定义成全局的
    	mysql_init(&mysql);
    	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf-8"); 
    
    	if(!mysql_real_connect(&mysql,"localhost","user","password","user_db",3306,NULL,0))
    	{
    		AfxMessageBox("数据库连接失败");
    	}
    	else
    	{
    		//MessageBox("链接数据库成功!");
    	}
    

    3.插入数据

    	//添加到数据库
    	
    	CString strSQL = "insert into xbzq.group values(NULL,'test');";
    	const char * q =strSQL;
    	mysql_query(&mysql,q);
    

    4.读出数据

    	strsql = "select * from sentence_lib where id = " + m_nId;
    	mysql_query(&mysql,strsql);
    	res = mysql_store_result(&mysql);
    	MYSQL_ROW row=NULL;
    	row = mysql_fetch_row(res);
    	strpassage = strpassage+row[1];
    
    
    
    

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

    	//从数据库中随机取出一个句子
    	CString strsql="select * from sentence_lib";
    	const char * q =strsql;
    	mysql_query(&mysql,q);
    
    	MYSQL_RES *res = mysql_store_result(&mysql);
    
    	//mysql_use_result(MYSQL *mysql);
    
    	int nRow = res->row_count;//得到组文章用的句子库的数量
    	
    
    ]]>
    http://www.youthtribe.com/archives/1282/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1130 http://www.youthtribe.com/archives/1130#respond Thu, 08 May 2014 07:52:38 +0000 http://www.youthtribe.com/?p=1130 CFileDialog infofile(FALSE,NULL,"请输入要保存的模板名称",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR, "TPL文件(*.tpl)|*.TPL| |",AfxGetMainWnd()); infofile.m_ofn.lpstrInitialDir = "c:\\"; CString strTplFileName; if(infofile.DoModal()==IDOK) { strTplFileName=infofile.GetPathName()+".tpl"; }

    这样就能保存到c盘下了。

    ]]>
    http://www.youthtribe.com/archives/1130/feed 0
    vc++ – 青春部落,流年似水 http://www.youthtribe.com/archives/1114 http://www.youthtribe.com/archives/1114#respond Sat, 03 May 2014 14:54:41 +0000 http://www.youthtribe.com/?p=1114 m_hWnd,buffer,255); CString strWindowName = ""; strWindowName.Format("%s",buffer); MessageBox(strWindowName); delete [] buffer;]]> [mfc][vc++]

    如何得到对话框标题
    
    	LPTSTR buffer=   new   TCHAR[255];
    	::GetWindowText(GetActiveWindow()->m_hWnd,buffer,255);
    	CString strWindowName = "";
    	strWindowName.Format("%s",buffer);
    	MessageBox(strWindowName);
    	delete [] buffer;
    ]]>
    http://www.youthtribe.com/archives/1114/feed 0