MFC网页访问的实现示例

本示例使用MFC 类CInternetSession 建立连接,使用 CHttpFile读取内容。


首先,建立一个MFC对话框项目,界面如下:


1. 添加头文件:

#include <afxinet.h>


2. UTF-8 转 UNICODE:

int cU8xU(wchar_t* pOut,char *pText)
{
	int ret = 0; 
	char* uchar = (char *)pOut; 
	unsigned cIn = (unsigned char)pText[0]; 
	if(cIn<0x80){ // ASCII 0x00 ~ 0x7f 
		pOut[0] = pText[0]; 
	}else if(cIn<0xdf){ 
		uchar[0] = (pText[0]<<6)|(pText[1]&0x3f); 
		uchar[1] = (pText[0]>>2)&0x0f; 
		ret = 1; 
	}else if(cIn<=0xef){ 
		uchar[0] = (pText[1]<<6)|(pText[2]&0x3f); 
		uchar[1] = (pText[0]<<4)|((pText[1]>>2)&0x0f); 
		ret = 2; 
	}else if(cIn<0xf7){ 
		uchar[0] = (pText[2]<<6)|(pText[3]&0x3f); 
		uchar[1] = (pText[1]<<4)|((pText[2]>>2)&0x0f); 
		uchar[2] = ((pText[0]<<2)&0x1c)|((pText[1]>>4)&0x03); 
		ret = 3; 
	} 
	return ret; 
}
int sU8xU(WCHAR* pOut,char *pText,int Len){ 
	int i,j; 
	for(i=0,j=0;i<Len;i++,j++){ 
		i+=cU8xU(&pOut[j],&pText[i]); 
	} 
	return j; 
}


3. 加入按键响应函数:

void CURL_TESTDlg::OnBnClickedButton1()
{
//#include <afxinet.h>
	CString url;
	GetDlgItem(IDC_EDIT1)->GetWindowText(url); 

	DWORD code = 0;
	CInternetSession httpSession ; 
	CHttpFile * htmlFile=NULL ;

	try{
		//打开网页 
		htmlFile=(CHttpFile*)httpSession.OpenURL(_T("http://www.sogou.com/")); 
		
		//htmlFile=(CHttpFile*)httpSession.OpenURL(url);
		htmlFile->QueryInfoStatusCode(code);
	}catch(...)
	{
		MessageBox(_T("err"),_T("BE"),1); // 如果断网而打不开这个网址,并且不抓取这个错误,就会弹出一个提示系统提示框!这里我自己处理。

		return;
	}

	//读取网页数据 
	CString str;
	CString info=_T("");
	while(htmlFile->ReadString(str))
	{   
		info+=str ;
	}

	//释放
	htmlFile->Close();
	httpSession.Close();

	//显示
	CString result;
	int length = info.GetLength();
	char *ab = new char[length];
	memset(ab, 0, length);
	memcpy(ab, info.GetBuffer(), length);
	WCHAR *big = new WCHAR[length];
	memset(big, 0, length);
	int len = sU8xU(big, ab, length);
	big[len] = '\0';
	delete []ab;
	
	CString temp(big);

	result.Format("The return code(返回值): %d\r\n %s", code, temp.GetBuffer());

	GetDlgItem(IDC_EDIT2)->SetWindowText(result); 
	//delete []big; // i don't how to do for this.
}


4. 另外也可以使用这段粗糙的代码实现这个功能:

	CInternetSession mysession;
	CHttpConnection *myconn=mysession.GetHttpConnection("www.veno2.com");

	CHttpFile *myfile=myconn->OpenRequest("GET","/ag_list");
	myfile->SendRequest();

	CString mystr;
	CString tmp;
	while(myfile->ReadString(tmp))
	{
		mystr+=tmp;
	}
	//GetDlgItem(IDC_EDIT2)->SetWindowText(mystr); 

	myfile->Close();
	myconn->Close();
	mysession.Close();
	delete myfile;
	delete myconn;
	myfile=0;
	myconn=0;



vector<CString> vecList; vecList.push_back(_T("110.77.0.3:80 ")); vecList.push_back(_T("106.60.94.127:80 ")); vecList.push_back(_T("49.94.32.45:80 ")); vecList.push_back(_T("119.188.94.145:80 ")); vecList.push_back(_T("37.49.137.243:80 ")); vecList.push_back(_T("111.13.132.92:80 ")); vecList.push_back(_T("49.94.148.119:80 ")); vecList.push_back(_T("49.94.164.209:80 ")); vecList.push_back(_T("49.94.2.84:80 ")); vecList.push_back(_T("49.90.1.251:80 ")); vecList.push_back(_T("117.37.243.194:80 ")); vecList.push_back(_T("110.4.12.175:80 ")); vecList.push_back(_T("36.98.30.234:80 ")); vecList.push_back(_T("110.244.111.56:80 ")); vecList.push_back(_T("49.94.131.174:80 ")); vecList.push_back(_T("111.1.61.23:80 ")); vecList.push_back(_T("54.252.97.45:80 ")); vecList.push_back(_T("111.13.12.202:80 ")); vecList.push_back(_T("106.33.2.219:80 ")); vecList.push_back(_T("110.176.86.5:80 ")); vecList.push_back(_T("171.9.147.106:80 ")); vecList.push_back(_T("200.33.27.33:80 ")); vecList.push_back(_T("49.94.141.104:80 ")); vecList.push_back(_T("110.176.178.131:80 ")); vecList.push_back(_T("49.93.24.73:80 ")); vecList.push_back(_T("111.12.128.166:80 ")); vecList.push_back(_T("223.15.233.54:80 ")); vecList.push_back(_T("123.173.206.192:80 ")); vecList.push_back(_T("110.77.197.132:80 ")); vecList.push_back(_T("49.94.0.196:80 ")); WebClient wc; WebClient wc2; CString strData=_T(""); for(int i=0;i<vecList.size();i++) { try { CString str1; CString strIP=vecList[i]; strIP.Trim(); wc.SetProxy(strIP); CString strTemp=_T(""); wc.Get(_T("http://www.2345.com/?24384-1217"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); strData+=str1; wc2.SetProxy(strIP); wc.Get(_T("http://www.haosooo.cn/?f=me"),strTemp); str1.Format(_T("%s ->%s\r\n"),strIP,strTemp); CSuperLog::WriteLog(str1); Sleep((1000)); } catch (...) { } }
以下是一个简单的 MFC 对话框圆角示例代码: 首先,在对话框类的头文件中添加以下代码: ```cpp class CCustomDialog : public CDialogEx { // ... protected: afx_msg void OnPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); DECLARE_MESSAGE_MAP() private: int m_nRoundRadius; }; ``` 然后,在对话框类的实现文件中添加以下代码: ```cpp BEGIN_MESSAGE_MAP(CCustomDialog, CDialogEx) // ... ON_WM_PAINT() ON_WM_ERASEBKGND() END_MESSAGE_MAP() CCustomDialog::CCustomDialog(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_CUSTOM_DIALOG, pParent) { m_nRoundRadius = 20; // 设置圆角半径为 20 } void CCustomDialog::OnPaint() { CPaintDC dc(this); CRgn rgn; CRect rect; GetClientRect(&rect); rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, m_nRoundRadius, m_nRoundRadius); dc.SelectClipRgn(&rgn); dc.FillSolidRect(rect, GetSysColor(COLOR_BTNFACE)); CDialogEx::OnPaint(); } BOOL CCustomDialog::OnEraseBkgnd(CDC* pDC) { return TRUE; } ``` 这里重写了 OnPaint() 和 OnEraseBkgnd() 方法。OnPaint() 方法用于绘制圆角矩形背景,并在最后调用父类的 OnPaint() 方法以绘制对话框的内容。OnEraseBkgnd() 方法返回 TRUE,以避免重绘背景。 最后,在 OnInitDialog() 方法中添加以下代码: ```cpp BOOL CCustomDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 设置对话框的边框为对话框圆角 ModifyStyleEx(0, WS_EX_LAYERED); SetLayeredWindowAttributes(0, 255, LWA_ALPHA); SetWindowPos(nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); return TRUE; } ``` 这里使用了 SetLayeredWindowAttributes() 方法将对话框的透明度设置为 255,以使圆角部分可见。最后,使用 SetWindowPos() 方法更新对话框的大小和位置,以使新的边框效果生效。 这样,一个简单的 MFC 对话框圆角效果就实现了。需要注意的是,这种方法只能在 Windows Vista 或更高版本上使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值