代码如下:
// x1, y1, x2, y2为在地图中选定的两个点坐标
// 分别作为要绘制矩形框的左下角和右上角两个点
x1 = 20741.798;
x2 = 20739.798;
y1 = 3401.866;
y2 = 3409.866;
pt.Set(x1, y1);
pts.Add(pt);
pt.Set(x1, y2);
pts.Add(pt);
pt.Set(x2, y2);
pts.Add(pt);
pt.Set(x2, y1);
pts.Add(pt);
sty = m_MapXFeature.GetStyle();// 获取Style
lyr = m_ctrlMapX.GetLayers().Item("TEMP1");// 获取要添加的图层
DrawRect(lyr, pts, sty);// 调用DrawRect函数绘制矩形框
// DrawRect函数,绘制矩形框
void DrawRect(CMapXLayer lyr, CMapXPoints pts, CMapXStyle sty)
{
try {
CMapXFeatureFactory fac;
CMapXFeature ftr, ftr2;
COleVariant vtPts, vtSty;
fac = m_ctrlMapX.GetFeatureFactory();
// Points Variant
vtPts.vt = VT_DISPATCH;
vtPts.pdispVal = pts.m_lpDispatch;
vtPts.pdispVal->AddRef();
// Style Variant
vtSty.vt = VT_DISPATCH;
vtSty.pdispVal = sty.m_lpDispatch;
vtSty.pdispVal->AddRef();
ftr = fac.CreateRegion(vtPts, vtSty);
ftr2 = lyr.AddFeature(ftr);
ftr2.Update();
lyr.Refresh();
} catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
} catch (COleException *e) {
e->ReportError();
e->Delete();
}
}