当前位置:早雪网网络学院编程文档其他语言 → 在vc中用dhtml优化设计

在vc中用dhtml优化设计

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:53:03

再公司中,师兄说,一个地税的管理系统用dhtml技术,可以在vc中开发出一个很漂亮的界面出来,

于是就开始查阅相关的资料

收集了一个demo,现在正在研究,不过看起开,效果还是挺不错的,但就是自己还没有掌握这样的技术,郁闷ing 

关键代码如下:

#include "stdafx.h"
#include "sreport.h"
#include "SHTMLReport.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSHTMLReport::CSHTMLReport()
{
 m_pHtmlDoc2=NULL;
 m_iHeadLines=1;
}

CSHTMLReport::~CSHTMLReport()
{

}

//***********************************************
// 指定HTML文档接口
//**********************************************
void CSHTMLReport::SetHtmlDocPtr(IHTMLDocument2 *pDoc)
{
 m_pHtmlDoc2=pDoc;
}

//***********************************************
// 指定操作的表的名称,表名称在HTML模板中指定
//*********************************************
void CSHTMLReport::SetTableName(CString name)
{
 m_strTableName=name;
}

//**************************************************
// 获取HTML表格COM接口,内部接口
//**************************************************
IHTMLTable * CSHTMLReport::GetTableDispatch()
{
 ASSERT(m_pHtmlDoc2);
 IHTMLElementCollection *all;
 m_pHtmlDoc2->get_all(&all);
 IDispatch *distable;
 all->item(COleVariant(m_strTableName),COleVariant(short(0)),&distable);
 IHTMLTable *pITable=NULL;
 HRESULT hr=distable->QueryInterface(IID_IHTMLTable, (void**)&pITable);//get table dispatch
 ASSERT(hr==S_OK);
 return pITable;
}

//*******************************************************
// 在指定位置插入一行,index==-1表示append
//*******************************************************
BOOL CSHTMLReport::InsertRow(int index)
{
 IHTMLTable *pITable=GetTableDispatch();
 IDispatch *disrow;
 HRESULT hr=pITable->insertRow(index,&disrow);//insert a  row at 1 position
 if(hr!=S_OK) return FALSE;

 IHTMLTableRow *pIRow;
 hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
 ASSERT(hr==S_OK);
 
 long cols;
 hr=pITable->get_cols(&cols);
 ASSERT(hr==S_OK&&cols!=0);
 
 IDispatch *discell;
 IHTMLElement *cell;

 CString str= " ";
 BSTR bsStr = str.AllocSysString();
 for(int i=0;i<cols;i++)
 {
  pIRow->insertCell(i,&discell);
  discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
  cell->put_innerHTML(bsStr);
  cell->Release();
  discell->Release();
 }
 SysFreeString(bsStr);
 pIRow->Release();

 if(!m_strIndexFormat.IsEmpty())
 {
  IHTMLElementCollection *irows;
  hr=pITable->get_rows(&irows);
  ASSERT(hr==S_OK);
  long rows;
  hr=irows->get_length(&rows);
  ASSERT(hr==S_OK);
  CString strHead="";
  if(index==-1) index=rows-1;
  for(long i=index;i<rows;i++)
  {
   strHead.Format(m_strIndexFormat,i);
   BSTR bsStrHead=strHead.AllocSysString();
   hr=irows->item(COleVariant(long(i)),COleVariant(long(0)),&disrow);
   ASSERT(hr==S_OK);
   //get row interface
   hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
   ASSERT(hr==S_OK);
   IHTMLElementCollection *icells;
   hr=pIRow->get_cells(&icells);
   ASSERT(hr==S_OK);
   hr=icells->item(COleVariant(long(0)),COleVariant(long(0)),&discell);
   ASSERT(hr==S_OK);
   discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
   ASSERT(hr==S_OK);
   cell->put_innerText(bsStrHead);
   SysFreeString(bsStrHead);
   cell->Release();
   pIRow->Release();
   disrow->Release();
  }
  irows->Release();
 }

 pIRow->Release();
 disrow->Release();
 pITable->Release();
 return TRUE;
}

//***************************************************************
// 修改单元格的内容:可以使用html语法
//***************************************************************
BOOL CSHTMLReport::SetItemHTML(int iRow, int iCol, CString html)
{
 IHTMLTable *pITable=GetTableDispatch();
 //get row collect
 IHTMLElementCollection *pIRows;
 HRESULT hr=pITable->get_rows(&pIRows);
 ASSERT(hr==S_OK);
 //get specisfied row
 IDispatch *disrow;
 hr=pIRows->item(COleVariant(long(iRow)),COleVariant(short(0)),&disrow);//iRow row
 if(hr!=S_OK||disrow==NULL) return FALSE;

 IHTMLTableRow *pIRow;
 hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
 ASSERT(hr==S_OK);
 //get cell collect
 IHTMLElementCollection *rowcells;
 pIRow->get_cells(&rowcells);
 //get cell
 IDispatch *discell;
 IHTMLElement *cell;
 hr=rowcells->item(COleVariant(long(iCol)),COleVariant(short(0)),&discell);//iCol col
 if(hr!=S_OK||discell==NULL) return FALSE;
 hr=discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
 ASSERT(hr==S_OK);

 BSTR bsStr = html.AllocSysString();
 cell->put_innerHTML(bsStr);
 SysFreeString(bsStr);
 
 cell->Release();
 pIRow->Release();
 pITable->Release();
 return TRUE;
}

//************************************************
// 删除指定行
//************************************************
BOOL CSHTMLReport::DeleteRow(int index)
{
 IHTMLTable *pITable=GetTableDispatch();
 HRESULT hr=pITable->deleteRow(index);
 pITable->Release();
 return hr==S_OK;
}

//************************************************
// 将指定行数据相同的单元格合并
//************************************************
BOOL CSHTMLReport::MergeRow(int iRow)
{
 IHTMLTable *pITable=GetTableDispatch();
 //get row collect
 IHTMLElementCollection *pIRows;
 HRESULT hr=pITable->get_rows(&pIRows);
 ASSERT(hr==S_OK);
 //get specisfied row
 IDispatch *disrow;
 hr=pIRows->item(COleVariant(long(iRow)),COleVariant(short(0)),&disrow)

[1] [2] [3]  下一页

[数据载入中...] [返回上一页] [打 印]