作者:baby小明君 | 来源:互联网 | 2023-08-15 13:27
//附: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] <&#61; 3");
//
hr &#61; pLabEngLyrProps->put_Expression(sExpress.AllocSysString());
hr &#61; pLabEngLyrProps->put_IsExpressionSimple(VARIANT_TRUE);
//hr &#61; pLabEngLyrProps->put_IsExpressionSimple(VARIANT_FALSE);
hr &#61; pLabEngLyrProps->putref_BasicOverposterLayerProperties(pBasicOverLyrProps);
IAnnotationExpressionEnginePtr pAnnoExpEng(__uuidof(AnnotationVbscriptEngine));
IAnnotationExpressionParserPtr pAnnoExpParser;
hr &#61; pAnnoExpEng->raw_SetExpression(_bstr_t(""),sExpress.AllocSysString(),&pAnnoExpParser);
hr &#61; 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 &#61; 10 * 80;
cy.Lo &#61; 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&#61;pMapControl->GetActiveView();
pAV->Refresh();
///
如下为C#代码
IAnnotateLayerPropertiesCollection pAnnoProps &#61; null;
//得到图层的标注属性集合对象
IAnnotateLayerProperties pAnnoLayerProps &#61; null; ;
ILineLabelPosition pPosition &#61; null;
ILineLabelPlacementPriorities pPlacement &#61; null;
IBasicOverposterLayerProperties pBasic &#61; null;
ILabelEngineLayerProperties pLabelEngine &#61; null;
pAnnoProps &#61; pGeoFeaLyr.AnnotationProperties;
pAnnoProps.Clear();
//新建标注文本的文本编号
ITextSymbol pTextSyl &#61; null;
pTextSyl &#61; new TextSymbolClass();
stdole.StdFont pFont;
pFont &#61; new stdole.StdFontClass();
pFont.Name &#61; "宋体";
pFont.Size &#61; 5;//字体大小
pTextSyl.Font &#61; pFont as stdole.IFontDisp;
IRgbColor pRGB &#61; new RgbColorClass();
pRGB.Red &#61; 110; pRGB.Blue &#61; 200; pRGB.Green &#61; 60;
pTextSyl.Color &#61; pRGB;
//设置标注文本的位置
pPosition &#61; new LineLabelPositionClass();
pPosition.Parallel &#61; false;
pPosition.Perpendicular &#61; true;
pPlacement &#61; new LineLabelPlacementPrioritiesClass();
pBasic &#61; new BasicOverposterLayerPropertiesClass();
pBasic.FeatureType &#61; esriBasicOverposterFeatureType.esriOverposterPolyline;
pBasic.LineLabelPlacementPriorities &#61; pPlacement;
pBasic.LineLabelPosition &#61; pPosition;
//新建一个图层标注引擎对象&#xff0c;设置它的属性
pLabelEngine &#61; new LabelEngineLayerPropertiesClass();
pLabelEngine.Symbol &#61; pTextSyl;
pLabelEngine.BasicOverposterLayerProperties &#61; pBasic;
//field必须是这种样式&#xff0c;如”[STATE_NAME]”
pLabelEngine.Expression &#61; sAnnoField;
pLabelEngine.Expression &#61; "[" &#43; sAnnoField &#43; "]";
pAnnoLayerProps &#61; pLabelEngine as IAnnotateLayerProperties;
pAnnoProps.Add(pAnnoLayerProps);
pGeoFeaLyr.DisplayAnnotation &#61; true;