|
【讨论】怎样用miDataSetGlobalHandle来实现数据的绑定
CMapXBindLayer bLayer;
CMapXFields flds;
bLayer.CreateDispatch(bLayer.GetClsid());
flds.CreateDispatch(flds.GetClsid());
//Our source data in the correct tab-delimited form.
//In practice, this could come from a text file or some
//other source.
const char* tabifiedData =
"\"Cust1\"\t\"Loc1\"\t-72.40\t42.22\r\n"
"\"Cust2\"\t\"Loc2\"\t-75.40\t40.48\r\n"
"\"Cust3\"\t\"Loc3\"\t-76.40\t38.02\r\n";
bLayer.SetLayerName("Customer");
bLayer.SetRefColumn1(3);
bLayer.SetRefColumn2(4);
bLayer.SetLayerType(miBindLayerTypeXY);
flds.Add(1, "Customer");
flds.Add(2, "Location");
flds.Add(3, "X");
flds.Add(4, "Y");
//The global handle which will contain the actual data.
HGLOBAL hGlobalData=NULL;
//This temporarily points to the location of the locked
//handle's data
char* pHandleData=NULL;
COleVariant SourceData;
//Allocate space for the handle's data and copy the source
//data into it
hGlobalData = GlobalAlloc(GMEM_MOVEABLE, strlen(tabifiedData)+1);
pHandleData = (char*)GlobalLock(hGlobalData);
strcpy(pHandleData, tabifiedData);
GlobalUnlock(hGlobalData);
pHandleData = NULL;
//Point the SourceData variant at the global handle
SourceData.vt = VT_I4;
SourceData.lVal = (long)hGlobalData;
try {
//Now add the Dataset to the Datasets collection
COleVariant bindVt, fldsVt;
COptionalVariant optVt;
fldsVt.vt = VT_DISPATCH;
fldsVt.pdispVal = flds.m_lpDispatch;
bindVt.vt = VT_DISPATCH;
bindVt.pdispVal = bLayer.m_lpDispatch;
CMapXDataset ds = m_ctrlMapX.GetDatasets().Add(miDataSetGlobalHandle, SourceData, COleVariant("My Dataset"), COleVariant(1l), optVt, bindVt, fldsVt, optVt);
//Create a simple Theme from the data
ds.GetThemes().Add(COptionalVariant(), COptionalVariant(), COptionalVariant());
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
} |
|