-
Notifications
You must be signed in to change notification settings - Fork 3
/
LatLonParser.cs
2158 lines (1931 loc) · 78 KB
/
LatLonParser.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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace KMZRebuilder
{
public class PointD
{
public double X;
public double Y;
public byte Type;
public PointD() { }
public PointD(int X, int Y)
{
this.X = X;
this.Y = Y;
}
public PointD(int X, int Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(float X, float Y)
{
this.X = X;
this.Y = Y;
}
public PointD(float X, float Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(double X, double Y)
{
this.X = X;
this.Y = Y;
}
public PointD(double X, double Y, byte Type)
{
this.X = X;
this.Y = Y;
this.Type = Type;
}
public PointD(Point point)
{
this.X = point.X;
this.Y = point.Y;
}
public PointD(Point point, byte Type)
{
this.X = point.X;
this.Y = point.Y;
this.Type = Type;
}
public PointD(PointF point)
{
this.X = point.X;
this.Y = point.Y;
}
public PointD(PointF point, byte Type)
{
this.X = point.X;
this.Y = point.Y;
this.Type = Type;
}
public PointF PointF
{
get
{
return new PointF((float)X, (float)Y);
}
set
{
this.X = value.X;
this.Y = value.Y;
}
}
public bool IsEmpty
{
get
{
return (X == 0) && (Y == 0);
}
}
public static PointF ToPointF(PointD point)
{
return point.PointF;
}
public static PointF[] ToPointF(PointD[] points)
{
PointF[] result = new PointF[points.Length];
for (int i = 0; i < result.Length; i++)
result[i] = points[i].PointF;
return result;
}
}
public class LatLonParser
{
public enum FFormat : byte
{
None = 0,
DDDDDD = 1,
DDMMMM = 2,
DDMMSS = 3
}
public enum DFormat : byte
{
ENG_NS = 0,
ENG_EW = 1,
RUS_NS = 2,
RUS_EW = 3,
MINUS = 4,
DEFAULT = 4,
NONE = 4
}
public static double ToLat(string line_in)
{
return Parse(line_in, true);
}
public static double ToLon(string line_in)
{
return Parse(line_in, false);
}
public static double Parse(string line_in, bool true_lat_false_lon)
{
int nn = 1;
string full = GetCorrectString(line_in, true_lat_false_lon, out nn);
if (String.IsNullOrEmpty(full)) return 0f;
string mm = "0";
string ss = "0";
string dd = "0";
if (full.IndexOf("°") > 0)
{
int dms = 0;
int from = 0;
int next = 0;
dd = full.Substring(from, (next = full.IndexOf("°", from)) - from);
from = next + 1;
if (full.IndexOf("'") > 0)
{
dms = 1;
mm = full.Substring(from, (next = full.IndexOf("'", from)) - from);
from = next + 1;
};
if (full.IndexOf("\"") > 0)
{
dms = 2;
ss = full.Substring(from, (next = full.IndexOf("\"", from)) - from);
from = next + 1;
};
if (from < full.Length)
{
if (dms == 1)
ss = full.Substring(from);
else if (dms == 0)
mm = full.Substring(from);
};
}
else
{
bool loop = true;
double num3 = 0.0;
int num4 = 1;
if (full[0] == '-') num4++;
while (loop)
{
try
{
num3 = Convert.ToDouble(full.Substring(0, num4++), System.Globalization.CultureInfo.InvariantCulture);
}
catch
{
loop = false;
};
if (num4 > full.Length)
{
loop = false;
};
}
dd = num3.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
double d = ((Convert.ToDouble(dd, System.Globalization.CultureInfo.InvariantCulture) + Convert.ToDouble(mm, System.Globalization.CultureInfo.InvariantCulture) / 60.0 + Convert.ToDouble(ss, System.Globalization.CultureInfo.InvariantCulture) / 60.0 / 60.0) * (double)nn);
return d;
}
public static PointD Parse(string line_in)
{
return new PointD(ToLon(line_in), ToLat(line_in));
}
private static string GetCorrectString(string str, bool lat, out int digit)
{
digit = 1;
if (String.IsNullOrEmpty(str)) return null;
string text = str.Trim();
if (String.IsNullOrEmpty(text)) return null;
text = text.ToLower().Replace("``", "\"").Replace("`", "'").Replace("%20", " ").Trim();
while (text.IndexOf(" ") >= 0) text = text.Replace(" ", " ");
text = text.Replace("° ", "°").Replace("' ", "'").Replace("\" ", "\"");
if (String.IsNullOrEmpty(text)) return null;
bool hasDigits = false;
bool noletters = true;
for (int i = 0; i < text.Length; i++)
{
if (char.IsDigit(text[i]))
hasDigits = true;
if (char.IsLetter(text[i]))
noletters = false;
};
if (!hasDigits) return null;
if (noletters)
{
string[] lalo = text.Split(new char[] { '+', ' ', '=', ';', ',', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (lalo.Length == 0)
return null;
if (lalo.Length == 2)
{
if (lat)
text = lalo[0];
else
text = lalo[1];
};
};
text = text.Replace("+", "").Replace(" ", "").Replace("=", "").Replace(";", "").Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim();
if (String.IsNullOrEmpty(text)) return null;
double d;
if (double.TryParse(text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d))
{
if (d < 0) digit = -1;
return text.Replace("-","");
};
int copyl = text.Length;
int find = 0;
int start = 0;
bool endsWithLetter = (char.IsLetter(text[text.Length - 1]));
if (lat)
{
if ((find = text.IndexOf("lat")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("latitude")) >= 0) start = find + (endsWithLetter ? 0 : 8);
if ((find = text.IndexOf("ø")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("øèð")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("øèðîòà")) >= 0) start = find + (endsWithLetter ? 0 : 6);
if ((find = text.IndexOf("n")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("ñ")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("ñø")) >= 0) start = find + (endsWithLetter ? 0 : 2);
if ((find = text.IndexOf("ñ.ø")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("ñ.ø.")) >= 0) start = find + (endsWithLetter ? 0 : 4);
if ((find = text.IndexOf("s")) >= 0) { start = find + (endsWithLetter ? 0 : 1); digit = -1; };
if ((find = text.IndexOf("þ")) >= 0) { start = find + (endsWithLetter ? 0 : 1); digit = -1; };
if ((find = text.IndexOf("þø")) >= 0) { start = find + (endsWithLetter ? 0 : 2); digit = -1; };
if ((find = text.IndexOf("þ.ø")) >= 0) { start = find + (endsWithLetter ? 0 : 3); digit = -1; };
if ((find = text.IndexOf("þ.ø.")) >= 0) { start = find + (endsWithLetter ? 0 : 4); digit = -1; };
}
else
{
if ((find = text.IndexOf("lon")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("longitude")) >= 0) start = find + (endsWithLetter ? 0 : 9);
if ((find = text.IndexOf("ä")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("äîë")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("äîëãîòà")) >= 0) start = find + (endsWithLetter ? 0 : 7);
if ((find = text.IndexOf("e")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("â")) >= 0) start = find + (endsWithLetter ? 0 : 1);
if ((find = text.IndexOf("âä")) >= 0) start = find + (endsWithLetter ? 0 : 2);
if ((find = text.IndexOf("â.ä")) >= 0) start = find + (endsWithLetter ? 0 : 3);
if ((find = text.IndexOf("â.ä.")) >= 0) start = find + (endsWithLetter ? 0 : 4);
if ((find = text.IndexOf("w")) >= 0) { start = find + (endsWithLetter ? 0 : 1); digit = -1; };
if ((find = text.IndexOf("ç")) >= 0) { start = find + (endsWithLetter ? 0 : 1); digit = -1; };
if ((find = text.IndexOf("çä")) >= 0) { start = find + (endsWithLetter ? 0 : 2); digit = -1; };
if ((find = text.IndexOf("ç.ä")) >= 0) { start = find + (endsWithLetter ? 0 : 3); digit = -1; };
if ((find = text.IndexOf("ç.ä.")) >= 0) { start = find + (endsWithLetter ? 0 : 4); digit = -1; };
};
if (endsWithLetter)
{
copyl = start;
start = 0;
for (int i = copyl - 1; i >= start; i--)
if (char.IsLetter(text[i]))
copyl = copyl - (start = i + 1);
}
else
{
for (int i = start; i < copyl; i++)
if (char.IsLetter(text[i]))
copyl = i - start;
};
if (copyl > (text.Length - start)) copyl -= start;
text = text.Substring(start, copyl);
text = text.Replace(",", ".");
return text;
}
public static string ToString(double fvalue)
{
return DoubleToString(fvalue, -1);
}
public static string ToString(double fvalue, int digitsAfterDelimiter)
{
return DoubleToString(fvalue, digitsAfterDelimiter);
}
public static string ToString(double lat, double lon)
{
return String.Format("{0},{1}", ToString(lat), ToString(lon));
}
public static string ToString(double lat, double lon, int digitsAfterDelimiter)
{
return String.Format("{0},{1}", DoubleToString(lat, digitsAfterDelimiter), DoubleToString(lon, digitsAfterDelimiter));
}
public static string ToString(double lat, double lon, FFormat fformat)
{
if (fformat == FFormat.None)
return String.Format("{0},{1}", ToString(lat, fformat), ToString(lon, fformat));
else
return String.Format("{0} {1} {2} {3}", new string[] { GetLinePrefix(lat, DFormat.ENG_NS), ToString(lat, fformat), GetLinePrefix(lat, DFormat.ENG_EW), ToString(lon, fformat) });
}
public static string ToString(double lat, double lon, FFormat fformat, int digitsAfterDelimiter)
{
if (fformat == FFormat.None)
return String.Format("{0},{1}", ToString(lat, fformat, digitsAfterDelimiter), ToString(lon, fformat, digitsAfterDelimiter));
else
return String.Format("{0} {1} {2} {3}", new string[] { GetLinePrefix(lat, DFormat.ENG_NS), ToString(lat, fformat, digitsAfterDelimiter), GetLinePrefix(lat, DFormat.ENG_EW), ToString(lon, fformat, digitsAfterDelimiter) });
}
public static string ToString(PointD latlon)
{
return String.Format("{0},{1}", ToString(latlon.Y), ToString(latlon.X));
}
public static string ToString(PointD latlon, int digitsAfterDelimiter)
{
return String.Format("{0},{1}", DoubleToString(latlon.Y, digitsAfterDelimiter), DoubleToString(latlon.X, digitsAfterDelimiter));
}
public static string ToString(PointD latlon, FFormat fformat)
{
if (fformat == FFormat.None)
return String.Format("{0},{1}", ToString(latlon.Y, fformat), ToString(latlon.X, fformat));
else
return String.Format("{0} {1} {2} {3}", new string[] { GetLinePrefix(latlon.Y, DFormat.ENG_NS), ToString(latlon.Y, fformat), GetLinePrefix(latlon.X, DFormat.ENG_EW), ToString(latlon.X, fformat) });
}
public static string ToString(PointD latlon, FFormat fformat, int digitsAfterDelimiter)
{
if (fformat == FFormat.None)
return String.Format("{0},{1}", ToString(latlon.Y, fformat, digitsAfterDelimiter), ToString(latlon.X, fformat, digitsAfterDelimiter));
else
return String.Format("{0} {1} {2} {3}", new string[] { GetLinePrefix(latlon.Y, DFormat.ENG_NS), ToString(latlon.Y, fformat, digitsAfterDelimiter), GetLinePrefix(latlon.X, DFormat.ENG_EW), ToString(latlon.X, fformat, digitsAfterDelimiter) });
}
public static string ToString(double fvalue, FFormat format)
{
return ToString(fvalue, format, 6);
}
public static string ToString(double fvalue, FFormat format, int digitsAfterDelimiter)
{
double num = Math.Abs(fvalue);
string result;
if (format == FFormat.None)
{
result = DoubleToString(num, digitsAfterDelimiter);
}
else if (format == FFormat.DDDDDD)
{
result = DoubleToString(num, digitsAfterDelimiter) + "°";
}
else
{
string text = "";
text = text + DoubleToString(Math.Truncate(num), 0) + "° ";
double num2 = (num - Math.Truncate(num)) * 60.0;
if (format == FFormat.DDMMMM)
{
text = text + DoubleToString(num2, 4) + "'";
}
else
{
text = text + string.Format("{0}", (int)Math.Truncate(num2)) + "' ";
num2 = (num2 - Math.Truncate(num2)) * 60.0;
text = text + DoubleToString(num2, 3) + "\"";
}
result = text;
}
return result;
}
public static string GetLinePrefix(double fvalue, DFormat format)
{
string result;
switch ((byte)format)
{
case 0:
result = ((fvalue >= 0.0) ? "N" : "S");
break;
case 1:
result = ((fvalue >= 0.0) ? "E" : "W");
break;
case 2:
result = ((fvalue >= 0.0) ? "Ñ" : "Þ");
break;
case 3:
result = ((fvalue >= 0.0) ? "Â" : "Ç");
break;
default:
result = ((fvalue >= 0.0) ? "" : "-");
break;
}
return result;
}
public static string DoubleToString(double val, int digitsAfterDelimiter)
{
if (digitsAfterDelimiter < 0)
return val.ToString(System.Globalization.CultureInfo.InvariantCulture);
string daf = "";
for (int i = 0; i < digitsAfterDelimiter; i++) daf += "0";
return val.ToString("0." + daf, System.Globalization.CultureInfo.InvariantCulture);
}
public static string DoubleToStringMax(double val, int maxDigitsAfterDelimiter)
{
string res = val.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (maxDigitsAfterDelimiter < 0) return res;
if (res.IndexOf(".") < 0) return res;
if ((res.Length - res.IndexOf(".")) <= maxDigitsAfterDelimiter) return res;
string daf = "";
for (int i = 0; i < maxDigitsAfterDelimiter; i++) daf += "0";
return val.ToString("0." + daf, System.Globalization.CultureInfo.InvariantCulture);
}
}
public class SASPlacemarkConnector
{
[StructLayout(LayoutKind.Sequential)]
public struct TRect
{
public int Left, Top, Right, Bottom;
public TRect(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public TRect(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
public int X
{
get { return Left; }
set { Right -= (Left - value); Left = value; }
}
public int Y
{
get { return Top; }
set { Bottom -= (Top - value); Top = value; }
}
public int Height
{
get { return Bottom - Top; }
set { Bottom = value + Top; }
}
public int Width
{
get { return Right - Left; }
set { Right = value + Left; }
}
public System.Drawing.Point Location
{
get { return new System.Drawing.Point(Left, Top); }
set { X = value.X; Y = value.Y; }
}
public System.Drawing.Size Size
{
get { return new System.Drawing.Size(Width, Height); }
set { Width = value.Width; Height = value.Height; }
}
public static implicit operator System.Drawing.Rectangle(TRect r)
{
return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
}
public static implicit operator TRect(System.Drawing.Rectangle r)
{
return new TRect(r);
}
public static bool operator ==(TRect r1, TRect r2)
{
return r1.Equals(r2);
}
public static bool operator !=(TRect r1, TRect r2)
{
return !r1.Equals(r2);
}
public bool Equals(TRect r)
{
return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
}
public override bool Equals(object obj)
{
if (obj is TRect)
return Equals((TRect)obj);
else if (obj is System.Drawing.Rectangle)
return Equals(new TRect((System.Drawing.Rectangle)obj));
return false;
}
public override int GetHashCode()
{
return ((System.Drawing.Rectangle)this).GetHashCode();
}
public override string ToString()
{
return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
}
}
public enum TernaryRasterOperations : uint
{
/// <summary>dest = source</summary>
SRCCOPY = 0x00CC0020,
/// <summary>dest = source OR dest</summary>
SRCPAINT = 0x00EE0086,
/// <summary>dest = source AND dest</summary>
SRCAND = 0x008800C6,
/// <summary>dest = source XOR dest</summary>
SRCINVERT = 0x00660046,
/// <summary>dest = source AND (NOT dest)</summary>
SRCERASE = 0x00440328,
/// <summary>dest = (NOT source)</summary>
NOTSRCCOPY = 0x00330008,
/// <summary>dest = (NOT src) AND (NOT dest)</summary>
NOTSRCERASE = 0x001100A6,
/// <summary>dest = (source AND pattern)</summary>
MERGECOPY = 0x00C000CA,
/// <summary>dest = (NOT source) OR dest</summary>
MERGEPAINT = 0x00BB0226,
/// <summary>dest = pattern</summary>
PATCOPY = 0x00F00021,
/// <summary>dest = DPSnoo</summary>
PATPAINT = 0x00FB0A09,
/// <summary>dest = pattern XOR dest</summary>
PATINVERT = 0x005A0049,
/// <summary>dest = (NOT dest)</summary>
DSTINVERT = 0x00550009,
/// <summary>dest = BLACK</summary>
BLACKNESS = 0x00000042,
/// <summary>dest = WHITE</summary>
WHITENESS = 0x00FF0062,
/// <summary>
/// Capture window as seen on screen. This includes layered windows
/// such as WPF windows with AllowsTransparency="true"
/// </summary>
CAPTUREBLT = 0x40000000
}
private delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out TRect lpRect);
[DllImport("gdi32.dll")]
public static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
TernaryRasterOperations dwRop);
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
int nWidth, int nHeight,
IntPtr hdcSrc, int nXSrc, int nYSrc,
TernaryRasterOperations dwRop);
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32.dll")]
public static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(HandleRef hWnd);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindow(HandleRef hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wparam, IntPtr lparam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wparam, int lparam);
//Sets window attributes
[DllImport("USER32.DLL")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
//Gets window attributes
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
private const int SW_HIDE = 0x0000;
private const int SW_SHOWNORMAL = 0x0001;
public const int SW_SHOW = 0x0005;
private const int WM_SETTEXT = 0x000C;
private const int WM_GETTEXT = 0x000D;
private const int WM_GETTEXTLENGTH = 0x000E;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
public const uint WM_CHAR = 0x102;
public const uint WM_LBUTTONDOWN = 0x201;
public const uint WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
private const int MK_LBUTTON = 0x01;
private const int VK_RETURN = 0x0d;
private const int VK_ESCAPE = 0x1b;
private const int VK_TAB = 0x09;
private const int VK_LEFT = 0x25;
private const int VK_UP = 0x26;
private const int VK_RIGHT = 0x27;
private const int VK_DOWN = 0x28;
private const int VK_F5 = 0x74;
private const int VK_F6 = 0x75;
private const int VK_F7 = 0x76;
private static uint MF_BYPOSITION = 0x400;
private static uint MF_REMOVE = 0x1000;
private static int GWL_STYLE = -16;
private static int WS_CHILD = 0x40000000; //child window
private static int WS_BORDER = 0x00800000; //window with border
private static int WS_DLGFRAME = 0x00400000; //window with double border but no title
private static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar
private static int WS_SYSMENU = 0x00080000; //window menu
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
return false;
list.Add(handle);
return true;
}
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
Win32Callback childProc = new Win32Callback(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
public static string GetWinClass(IntPtr hwnd)
{
if (hwnd == IntPtr.Zero)
return null;
StringBuilder classname = new StringBuilder(100);
IntPtr result = GetClassName(hwnd, classname, classname.Capacity);
if (result != IntPtr.Zero)
return classname.ToString();
return null;
}
public static IEnumerable<IntPtr> EnumAllWindows(IntPtr hwnd, string childClassName)
{
List<IntPtr> children = GetChildWindows(hwnd);
if (children == null)
yield break;
foreach (IntPtr child in children)
{
if (GetWinClass(child) == childClassName)
yield return child;
foreach (IntPtr childchild in EnumAllWindows(child, childClassName))
yield return childchild;
}
}
public static string GetText(IntPtr hWnd)
{
IntPtr Handle = Marshal.AllocHGlobal(250);
int NumText = (int)SendMessage(hWnd, WM_GETTEXT, 250, Handle);
byte[] res = new byte[250];
Marshal.Copy(Handle, res, 0, res.Length);
Marshal.FreeHGlobal(Handle);
string Text = System.Text.Encoding.GetEncoding(1251).GetString(res, 0, NumText);
return Text;
}
public static string GetText(IntPtr hWnd, out byte[] res)
{
IntPtr Handle = Marshal.AllocHGlobal(250);
int NumText = (int)SendMessage(hWnd, WM_GETTEXT, 250, Handle);
res = new byte[250];
Marshal.Copy(Handle, res, 0, res.Length);
Marshal.FreeHGlobal(Handle);
string Text = System.Text.Encoding.GetEncoding(1251).GetString(res, 0, NumText);
return Text;
}
public static bool SetText(IntPtr hWnd, string text)
{
IntPtr Handle = Marshal.AllocHGlobal(250);
byte[] txt = System.Text.Encoding.GetEncoding(1251).GetBytes(text + "\0");
Marshal.Copy(txt, 0, Handle, txt.Length);
int NumText = (int)SendMessage(hWnd, WM_SETTEXT, txt.Length, Handle);
Marshal.FreeHGlobal(Handle);
return NumText == 1;
}
public void Show()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if (MEP != IntPtr.Zero)
ShowWindow(MEP, SW_SHOW);
};
}
public bool SASisOpen
{
get
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
return true;
return false;
}
}
public void ShowSAS()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length == 0) return;
System.Diagnostics.Process p = procs[0];
ShowWindow(p.MainWindowHandle, SW_SHOW);
SetForegroundWindow(p.MainWindowHandle);
SetActiveWindow(p.MainWindowHandle);
SetFocus(p.MainWindowHandle);
}
private bool IsVisible()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if (MEP != IntPtr.Zero)
return IsWindowVisible(MEP);
};
return false;
}
public bool Visible
{
get
{
return IsVisible();
}
set
{
if (value == true)
Show();
else
Hide();
}
}
public void Hide()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if (MEP != IntPtr.Zero)
ShowWindow(MEP, SW_HIDE);
};
}
public bool ClickOk()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if (MEP != IntPtr.Zero)
{
IntPtr tp0 = IntPtr.Zero;
while ((tp0 = FindWindowEx(MEP, tp0, "TPanel", null)) != IntPtr.Zero)
{
IntPtr tp1 = IntPtr.Zero;
while ((tp1 = FindWindowEx(tp0, tp1, "TButton", null)) != IntPtr.Zero)
{
string txt = GetText(tp1);
if (txt == "Ok")
{
SendMessage(tp1, (int)WM_LBUTTONDOWN, (int)10, (int)10);
SendMessage(tp1, (int)WM_LBUTTONUP, (int)10, (int)10);
};
};
};
};
};
return false;
}
public bool ClickCancel()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if (MEP != IntPtr.Zero)
{
IntPtr tp0 = IntPtr.Zero;
while ((tp0 = FindWindowEx(MEP, tp0, "TPanel", null)) != IntPtr.Zero)
{
IntPtr tp1 = IntPtr.Zero;
while ((tp1 = FindWindowEx(tp0, tp1, "TButton", null)) != IntPtr.Zero)
{
string txt = GetText(tp1);
if ((txt != "Ok") && (txt == "~"))
{
SendMessage(tp1, (int)WM_LBUTTONDOWN, (int)10, (int)10);
SendMessage(tp1, (int)WM_LBUTTONUP, (int)10, (int)10);
};
};
};
};
};
return false;
}
public bool AddPlacemark(string name, double lat, double lon)
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("SASPlanet");
if (procs.Length > 0)
{
System.Diagnostics.Process p = procs[0];
// List<IntPtr> chw = GetChildWindows(p.MainWindowHandle);
IntPtr MEP = FindWindow("TfrmMarkEditPoint", null);
if ((MEP == IntPtr.Zero) || (!IsWindowVisible(MEP)))
{
ShowWindow(p.MainWindowHandle, SW_SHOW);
SetForegroundWindow(p.MainWindowHandle);
SetActiveWindow(p.MainWindowHandle);