//附:1.注记可显示,但和图形对齐的方式没找到;2.没加注释---有愿意的不愿意。
HRESULT hr = E_FAIL;
IGeoFeatureLayerPtr pGeoFeaLyr;
IMapControl2Ptr pMapControl;
pMapControl=m_MapCtrl.get_Object();
pGeoFeaLyr=pMapControl->GetLayer(2);//层索引
if(pGeoFeaLyr == NULL) return;
//////////////////////////////////////////////////////////////////////////
ILineLabelPlacementPrioritiesPtr pLineLabPlaPrior(__uuidof(LineLabelPlacementPriorities));
hr = pLineLabPlaPrior->put_AboveAfter(3);
hr = pLineLabPlaPrior->put_AboveAlong(1);
hr = pLineLabPlaPrior->put_AboveBefore(3);
hr = pLineLabPlaPrior->put_AboveEnd(3);
hr = pLineLabPlaPrior->put_AboveStart(3);
hr = pLineLabPlaPrior->put_BelowAfter(3);
hr = pLineLabPlaPrior->put_BelowAlong(3);
hr = pLineLabPlaPrior->put_BelowBefore(3);
hr = pLineLabPlaPrior->put_BelowEnd(3);
hr = pLineLabPlaPrior->put_BelowStart(3);
hr = pLineLabPlaPrior->put_CenterAfter(3);
hr = pLineLabPlaPrior->put_CenterAlong(3);
hr = pLineLabPlaPrior->put_CenterBefore(3);
hr = pLineLabPlaPrior->put_CenterEnd(3);
hr = pLineLabPlaPrior->put_CenterStart(3);
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
ILineLabelPositionPtr pLineLabPos(__uuidof(LineLabelPosition));
hr = pLineLabPos->put_Above(TRUE);
hr = pLineLabPos->put_AtEnd(FALSE);
hr = pLineLabPos->put_AtStart(FALSE);
hr = pLineLabPos->put_Below(FALSE);
hr = pLineLabPos->put_InLine(FALSE);
hr = pLineLabPos->put_Left(FALSE);
hr = pLineLabPos->put_OnTop(FALSE);
hr = pLineLabPos->put_Right(FALSE);
hr = pLineLabPos->put_Parallel(TRUE);
hr = pLineLabPos->put_Perpendicular(FALSE);
hr = pLineLabPos->put_Horizontal(FALSE);
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
IPointPlacementPrioritiesPtr pPtPlacePrior(__uuidof(PointPlacementPriorities));
hr = pPtPlacePrior->put_AboveCenter(2);
hr = pPtPlacePrior->put_AboveLeft(3);
hr = pPtPlacePrior->put_AboveRight(1);
hr = pPtPlacePrior->put_BelowCenter(3);
hr = pPtPlacePrior->put_BelowLeft(3);
hr = pPtPlacePrior->put_BelowRight(3);
hr = pPtPlacePrior->put_CenterLeft(30);
hr = pPtPlacePrior->put_CenterRight(2);
//////////////////////////////////////////////////////////////////////////
IBasicOverposterLayerPropertiesPtr pBasicOverLyrProps(__uuidof(BasicOverposterLayerProperties));
hr = pBasicOverLyrProps->put_NumLabelsOption(esriNoLabelRestrictions);
hr = pBasicOverLyrProps->put_PointPlacementPriorities(pPtPlacePrior);
hr = pBasicOverLyrProps->put_LineLabelPosition(pLineLabPos);
hr = pBasicOverLyrProps->put_LineLabelPlacementPriorities(pLineLabPlaPrior);
//////////////////////////////////////////////////////////////////////////
CString sExpress;
//sExpress.Format("Function FindLabel([Name],[Rank]) /n/rFindLabel=Right([Name],1)&[Rank]/n/rEnd Function");//复杂标到式,下面的IsExpressionSimple需设为FALSE
sExpress = _T("[ObjectID]& [Name]");//
ILabelEngineLayerPropertiesPtr pLabEngLyrProps(__uuidof(LabelEngineLayerProperties));
IAnnotateLayerPropertiesCollectionPtr pAnnoLyrPropsColl;
pGeoFeaLyr->get_AnnotationProperties(&pAnnoLyrPropsColl);
if(pAnnoLyrPropsColl == NULL) return;
pAnnoLyrPropsColl->Clear();
IAnnotateLayerPropertiesPtr pAnnoLyrProps = pLabEngLyrProps;
pAnnoLyrProps->put_WhereClause((_bstr_t)"[ObjectID] <= 3");
//////////////////////////////////////////////////////////////////////////
hr = pLabEngLyrProps->put_Expression(sExpress.AllocSysString());
hr = pLabEngLyrProps->put_IsExpressionSimple(VARIANT_TRUE);
//hr = pLabEngLyrProps->put_IsExpressionSimple(VARIANT_FALSE);
hr = pLabEngLyrProps->putref_BasicOverposterLayerProperties(pBasicOverLyrProps);
IAnnotationExpressionEnginePtr pAnnoExpEng(__uuidof(AnnotationVBScriptEngine));
IAnnotationExpressionParserPtr pAnnoExpParser;
hr = pAnnoExpEng->raw_SetExpression(_bstr_t(""),sExpress.AllocSysString(),&pAnnoExpParser);
hr = pLabEngLyrProps->putref_ExpressionParser(pAnnoExpEng);
IColorPtr pColor(__uuidof(RgbColor));
pColor->put_RGB(RGB(255,0,0));
IFontPtr pFont;
pFont.CreateInstance("StdFont");
pFont->put_Name((_bstr_t)"仿宋_GB2321");
pFont->put_Bold(TRUE);
CY cy;
cy.Hi = 10 * 80;
cy.Lo = 8 * 80;
pFont->put_Size(cy);
pFont->put_Italic(FALSE);
pFont->put_Underline(FALSE);
pFont->put_Strikethrough(FALSE);
IFontDispPtr pFontDisp(pFont);
ITextSymbolPtr pTextSym(__uuidof(TextSymbol));
pTextSym->put_Font(pFontDisp);
pTextSym->put_Size(18);
pTextSym->put_Color(pColor);
pTextSym->put_HorizontalAlignment(esriTHALeft);
pTextSym->put_RightToLeft(VARIANT_TRUE);
pLabEngLyrProps->putref_Symbol(pTextSym);
pAnnoLyrPropsColl->Add(pAnnoLyrProps);
pGeoFeaLyr->put_DisplayAnnotation(VARIANT_TRUE);
IActiveViewPtr pAV;
pAV=pMapControl->GetActiveView();
pAV->Refresh();
///////////////////////////////////////////////////////////////////////////////////////////
如下为C#代码
IAnnotateLayerPropertiesCollection pAnnoProps = null;
//得到图层的标注属性集合对象
IAnnotateLayerProperties pAnnoLayerProps = null; ;
ILineLabelPosition pPosition = null;
ILineLabelPlacementPriorities pPlacement = null;
IBasicOverposterLayerProperties pBasic = null;
ILabelEngineLayerProperties pLabelEngine = null;
pAnnoProps = pGeoFeaLyr.AnnotationProperties;
pAnnoProps.Clear();
//新建标注文本的文本编号
ITextSymbol pTextSyl = null;
pTextSyl = new TextSymbolClass();
stdole.StdFont pFont;
pFont = new stdole.StdFontClass();
pFont.Name = "宋体";
pFont.Size = 5;//字体大小
pTextSyl.Font = pFont as stdole.IFontDisp;
IRgbColor pRGB = new RgbColorClass();
pRGB.Red = 110; pRGB.Blue = 200; pRGB.Green = 60;
pTextSyl.Color = pRGB;
//设置标注文本的位置
pPosition = new LineLabelPositionClass();
pPosition.Parallel = false;
pPosition.Perpendicular = true;
pPlacement = new LineLabelPlacementPrioritiesClass();
pBasic = new BasicOverposterLayerPropertiesClass();
pBasic.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolyline;
pBasic.LineLabelPlacementPriorities = pPlacement;
pBasic.LineLabelPosition = pPosition;
//新建一个图层标注引擎对象,设置它的属性
pLabelEngine = new LabelEngineLayerPropertiesClass();
pLabelEngine.Symbol = pTextSyl;
pLabelEngine.BasicOverposterLayerProperties = pBasic;
//field必须是这种样式,如”[STATE_NAME]”
pLabelEngine.Expression = sAnnoField;
pLabelEngine.Expression = "[" + sAnnoField + "]";
pAnnoLayerProps = pLabelEngine as IAnnotateLayerProperties;
pAnnoProps.Add(pAnnoLayerProps);
pGeoFeaLyr.DisplayAnnotation = true;