/
Form1.cs
1120 lines (913 loc) · 40.3 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.GeoprocessingUI;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
using Object = System.Object;
using Path = System.IO.Path;
namespace ArcEngine
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// 窗口最大化显示
WindowState = FormWindowState.Maximized;
}
// axMapControl1 选中 Map 对象
private IMap _hitMap;
// axMapControl1 选中 Layer 对象
private ILayer _hitLayer;
private void Form1_Load(object sender, EventArgs e)
{
// 设置 axMapControl1 地图显示名称
axMapControl1.Map.Name = "图层";
// treeView1 展开
treeView1.ExpandAll();
// 初始化
_hitMap = null;
_hitLayer = null;
}
private void axMapControl1_OnMouseDown(object sender,
IMapControlEvents2_OnMouseDownEvent e)
{
// 鼠标中键平移图层
if (e.button == 4)
{
axMapControl1.Pan();
}
}
// axTOCControl1 中判断鼠标点中的项目并弹出右键菜单
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2)
{
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null;
ILayer layer = null;
Object other = null;
Object index = null;
axTOCControl1.HitTest(
e.x, e.y, ref item, ref map, ref layer, ref other, ref index
);
// 如果点中了一个 Map 项目
if (item == esriTOCControlItem.esriTOCControlItemMap)
{
mapTOCMenuStrip.Show(axTOCControl1, e.x, e.y);
_hitMap = map as IMap;
}
else if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
layerTOCMenuStrip.Show(axTOCControl1, e.x, e.y);
_hitLayer = layer;
}
}
}
private void 添加数据ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = @"shp files (*.shp)|*.shp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
addLayer(openFileDialog.FileName);
}
}
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
{
axMapControl1.Map.DeleteLayer(_hitLayer);
}
// 同步 数据视图、布局视图
private void axMapControl1_OnExtentUpdated(Object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IObjectCopy pobjectcopy = new ObjectCopyClass();
object from = pobjectcopy.Copy(axMapControl1.Map);
object to = axPageLayoutControl1.ActiveView.FocusMap;
pobjectcopy.Overwrite(from, ref to);
axPageLayoutControl1.ActiveView.Refresh();
}
// 双击 axTOCControl1 显示符号系统
private void axTOCControl1_OnDoubleClick(Object sender, ITOCControlEvents_OnDoubleClickEvent e)
{
esriTOCControlItem toccItem = esriTOCControlItem.esriTOCControlItemNone;
ILayer iLayer = null;
IBasicMap iBasicMap = null;
object unk = null;
object data = null;
if (e.button == 1)
{
axTOCControl1.HitTest(e.x, e.y, ref toccItem, ref iBasicMap, ref iLayer, ref unk,
ref data);
new System.Drawing.Point(e.x, e.y);
if (toccItem == esriTOCControlItem.esriTOCControlItemLegendClass)
{
ILegendClass pLC = new LegendClassClass();
ILegendGroup pLG = new LegendGroupClass();
if (unk is ILegendGroup)
{
pLG = (ILegendGroup) unk;
}
pLC = pLG.get_Class((int) data);
ISymbol pSym;
pSym = pLC.Symbol;
ESRI.ArcGIS.DisplayUI.ISymbolSelector pSS = new
ESRI.ArcGIS.DisplayUI.SymbolSelectorClass();
bool bOK;
pSS.AddSymbol(pSym);
bOK = pSS.SelectSymbol(0);
if (bOK)
{
pLC.Symbol = pSS.GetSymbolAt(0);
}
axMapControl1.ActiveView.Refresh();
axTOCControl1.Refresh();
}
}
}
// 工具箱
private void treeView1_MouseDown(Object sender, MouseEventArgs e)
{
if ((sender as TreeView) != null)
{
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
string name = treeView1.SelectedNode.Name;
if (name == "缓冲区")
{
IHookHelper hookHelper = new HookHelperClass();
hookHelper.Hook = axMapControl1.Object;
BufferAnalysisForm bufferAnalysisForm = new BufferAnalysisForm(hookHelper, addLayer);
bufferAnalysisForm.ShowDialog();
// http://www.itboth.com/d/MFRRFf/textbox-buffer-arcengine-string-layer
}
else if (name == "擦除")
{
IHookHelper hookHelper = new HookHelperClass();
hookHelper.Hook = axMapControl1.Object;
EraseForm eraseForm = new EraseForm(hookHelper, addLayer);
eraseForm.ShowDialog();
}
else if (name == "加载站点")
{
if (dataBasePath == null)
{
Show("未选择数据集路径 !");
return;
}
ICommand pCommand;
pCommand = new AddNetStopsTool(dataBasePath);
pCommand.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = pCommand as ITool;
}
else if (name == "加载障碍点")
{
if (dataBasePath == null)
{
Show("未选择数据集路径 !");
return;
}
ICommand pCommand;
pCommand = new AddNetBarriesTool(dataBasePath);
pCommand.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = pCommand as ITool;
}
else if (name == "生成最短路径")
{
if (dataBasePath == null)
{
Show("未选择数据集路径 !");
return;
}
if (featureName == null)
{
Show("未选择数据集 !");
return;
}
if (networkName == null)
{
Show("未选择网络数据集 !");
return;
}
ICommand pCommand;
pCommand = new ShortPathSolveCommand(dataBasePath, featureName, networkName);
pCommand.OnCreate(axMapControl1.Object);
pCommand.OnClick();
}
else if (name == "清除分析")
{
axMapControl1.CurrentTool = null;
try
{
IFeatureWorkspace pFWorkspace;
//string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//打开工作空间
pFWorkspace = NetWorkAnalysClass.OpenWorkspace(dataBasePath) as IFeatureWorkspace;
IGraphicsContainer pGrap = axMapControl1.ActiveView as IGraphicsContainer;
pGrap.DeleteAllElements(); //删除所添加的图片要素
IFeatureClass inputFClass = pFWorkspace.OpenFeatureClass("Stops");
//删除站点要素
if (inputFClass.FeatureCount(null) > 0)
{
ITable pTable = inputFClass as ITable;
pTable.DeleteSearchedRows(null);
}
IFeatureClass barriesFClass = pFWorkspace.OpenFeatureClass("Barries"); //删除障碍点要素
if (barriesFClass.FeatureCount(null) > 0)
{
ITable pTable = barriesFClass as ITable;
pTable.DeleteSearchedRows(null);
}
for (int i = 0; i < axMapControl1.LayerCount; i++) //删除分析结果
{
ILayer pLayer = axMapControl1.get_Layer(i);
if (pLayer.Name == ShortPathSolveCommand.m_NAContext.Solver.DisplayName)
{
axMapControl1.DeleteLayer(i);
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
axMapControl1.Refresh();
}
else if (name == "最短路径分析")
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
dataBasePath = dialog.SelectedPath;
NetWorkForm netWorkForm = new NetWorkForm(
dataBasePath, setNetWorkPropety
);
netWorkForm.ShowDialog();
}
}
}
}
// 最短路径分析 属性
string dataBasePath; // 数据集路径
string featureName; // 数据集名称
string networkName; // 网路数据集名称
/// <summary>
/// 设置 数据集名称、网路数据集名称
/// </summary>
/// <param name="featureName"></param>
/// <param name="networkName"></param>
private void setNetWorkPropety(string featureName, string networkName)
{
this.featureName = featureName;
this.networkName = networkName;
}
// 利用 文件路径 为 axMapControl1 添加图层
private void addLayer(string path)
{
// GetDirectoryName 方法可以解析出目录名
string directoryName = Path.GetDirectoryName(path);
// GetFileNameWithoutExtension 方法可以解析出
// 不包含扩展名的文件名称(不包含路径名)
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
IWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
// 调用 OpenFromFile 通过目录名方法打开 Shapefile 数据库
IWorkspace workspace = shapefileWorkspaceFactory.OpenFromFile(directoryName, 0);
if (workspace != null)
{
// ESRI.ArcGIS.Geodatabase.IEnumDataset
IEnumDataset enumDataset =
workspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
enumDataset.Reset();
IDataset shpDataset = enumDataset.Next();
while (shpDataset != null)
{
if (shpDataset.Name == fileNameWithoutExtension)
{
// ESRI.ArcGIS.Carto.IFeatureLayer
IFeatureLayer newLayer = new FeatureLayerClass();
newLayer.FeatureClass = shpDataset as IFeatureClass;
newLayer.Name = fileNameWithoutExtension;
axMapControl1.Map.AddLayer(newLayer);
break;
}
shpDataset = enumDataset.Next();
}
}
}
private void Show(string s)
{
MessageBox.Show(s);
}
private void 图例ToolStripMenuItem_Click(Object sender, EventArgs e)
{
AddLegend(axPageLayoutControl1);
}
// 图例
private void AddLegend(AxPageLayoutControl axPageLayout)
{
//删除已经存在的图例
IElement pElement = axPageLayout.FindElementByName("Legends");
if (pElement != null)
{
axPageLayout.ActiveView.GraphicsContainer.DeleteElement(pElement);
}
IActiveView pActiveView = axPageLayout.PageLayout as IActiveView;
IGraphicsContainer container = axPageLayout.PageLayout as IGraphicsContainer;
// 获得MapFrame
IMapFrame mapFrame = container.FindFrame(pActiveView.FocusMap) as IMapFrame;
//根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround
UID uid = new UIDClass();
uid.Value = "esriCarto.Legend";
IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null);
//设置图例的Title
ILegend2 legend = mapSurroundFrame.MapSurround as ILegend2;
legend.Title = "地图图例";
ILegendFormat format = new LegendFormatClass();
ITextSymbol symbol = new TextSymbolClass();
symbol.Size = 4;
format.TitleSymbol = symbol;
legend.Format = format;
//QI,确定mapSurroundFrame的位置
IElement element = mapSurroundFrame as IElement;
IEnvelope envelope = new EnvelopeClass();
double x = 3;
double y = 5;
envelope.PutCoords(x, y, x + 5, y + 5);
element.Geometry = envelope;
//使用IGraphicsContainer接口添加显示
container.AddElement(element, 0);
pActiveView.Refresh();
}
private void 指北针ToolStripMenuItem_Click(Object sender, EventArgs e)
{
AddNorthArrow(axPageLayoutControl1);
}
// 指北针
public void AddNorthArrow(AxPageLayoutControl axPageLayout)
{
//删除已经存在的指北针
IElement pElement = axPageLayout.FindElementByName("NorthArrows");
if (pElement != null)
{
axPageLayout.ActiveView.GraphicsContainer.DeleteElement(pElement);
}
IGraphicsContainer container = axPageLayout.PageLayout as IGraphicsContainer;
IActiveView activeView = axPageLayout.PageLayout as IActiveView;
// 获得MapFrame
IFrameElement frameElement = container.FindFrame(activeView.FocusMap);
IMapFrame mapFrame = frameElement as IMapFrame;
//根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround
UID uid = new UIDClass();
uid.Value = "esriCarto.MarkerNorthArrow";
IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null);
//设置MapSurroundFrame中指北针的点符号
IMapSurround mapSurround = mapSurroundFrame.MapSurround;
IMarkerNorthArrow markerNorthArrow = mapSurround as IMarkerNorthArrow;
IMarkerSymbol markerSymbol = markerNorthArrow.MarkerSymbol;
markerSymbol.Size = 48;
markerNorthArrow.MarkerSymbol = markerSymbol;
//QI,确定mapSurroundFrame的位置
IElement element = mapSurroundFrame as IElement;
IEnvelope envelope = new EnvelopeClass();
double x = 15;
double y = 22;
envelope.PutCoords(x, y, x + 5, y + 5);
element.Geometry = envelope;
//使用IGraphicsContainer接口添加显示
container.AddElement(element, 0);
activeView.Refresh();
}
private void 比例尺ToolStripMenuItem_Click(Object sender, EventArgs e)
{
AddScalebar(axPageLayoutControl1);
}
// 比例尺
public void AddScalebar(AxPageLayoutControl axPageLayout)
{
//删除已经存在的比例尺
IElement pelement = axPageLayout.FindElementByName("AlternatingScaleBar");
if (pelement != null)
{
axPageLayout.ActiveView.GraphicsContainer.DeleteElement(pelement);
}
IGraphicsContainer container = axPageLayout.PageLayout as IGraphicsContainer;
IActiveView activeView = axPageLayout.PageLayout as IActiveView;
// 获得MapFrame
IFrameElement frameElement = container.FindFrame(activeView.FocusMap);
IMapFrame mapFrame = frameElement as IMapFrame;
//根据MapSurround的uid,创建相应的MapSurroundFrame和MapSurround
UID uid = new UIDClass();
uid.Value = "esriCarto.AlternatingScaleBar";
IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null);
//设置MapSurroundFrame中比例尺的样式
IMapSurround mapSurround = mapSurroundFrame.MapSurround;
IScaleBar markerScaleBar = ((IScaleBar) mapSurround);
markerScaleBar.LabelPosition = esriVertPosEnum.esriBelow;
markerScaleBar.UseMapSettings();
//QI,确定mapSurroundFrame的位置
IElement element = mapSurroundFrame as IElement;
IEnvelope envelope = new EnvelopeClass();
double x = 12;
double y = 5;
envelope.PutCoords(x, y, x + 1, y + 1);
element.Geometry = envelope;
//使用IGraphicsContainer接口添加显示
container.AddElement(element, 0);
activeView.Refresh();
}
private void 标题ToolStripMenuItem_Click(Object sender, EventArgs e)
{
TitleForm titleForm = new TitleForm(AddTitle);
titleForm.ShowDialog();
}
// 标题
private void AddTitle(String title)
{
IGraphicsContainer graphicsContainer = axPageLayoutControl1.PageLayout as IGraphicsContainer;
IEnvelope envelope = new EnvelopeClass();
double x = 8;
double y = 22;
envelope.PutCoords(x, y, x + 5, y + 5);
IRgbColor pColor = new RgbColorClass()
{
Red = 0,
Blue = 0,
Green = 0
};
IFontDisp pFont = new StdFont()
{
Name = "宋体",
Bold = true
} as IFontDisp;
ITextSymbol pTextSymbol = new TextSymbolClass()
{
Color = pColor,
Font = pFont,
Size = 25
};
ITextElement pTextElement = new TextElementClass()
{
Symbol = pTextSymbol,
ScaleText = true,
Text = title
};
IElement element = pTextElement as IElement;
element.Geometry = envelope;
graphicsContainer.AddElement(element, 0);
axPageLayoutControl1.Refresh();
}
private void axPageLayoutControl1_OnMouseDown(Object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
{
// 中键平移布局视图
if (e.button == 4)
{
axPageLayoutControl1.Pan();
}
}
// 打开属性表
private void 属性ToolStripMenuItem_Click(Object sender, EventArgs e)
{
// 传入点击的图层
PropertySheet propertySheet = new PropertySheet(_hitLayer as IFeatureLayer);
propertySheet.ShowDialog();
}
// 打开符号系统
private void 符号系统ToolStripMenuItem_Click(Object sender, EventArgs e)
{
}
// 导出地图
private void 导出ToolStripMenuItem_Click(Object sender, EventArgs e)
{
SaveFileDialog mapExportDialog = new SaveFileDialog();
mapExportDialog.Filter =
"JPEG格式(*.jpg)|*.jpg|EPS格式(*.eps)|*.eps|EMF格式(*.emf)|*.emf|BMP格式(*.bmp)|*.bmp|PDF格式(*.pdf)|*.pdf|TIFF格式(*.tif)|*.tif|PNG格式(*.png)|*.png|SVG格式(*.svg)|*.svg|AI格式(*.ai)|*.ai|所有格式(*.*)|*.*";
mapExportDialog.RestoreDirectory = true;
if (mapExportDialog.ShowDialog() == DialogResult.OK)
{
string strLocalFileName = mapExportDialog.FileName;
//获取文件路径,不带文件名
string strFilePath = strLocalFileName.Substring(0, strLocalFileName.LastIndexOf("\\") + 2);
//string strFileName = strLocalFileName.Substring(strLocalFileName.LastIndexOf("\\") + 1, strLocalFileName.LastIndexOf(".")-3);
String strFileName = Path.GetFileNameWithoutExtension(strLocalFileName);
string picType;
switch (mapExportDialog.FilterIndex)
{
case 1:
picType = "JPEG";
break;
case 2:
picType = "EPS";
break;
case 3:
picType = "EMF";
break;
case 4:
picType = "BMP";
break;
case 5:
picType = "PDF";
break;
case 6:
picType = "TIFF";
break;
case 7:
picType = "PNG";
break;
case 8:
picType = "SVG";
break;
case 9:
picType = "AI";
break;
default:
picType = "EMF";
break;
}
// MessageBox.Show(picType);
ExportActiveViewParameterized(96, 1, picType, strFilePath, strFileName, false, axPageLayoutControl1);
}
}
[DllImport("GDI32.dll")]
public static extern int GetDeviceCaps(int hdc, int nIndex);
[DllImport("User32.dll")]
public static extern int GetDC(int hWnd);
[DllImport("User32.dll")]
public static extern int ReleaseDC(int hWnd, int hDC);
//[DllImport("user32.dll", SetLastError = true)]
//static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);
const uint SpiGetfontsmoothing = 74;
const uint SpiSetfontsmoothing = 75;
const uint SpifUpdateinifile = 0x1;
private void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType,
string sOutputDir, string sNameRoot, Boolean bClipToGraphicsExtent, AxPageLayoutControl pageLayoutControl)
{
//解决文件名错误
sNameRoot = sNameRoot.Substring(1, sNameRoot.Length - 1);
IActiveView docActiveView = pageLayoutControl.ActiveView;
IExport docExport;
long iPrevOutputImageQuality;
IOutputRasterSettings docOutputRasterSettings;
IEnvelope PixelBoundsEnv;
tagRECT exportRECT;
tagRECT DisplayBounds;
IDisplayTransformation docDisplayTransformation;
IPageLayout docPageLayout;
IEnvelope docMapExtEnv;
long hdc;
long tmpDC;
//string sNameRoot;
long iScreenResolution;
bool bReenable = false;
IEnvelope docGraphicsExtentEnv;
IUnitConverter pUnitConvertor;
if (GetFontSmoothing())
{
bReenable = true;
DisableFontSmoothing();
if (GetFontSmoothing())
{
//font smoothing is NOT successfully disabled, error out.
return;
}
//else font smoothing was successfully disabled.
}
// The Export*Class() type initializes a new export class of the desired type.
if (ExportType == "PDF")
{
docExport = new ExportPDFClass();
}
else if (ExportType == "EPS")
{
docExport = new ExportPSClass();
}
else if (ExportType == "AI")
{
docExport = new ExportAIClass();
}
else if (ExportType == "BMP")
{
docExport = new ExportBMPClass();
}
else if (ExportType == "TIFF")
{
docExport = new ExportTIFFClass();
}
else if (ExportType == "SVG")
{
docExport = new ExportSVGClass();
}
else if (ExportType == "PNG")
{
docExport = new ExportPNGClass();
}
else if (ExportType == "GIF")
{
docExport = new ExportGIFClass();
}
else if (ExportType == "EMF")
{
docExport = new ExportEMFClass();
}
else if (ExportType == "JPEG")
{
docExport = new ExportJPEGClass();
}
else
{
MessageBox.Show("!!不支持的格式 " + ExportType + ", 默认导出 EMF.");
ExportType = "EMF";
docExport = new ExportEMFClass();
}
// save the previous output image quality, so that when the export is complete it will be set back.
docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio;
if (docExport is IExportImage)
{
// always set the output quality of the DISPLAY to 1 for image export formats
SetOutputQuality(docActiveView, 1);
}
else
{
// for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time
SetOutputQuality(docActiveView, lResampleRatio);
}
//set the name root for the export
// sNameRoot = "ExportActiveViewSampleOutput";
//set the export filename (which is the nameroot + the appropriate file extension)
docExport.ExportFileName =
sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];
tmpDC = GetDC(0);
iScreenResolution = GetDeviceCaps((int) tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X)
ReleaseDC(0, (int) tmpDC);
docExport.Resolution = iOutputResolution;
if (docActiveView is IPageLayout)
{
//get the bounds of the "exportframe" of the active view.
DisplayBounds = docActiveView.ExportFrame;
//set up pGraphicsExtent, used if clipping to graphics extent.
docGraphicsExtentEnv = GetGraphicsExtent(docActiveView);
}
else
{
//Get the bounds of the deviceframe for the screen.
docDisplayTransformation = docActiveView.ScreenDisplay.DisplayTransformation;
DisplayBounds = docDisplayTransformation.get_DeviceFrame();
}
PixelBoundsEnv = new Envelope() as IEnvelope;
if (bClipToGraphicsExtent && (docActiveView is IPageLayout))
{
docGraphicsExtentEnv = GetGraphicsExtent(docActiveView);
docPageLayout = docActiveView as PageLayout;
pUnitConvertor = new UnitConverter();
//assign the x and y values representing the clipped area to the PixelBounds envelope
PixelBoundsEnv.XMin = 0;
PixelBoundsEnv.YMin = 0;
PixelBoundsEnv.XMax =
pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMax, docPageLayout.Page.Units,
esriUnits.esriInches) * docExport.Resolution -
pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMin, docPageLayout.Page.Units,
esriUnits.esriInches) * docExport.Resolution;
PixelBoundsEnv.YMax =
pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMax, docPageLayout.Page.Units,
esriUnits.esriInches) * docExport.Resolution -
pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMin, docPageLayout.Page.Units,
esriUnits.esriInches) * docExport.Resolution;
//'assign the x and y values representing the clipped export extent to the exportRECT
exportRECT.bottom = (int) (PixelBoundsEnv.YMax) + 1;
exportRECT.left = (int) (PixelBoundsEnv.XMin);
exportRECT.top = (int) (PixelBoundsEnv.YMin);
exportRECT.right = (int) (PixelBoundsEnv.XMax) + 1;
//since we're clipping to graphics extent, set the visible bounds.
docMapExtEnv = docGraphicsExtentEnv;
}
else
{
double tempratio = iOutputResolution / iScreenResolution;
double tempbottom = DisplayBounds.bottom * tempratio;
double tempright = DisplayBounds.right * tempratio;
//'The values in the exportRECT tagRECT correspond to the width
//and height to export, measured in pixels with an origin in the top left corner.
exportRECT.bottom = (int) Math.Truncate(tempbottom);
exportRECT.left = 0;
exportRECT.top = 0;
exportRECT.right = (int) Math.Truncate(tempright);
//populate the PixelBounds envelope with the values from exportRECT.
// We need to do this because the exporter object requires an envelope object
// instead of a tagRECT structure.
PixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
//since it's a page layout or an unclipped page layout we don't need docMapExtEnv.
docMapExtEnv = null;
}
// Assign the envelope object to the exporter object's PixelBounds property. The exporter object
// will use these dimensions when allocating memory for the export file.
docExport.PixelBounds = PixelBoundsEnv;
// call the StartExporting method to tell docExport you're ready to start outputting.
hdc = docExport.StartExporting();
// Redraw the active view, rendering it to the exporter object device context instead of the app display.
// We pass the following values:
// * hDC is the device context of the exporter object.
// * exportRECT is the tagRECT structure that describes the dimensions of the view that will be rendered.
// The values in exportRECT should match those held in the exporter object's PixelBounds property.
// * docMapExtEnv is an envelope defining the section of the original image to draw into the export object.
docActiveView.Output((int) hdc, (int) docExport.Resolution, ref exportRECT, docMapExtEnv, null);
//finishexporting, then cleanup.
docExport.FinishExporting();
docExport.Cleanup();
MessageBox.Show(
"成功导出地图: " + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] +
".", "提示");
//set the output quality back to the previous value
SetOutputQuality(docActiveView, iPrevOutputImageQuality);
if (bReenable)
{
EnableFontSmoothing();
bReenable = false;
if (!GetFontSmoothing())
{
//error: cannot reenable font smoothing.
MessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error");
}
}
docMapExtEnv = null;
PixelBoundsEnv = null;
}
private void SetOutputQuality(IActiveView docActiveView, long iResampleRatio)
{
IGraphicsContainer oiqGraphicsContainer;
IElement oiqElement;
IOutputRasterSettings docOutputRasterSettings;
IMapFrame docMapFrame;
IActiveView TmpActiveView;
if (docActiveView is IMap)
{
docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int) iResampleRatio;
}
else if (docActiveView is IPageLayout)
{
//assign ResampleRatio for PageLayout
docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int) iResampleRatio;
//and assign ResampleRatio to the Maps in the PageLayout
oiqGraphicsContainer = docActiveView as IGraphicsContainer;
oiqGraphicsContainer.Reset();
oiqElement = oiqGraphicsContainer.Next();
while (oiqElement != null)
{
if (oiqElement is IMapFrame)
{
docMapFrame = oiqElement as IMapFrame;
TmpActiveView = docMapFrame.Map as IActiveView;
docOutputRasterSettings =
TmpActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
docOutputRasterSettings.ResampleRatio = (int) iResampleRatio;
}
oiqElement = oiqGraphicsContainer.Next();
}
docMapFrame = null;
oiqGraphicsContainer = null;
TmpActiveView = null;
}
docOutputRasterSettings = null;
}
private IEnvelope GetGraphicsExtent(IActiveView docActiveView)
{
IEnvelope GraphicsBounds;
IEnvelope GraphicsEnvelope;
IGraphicsContainer oiqGraphicsContainer;
IPageLayout docPageLayout;
IDisplay GraphicsDisplay;
IElement oiqElement;
GraphicsBounds = new EnvelopeClass();
GraphicsEnvelope = new EnvelopeClass();
docPageLayout = docActiveView as IPageLayout;
GraphicsDisplay = docActiveView.ScreenDisplay;
oiqGraphicsContainer = docActiveView as IGraphicsContainer;
oiqGraphicsContainer.Reset();
oiqElement = oiqGraphicsContainer.Next();
while (oiqElement != null)
{
oiqElement.QueryBounds(GraphicsDisplay, GraphicsEnvelope);
GraphicsBounds.Union(GraphicsEnvelope);
oiqElement = oiqGraphicsContainer.Next();
}
return GraphicsBounds;
}
private void DisableFontSmoothing()