您的位置:首页 > 其它

WTL 学习四 动态生成CListViewCtrl 并应用tinyxml加载远程Xml数据

2012-05-15 16:58 363 查看
本机站点:http://localhost:2000/Default6.aspx 代码内容

protected void Page_Load(object sender, EventArgs e)
{
string strSessionXml = "<?xml version=\"1.0\" encoding=\"gb2312\"?><root><item id=\"1\" name=\"风云\" address=\"192.168.1.101\" datetime=\"2012/5/15 16:24:35\" state=\"在线\"></item><item id=\"2\" name=\"雄霸\" address=\"192.168.1.102\" datetime=\"2012/5/15 16:24:35\" state=\"离线\"></item></root>";
Response.Write(strSessionXml);
}

生成 的Xml为

<?xml version="1.0" encoding="gb2312"?>
<root>
<item id="1" name="风云" address="192.168.1.101" datetime="2012/5/15 16:24:35" state="在线"></item>
<item id="2" name="雄霸" address="192.168.1.102" datetime="2012/5/15 16:24:35" state="离线"></item>
</root>


stdafx.h

#include <atlbase.h>
#include <atlstr.h>
#include <wtl/atlapp.h>

extern CAppModule _Module;
#define _WTL_NO_CSTRING   // 应用ATL CString
#include <atlwin.h>

#include <wtl/atlframe.h>
#include <wtl/atlctrls.h>
#include <wtl/atldlgs.h>
#include <wtl/atlmisc.h>
#include <tinyxml/tinyxml.h>  // 使用tinyxml
#pragma comment(lib, "winhttp.lib") // 使用winhttp


TestDialog.h

class TestDialog : public CDialogImpl<TestDialog>
{
public:
enum { IDD = IDD_TESTDLG };

BEGIN_MSG_MAP(TestDialog)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
COMMAND_ID_HANDLER(IDOK, OnOK)
END_MSG_MAP()

LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

CListViewCtrl  m_List;
};


TestDialog.cpp 很多字符转换,也不知道应用的对不对

,先来实现功能吧,后续慢慢优化

BOOL GetURL(LPWSTR szUrl,CString* strResult )
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;

HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;

BOOL bResults = FALSE;
BOOL bBodyTagStart = FALSE;
BOOL bBodyTagEnd = FALSE;

LPWSTR lpszUserAgent = L"WinHTTP URLMonitor Docklet/1.0";
LPWSTR lpszHostName = szUrl;
LPWSTR lpszUrlPath = L"/Default6.aspx";               //访问的页面
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
LPCWSTR pwszVerb = L"GET";

// Use WinHttpOpen to obtain a session handle.

hSession = WinHttpOpen( L"Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.0;   .NET   CLR   1.1.4322) ",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );

//hSession = WinHttpOpen(lpszUserAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
//                       WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

// Specify an HTTP server.

nPort=2000;               //端口号

if (hSession)
hConnect = WinHttpConnect(hSession, lpszHostName, nPort, 0);

// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, pwszVerb,
lpszUrlPath, //urlComp.lpszUrlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

// End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);

//**************************** 编码
int fUnicode=-1;
UINT uCodePage=CP_ACP;
LPSTR buffer;
DWORD dwBufferSize=16384;
buffer=(LPSTR)LocalAlloc(LPTR, dwBufferSize);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_TYPE, WINHTTP_HEADER_NAME_BY_INDEX,
buffer, &dwBufferSize, WINHTTP_NO_HEADER_INDEX);

TCHAR * pCursor;
_tcsupr((wchar_t *)buffer);
pCursor=(TCHAR *)_tcsstr((wchar_t *)buffer, TEXT("CHARSET="));
if(NULL!=pCursor)
{
pCursor+=8;
if(lstrcmp(pCursor, TEXT("UTF-8"))==0) uCodePage=CP_UTF8;
}

LocalFree(buffer);
//**************************************

LPSTR pszOutBuffer;
char pszPreviousBuffer[21];

ZeroMemory(pszPreviousBuffer, sizeof(pszPreviousBuffer));
int offset = 0;

//CString   strResult;
if (bResults)
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
{

}
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
dwSize=0;
}
else
{
ZeroMemory(pszOutBuffer, dwSize+1);
if (WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
int   iLen   =  dwSize+1;
wchar_t   *pUnicode   =   new   wchar_t[iLen];
MultiByteToWideChar(uCodePage,	0,   (LPCSTR)pszOutBuffer,   -1,   pUnicode,   iLen);
strResult->Append(pUnicode);
}
delete [] pszOutBuffer;
}

} while (dwSize>0);

return TRUE;
}

LRESULT TestDialog::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
if(!m_List)
{
RECT cRect2;
cRect2.left = 20;
cRect2.top = 100;
cRect2.bottom = 300;
cRect2.right = 550;
m_List.Create(m_hWnd,&cRect2,NULL,WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL,   WS_EX_CLIENTEDGE,2009);

char *szColumn[]={"编号","昵称","IP地址","登陆时间","状态"};

int widths[]={40,100,100,140,40};
LV_COLUMN lvc;
lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lvc.fmt=LVCFMT_LEFT;
for(int i=0;i<5;i++) {//插入各列
USES_CONVERSION;
lvc.pszText=A2W(szColumn[i]);
lvc.cx=widths[i];
lvc.iSubItem=i;
m_List.InsertColumn(i,&lvc);
}
}

CString strResult;
GetURL(_T("localhost"),&strResult);

USES_CONVERSION;
const   char*   pSth   =   T2A(strResult.GetBuffer(0));
const   char*   pSID  =    T2A(L"id");
const   char*   pSName = T2A(L"name");
const   char*   pSAddress  =    T2A(L"address");
const   char*   pSDate = T2A(L"datetime");
const   char*   pSState = T2A(L"state");

TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->Parse(pSth);
TiXmlElement *note = myDocument->RootElement();
TiXmlElement *CurrentPerson = note->FirstChildElement();

int i = 0;
while(CurrentPerson)
{
TiXmlElement *element = CurrentPerson;
TiXmlAttribute * attribute = element->FirstAttribute();

//attribute->Name();
//attribute->Value();

m_List.InsertItem(i,  L"");
while(attribute!= NULL)
{
if(strcmp( attribute->Name(),pSID)==0)
{
m_List.SetItemText (i, 0,A2CT(attribute->Value()));
}

if(strcmp( attribute->Name(),pSName)==0)
{
m_List.SetItemText (i, 1,A2CT(attribute->Value()));
}

if(strcmp( attribute->Name(),pSAddress)==0)
{
m_List.SetItemText (i, 2,A2CT(attribute->Value()));
}

if(strcmp( attribute->Name(),pSDate)==0)
{
//CString temp = CString(attribute->Value()).Format("%H:%M:%S");
//m_List.SetItemText (i, 3,temp.GetBuffer(1));
m_List.SetItemText (i, 3,A2CT(attribute->Value()));
}

if(strcmp( attribute->Name(),pSState)==0)
{
m_List.SetItemText (i, 4,A2CT(attribute->Value()));
}

attribute = attribute->Next();
}
i++;
CurrentPerson = CurrentPerson->NextSiblingElement();
}

return TRUE;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: