jace.tang
2023-02-11 ed8d469ccdc0e627d8f180bb92a9d78dbdb008b1
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
using Bro.UI.HalconDisplay.ViewROI;
using HalconDotNet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
 
namespace Bro.UI.HalconDisplay
{
    public delegate void IconicDelegate(int val);
    public delegate void FuncDelegate();
    public delegate void OnIconicObjectMovedHandler(Object sender, double moveX, double moveY);
    public delegate void OnIconicObjectZoomedHandler(Object sender, double zoomCenterX, double zoomCenterY, double scale);
 
    /// <summary>
    /// This class works as a wrapper class for the HALCON window
    /// HWindow. HWndCtrl is in charge of the visualization.
    /// You can move and zoom the visible image part by using GUI component 
    /// inputs or with the mouse. The class HWndCtrl uses a graphics stack 
    /// to manage the iconic objects for the display. Each object is linked 
    /// to a graphical context, which determines how the object is to be drawn.
    /// The context can be changed by calling changeGraphicSettings().
    /// The graphical "modes" are defined by the class GraphicsContext and 
    /// map most of the dev_set_* operators provided in HDevelop.
    /// </summary>
    public class HWndCtrl
    {
        /// <summary>No action is performed on mouse events</summary>
        public const int MODE_VIEW_NONE = 10;
 
        /// <summary>Zoom is performed on mouse events</summary>
        public const int MODE_VIEW_ZOOM = 11;
 
        /// <summary>Move is performed on mouse events</summary>
        public const int MODE_VIEW_MOVE = 12;
 
        /// <summary>Magnification is performed on mouse events</summary>
        public const int MODE_VIEW_ZOOMWINDOW = 13;
 
 
        public const int MODE_INCLUDE_ROI = 1;
 
        public const int MODE_EXCLUDE_ROI = 2;
 
 
        /// <summary>
        /// Constant describes delegate message to signal new image
        /// </summary>
        public const int EVENT_UPDATE_IMAGE = 31;
        /// <summary>
        /// Constant describes delegate message to signal error
        /// when reading an image from file
        /// </summary>
        public const int ERR_READING_IMG = 32;
        /// <summary> 
        /// Constant describes delegate message to signal error
        /// when defining a graphical context
        /// </summary>
        public const int ERR_DEFINING_GC = 33;
 
        /// <summary> 
        /// Maximum number of HALCON objects that can be put on the graphics 
        /// stack without loss. For each additional object, the first entry 
        /// is removed from the stack again.
        /// </summary>
        private const int MAXNUMOBJLIST = 1000;
 
 
        private int stateView;
        private bool mousePressed = false;
        private double startX, startY;
 
        /// <summary>HALCON window</summary>
        private HWindowControl viewPort;
 
        /// <summary>
        /// Instance of ROIController, which manages ROI interaction
        /// </summary>
        private ROIController roiManager;
 
        /* dispROI is a flag to know when to add the ROI models to the 
           paint routine and whether or not to respond to mouse events for 
           ROI objects */
        private int dispROI;
 
 
        /* Basic parameters, like dimension of window and displayed image part */
        private int windowWidth;
        private int windowHeight;
        private int imageWidth;
        private int imageHeight;
 
        private int[] CompRangeX;
        private int[] CompRangeY;
 
 
        private int prevCompX, prevCompY;
        private double stepSizeX, stepSizeY;
 
 
        /* Image coordinates, which describe the image part that is displayed  
           in the HALCON window */
        private double ImgRow1, ImgCol1, ImgRow2, ImgCol2;
 
        /// <summary>Error message when an exception is thrown</summary>
        public string exceptionText = "";
 
 
        /* Delegates to send notification messages to other classes */
        /// <summary>
        /// Delegate to add information to the HALCON window after 
        /// the paint routine has finished
        /// </summary>
        public FuncDelegate addInfoDelegate;
 
        /// <summary>
        /// Delegate to notify about failed tasks of the HWndCtrl instance
        /// </summary>
        public IconicDelegate NotifyIconObserver;
 
 
        private HWindow ZoomWindow;
        private double zoomWndFactor;
        private double zoomAddOn;
        private int zoomWndSize;
 
 
        /// <summary> 
        /// List of HALCON objects to be drawn into the HALCON window. 
        /// The list shouldn't contain more than MAXNUMOBJLIST objects, 
        /// otherwise the first entry is removed from the list.
        /// </summary>
        public List<HObjectEntry> HObjList;
 
        /// <summary>
        /// Instance that describes the graphical context for the
        /// HALCON window. According on the graphical settings
        /// attached to each HALCON object, this graphical context list 
        /// is updated constantly.
        /// </summary>
        public GraphicsContext mGC;
 
 
        /// <summary> 
        /// Initializes the image dimension, mouse delegation, and the 
        /// graphical context setup of the instance.
        /// </summary>
        /// <param name="view"> HALCON window </param>
        public HWndCtrl(HWindowControl view)
        {
            //viewPort = view;
            //stateView = MODE_VIEW_NONE;
            //windowWidth = viewPort.Size.Width;
            //windowHeight = viewPort.Size.Height;
 
            //zoomWndFactor = (double)imageWidth / viewPort.Width;
            //zoomAddOn = Math.Pow(0.9, 5);
            //zoomWndSize = 150;
 
            ///*default*/
            //CompRangeX = new int[] { 0, 100 };
            //CompRangeY = new int[] { 0, 100 };
 
            //prevCompX = prevCompY = 0;
 
            //dispROI = MODE_INCLUDE_ROI;//1;
 
            //viewPort.HMouseUp += new HalconDotNet.HMouseEventHandler(this.mouseUp);
            //viewPort.HMouseDown += new HalconDotNet.HMouseEventHandler(this.mouseDown);
            //viewPort.HMouseMove += new HalconDotNet.HMouseEventHandler(this.mouseMoved);
 
            //addInfoDelegate = new FuncDelegate(dummyV);
            //NotifyIconObserver = new IconicDelegate(dummy);
 
            //// graphical stack 
            //HObjList = new ArrayList(20);
            //mGC = new GraphicsContext();
            //mGC.gcNotification = new GCDelegate(exceptionGC);
            viewPort = view;
            stateView = MODE_VIEW_NONE;
            windowWidth = viewPort.Size.Width;
            windowHeight = viewPort.Size.Height;
 
            // initialize the image part to window size
            initializeDisplayImagePart();
            resetImagePart(widthImagePart, heightImagePart);
 
 
            if (widthImagePart > 0)
                zoomWndFactor = (double)widthImagePart / viewPort.Width;
            else
                zoomWndFactor = 1;
 
            zoomAddOn = Math.Pow(0.9, 5);
            zoomWndSize = 150;
 
            /*Set the boundaries and steps for changes for the GUI elements*/
            /*default*/
            CompRangeX = new int[] { 0, 100 };
            CompRangeY = new int[] { 0, 100 };
 
            prevCompX = prevCompY = 0;
            
            /* Initialize the values for value range, step for 
             * some GUI elements */
            setGUICompRangeX(CompRangeX, prevCompX);
            setGUICompRangeY(CompRangeY, prevCompY);
 
            //displayMode = MODE_VIEW_NONE;
 
            dispROI = MODE_INCLUDE_ROI;//1;
 
            viewPort.HMouseUp += new HalconDotNet.HMouseEventHandler(this.mouseUp);
            viewPort.HMouseDown += new HalconDotNet.HMouseEventHandler(this.mouseDown);
            viewPort.HMouseMove += new HalconDotNet.HMouseEventHandler(this.mouseMoved);
            viewPort.HMouseWheel += new HalconDotNet.HMouseEventHandler(this.mouseWheel);
 
            addInfoDelegate = new FuncDelegate(dummyV);
            NotifyIconObserver = new IconicDelegate(dummy);
 
 
 
            //OnImageZoomed = new OnIconicObjectZoomedHandler(dummyZoomed);
            //OnImageZoomed = new OnIconicObjectZoomedHandler(dummyZoomed);
            // graphical stack 
            HObjList = new List<HObjectEntry>();
            mGC = new GraphicsContext();
            mGC.gcNotification = new GCDelegate(exceptionGC);
 
        }
 
 
        /// <summary>
        /// Registers an instance of an ROIController with this window 
        /// controller (and vice versa).
        /// </summary>
        /// <param name="rC"> 
        /// Controller that manages interactive ROIs for the HALCON window 
        /// </param>
        public void useROIController(ROIController rC)
        {
            roiManager = rC;
            rC.setViewController(this);
        }
 
        /// <summary>
        /// Adjust window settings by the values supplied for the left 
        /// upper corner and the right lower corner
        /// </summary>
        /// <param name="r1">y coordinate of left upper corner</param>
        /// <param name="c1">x coordinate of left upper corner</param>
        /// <param name="r2">y coordinate of right lower corner</param>
        /// <param name="c2">x coordinate of right lower corner</param>
        public void setImagePart(int r1, int c1, int r2, int c2)
        {
            ImgRow1 = r1;
            ImgCol1 = c1;
            ImgRow2 = imageHeight = r2;
            ImgCol2 = imageWidth = c2;
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)ImgCol1;
            rect.Y = (int)ImgRow1;
            rect.Height = (int)imageHeight;
            rect.Width = (int)imageWidth;
            viewPort.ImagePart = rect;
        }
 
 
        /// <summary>
        /// Sets the view mode for mouse events in the HALCON window
        /// (zoom, move, magnify or none).
        /// </summary>
        /// <param name="mode">One of the MODE_VIEW_* constants</param>
        public void setViewState(int mode)
        {
            stateView = mode;
 
            if (roiManager != null)
                roiManager.resetROI();
        }
 
        /********************************************************************/
        private void dummy(int val)
        {
 
        }
      
        private void dummyV()
        {
 
        }
 
        /*******************************************************************/
        private void exceptionGC(string message)
        {
            exceptionText = message;
            NotifyIconObserver(ERR_DEFINING_GC);
        }
 
        /// <summary>
        /// Paint or don't paint the ROIs into the HALCON window by 
        /// defining the parameter to be equal to 1 or not equal to 1.
        /// </summary>
        public void setDispLevel(int mode)
        {
            dispROI = mode;
        }
 
        /****************************************************************************/
        /*                          graphical element                               */
        /****************************************************************************/
        private void zoomImage(double x, double y, double scale)
        {
            double lengthC, lengthR;
            double percentC, percentR;
            int lenC, lenR;
 
            percentC = (x - ImgCol1) / (ImgCol2 - ImgCol1);
            percentR = (y - ImgRow1) / (ImgRow2 - ImgRow1);
 
            lengthC = (ImgCol2 - ImgCol1) * scale;
            lengthR = (ImgRow2 - ImgRow1) * scale;
 
            ImgCol1 = x - lengthC * percentC;
            ImgCol2 = x + lengthC * (1 - percentC);
 
            ImgRow1 = y - lengthR * percentR;
            ImgRow2 = y + lengthR * (1 - percentR);
 
            lenC = (int)Math.Round(lengthC);
            lenR = (int)Math.Round(lengthR);
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)Math.Round(ImgCol1);
            rect.Y = (int)Math.Round(ImgRow1);
            rect.Width = (lenC > 0) ? lenC : 1;
            rect.Height = (lenR > 0) ? lenR : 1;
            viewPort.ImagePart = rect;
 
            zoomWndFactor *= scale;
            repaint();
        }
 
        /// <summary>
        /// Scales the image in the HALCON window according to the 
        /// value scaleFactor
        /// </summary>
        public void zoomImage(double scaleFactor)
        {
            double midPointX, midPointY;
 
            if (((ImgRow2 - ImgRow1) == scaleFactor * imageHeight) &&
                ((ImgCol2 - ImgCol1) == scaleFactor * imageWidth))
            {
                repaint();
                return;
            }
 
            ImgRow2 = ImgRow1 + imageHeight;
            ImgCol2 = ImgCol1 + imageWidth;
 
            midPointX = ImgCol1;
            midPointY = ImgRow1;
 
            zoomWndFactor = (double)imageWidth / viewPort.Width;
            zoomImage(midPointX, midPointY, scaleFactor);
        }
 
 
        /// <summary>
        /// Scales the HALCON window according to the value scale
        /// </summary>
        public void scaleWindow(double scale)
        {
            ImgRow1 = 0;
            ImgCol1 = 0;
 
            ImgRow2 = imageHeight;
            ImgCol2 = imageWidth;
 
            viewPort.Width = (int)(ImgCol2 * scale);
            viewPort.Height = (int)(ImgRow2 * scale);
 
            zoomWndFactor = ((double)imageWidth / viewPort.Width);
        }
 
        /// <summary>
        /// Recalculates the image-window-factor, which needs to be added to 
        /// the scale factor for zooming an image. This way the zoom gets 
        /// adjusted to the window-image relation, expressed by the equation 
        /// imageWidth/viewPort.Width.
        /// </summary>
        public void setZoomWndFactor()
        {
            zoomWndFactor = ((double)imageWidth / viewPort.Width);
        }
 
        /// <summary>
        /// Sets the image-window-factor to the value zoomF
        /// </summary>
        public void setZoomWndFactor(double zoomF)
        {
            zoomWndFactor = zoomF;
        }
 
        /*******************************************************************/
        private void moveImage(double motionX, double motionY)
        {
            ImgRow1 += -motionY;
            ImgRow2 += -motionY;
 
            ImgCol1 += -motionX;
            ImgCol2 += -motionX;
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)Math.Round(ImgCol1);
            rect.Y = (int)Math.Round(ImgRow1);
            viewPort.ImagePart = rect;
 
            repaint();
        }
 
 
        /// <summary>
        /// Resets all parameters that concern the HALCON window display 
        /// setup to their initial values and clears the ROI list.
        /// </summary>
        public void resetAll()
        {
            ImgRow1 = 0;
            ImgCol1 = 0;
            ImgRow2 = imageHeight;
            ImgCol2 = imageWidth;
 
            zoomWndFactor = (double)imageWidth / viewPort.Width;
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)ImgCol1;
            rect.Y = (int)ImgRow1;
            rect.Width = (int)imageWidth;
            rect.Height = (int)imageHeight;
            viewPort.ImagePart = rect;
 
 
            if (roiManager != null)
                roiManager.reset();
        }
 
        public void resetWindow()
        {
            ImgRow1 = 0;
            ImgCol1 = 0;
            ImgRow2 = imageHeight;
            ImgCol2 = imageWidth;
 
            zoomWndFactor = (double)imageWidth / viewPort.Width;
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)ImgCol1;
            rect.Y = (int)ImgRow1;
            rect.Width = (int)imageWidth;
            rect.Height = (int)imageHeight;
            viewPort.ImagePart = rect;
        }
 
 
        public double CurX=0.0;
        public double CurY = 0.0;
        /*************************************************************************/
        /*                   Event handling for mouse                                */
        /*************************************************************************/
        private void mouseDown(object sender, HMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                mousePressed = true;
            int activeROIidx = -1;
            double scale;
            
            if (roiManager != null && (dispROI == MODE_INCLUDE_ROI))
            {
                activeROIidx = roiManager.mouseDownAction(e, e.X, e.Y);
            }
 
            if (activeROIidx == -1)
            {
                switch (stateView)
                {
                    case MODE_VIEW_MOVE:
                        startX = e.X;
                        startY = e.Y;
                        break;
                    case MODE_VIEW_ZOOM:
                        if (e.Button == System.Windows.Forms.MouseButtons.Left)
                            scale = 0.9;
                        else
                            scale = 1 / 0.9;
                        zoomImage(e.X, e.Y, scale);
                        break;
                    case MODE_VIEW_NONE:
                        break;
                    case MODE_VIEW_ZOOMWINDOW:
                        activateZoomWindow((int)e.X, (int)e.Y);
                        break;
                    default:
                        break;
                }
            }
        }
       
        /*******************************************************************/
        private void activateZoomWindow(int X, int Y)
        {
            double posX, posY;
            int zoomZone;
 
            if (ZoomWindow != null)
                ZoomWindow.Dispose();
 
            HOperatorSet.SetSystem("border_width", 10);
            ZoomWindow = new HWindow();
 
            posX = ((X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width;
            posY = ((Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height;
 
            zoomZone = (int)((zoomWndSize / 2) * zoomWndFactor * zoomAddOn);
            ZoomWindow.OpenWindow((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2),
                                   zoomWndSize, zoomWndSize,
                                   viewPort.HalconID, "visible", "");
            ZoomWindow.SetPart(Y - zoomZone, X - zoomZone, Y + zoomZone, X + zoomZone);
            repaint(ZoomWindow);
            ZoomWindow.SetColor("black");
        }
 
        /*******************************************************************/
        private void mouseUp(object sender, HalconDotNet.HMouseEventArgs e)
        {
            mousePressed = false;
 
            if (roiManager != null
                && (roiManager.activeROIidx != -1)
                && (dispROI == MODE_INCLUDE_ROI))
            {
                roiManager.NotifyRCObserver(ROIController.EVENT_UPDATE_ROI);
            }
            else if (stateView == MODE_VIEW_ZOOMWINDOW)
            {
                ZoomWindow.Dispose();
            }
        }
 
        /*******************************************************************/
        private void mouseMoved(object sender, HalconDotNet.HMouseEventArgs e)
        {
            double motionX, motionY;
            double posX, posY;
            double zoomZone;
 
            if ((e.X > 0) && (e.Y > 0))
            {
                CurX = e.X;
                CurY = e.Y;
            }
            
            if (!mousePressed)
                return;
 
            if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI))
            {
                roiManager.mouseMoveAction(e.X, e.Y);
            }
            else if (stateView == MODE_VIEW_MOVE)
            {
                motionX = ((e.X - startX));
                motionY = ((e.Y - startY));
 
                if (((int)motionX != 0) || ((int)motionY != 0))
                {
                    moveImage(motionX, motionY);
                    startX = e.X - motionX;
                    startY = e.Y - motionY;
                }
            }
            else if (stateView == MODE_VIEW_ZOOMWINDOW)
            {
                HSystem.SetSystem("flush_graphic", "false");
                ZoomWindow.ClearWindow();
 
 
                posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width;
                posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height;
                zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn;
 
                ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2),
                                            (int)posX - (zoomWndSize / 2),
                                            zoomWndSize, zoomWndSize);
                ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone),
                                   (int)(e.Y + zoomZone), (int)(e.X + zoomZone));
                repaint(ZoomWindow);
 
                HSystem.SetSystem("flush_graphic", "true");
                ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0);
            }
        }
 
        /// <summary>
        /// To initialize the move function using a GUI component, the HWndCtrl
        /// first needs to know the range supplied by the GUI component. 
        /// For the x direction it is specified by xRange, which is 
        /// calculated as follows: GuiComponentX.Max()-GuiComponentX.Min().
        /// The starting value of the GUI component has to be supplied 
        /// by the parameter Init
        /// </summary>
        public void setGUICompRangeX(int[] xRange, int Init)
        {
            int cRangeX;
 
            CompRangeX = xRange;
            cRangeX = xRange[1] - xRange[0];
            prevCompX = Init;
            stepSizeX = ((double)imageWidth / cRangeX) * (imageWidth / windowWidth);
 
        }
 
        /// <summary>
        /// To initialize the move function using a GUI component, the HWndCtrl
        /// first needs to know the range supplied by the GUI component. 
        /// For the y direction it is specified by yRange, which is 
        /// calculated as follows: GuiComponentY.Max()-GuiComponentY.Min().
        /// The starting value of the GUI component has to be supplied 
        /// by the parameter Init
        /// </summary>
        public void setGUICompRangeY(int[] yRange, int Init)
        {
            int cRangeY;
 
            CompRangeY = yRange;
            cRangeY = yRange[1] - yRange[0];
            prevCompY = Init;
            stepSizeY = ((double)imageHeight / cRangeY) * (imageHeight / windowHeight);
        }
 
        public void SetLineWidth(int value)
        {
            viewPort.HalconWindow.SetLineWidth(value);
        }
 
        /// <summary>
        /// Resets to the starting value of the GUI component.
        /// </summary>
        public void resetGUIInitValues(int xVal, int yVal)
        {
            prevCompX = xVal;
            prevCompY = yVal;
        }
 
        /// <summary>
        /// Moves the image by the value valX supplied by the GUI component
        /// </summary>
        public void moveXByGUIHandle(int valX)
        {
            double motionX;
 
            motionX = (valX - prevCompX) * stepSizeX;
 
            if (motionX == 0)
                return;
 
            moveImage(motionX, 0.0);
            prevCompX = valX;
        }
 
 
        /// <summary>
        /// Moves the image by the value valY supplied by the GUI component
        /// </summary>
        public void moveYByGUIHandle(int valY)
        {
            double motionY;
 
            motionY = (valY - prevCompY) * stepSizeY;
 
            if (motionY == 0)
                return;
 
            moveImage(0.0, motionY);
            prevCompY = valY;
        }
 
        /// <summary>
        /// Zooms the image by the value valF supplied by the GUI component
        /// </summary>
        public void zoomByGUIHandle(double valF)
        {
            double x, y, scale;
            double prevScaleC;
 
 
            x = (ImgCol1 + (ImgCol2 - ImgCol1) / 2);
            y = (ImgRow1 + (ImgRow2 - ImgRow1) / 2);
 
            prevScaleC = (double)((ImgCol2 - ImgCol1) / imageWidth);
            scale = ((double)1.0 / prevScaleC * (100.0 / valF));
 
            zoomImage(x, y, scale);
        }
 
        /// <summary>
        /// Triggers a repaint of the HALCON window
        /// </summary>
        public void repaint()
        {
            repaint(viewPort.HalconWindow);
        }
 
        /// <summary>
        /// Repaints the HALCON window 'window'
        /// </summary>
        public void repaint(HWindow window)
        {
            window.SetColored(12);
            int count = HObjList.Count;
            HObjectEntry entry;
 
            HSystem.SetSystem("flush_graphic", "false");
 
            Thread.Sleep(10);
 
            window.ClearWindow();
            //mGC.stateOfSettings.Clear();
 
            for (int i = 0; i < count; i++)
            {
                entry = ((HObjectEntry)HObjList[i]);
                //mGC.applyContext(window, entry.gContext);
                if (entry.Type == HobjType.REGION)
                {
                    window.SetColor(entry.ColorCode);
                }
                    
                if (entry.HObj.IsInitialized())
                    window.DispObj(entry.HObj);
            }
 
            addInfoDelegate();
 
            if (roiManager != null && (dispROI == MODE_INCLUDE_ROI))
                roiManager.paintData(window);
 
            HSystem.SetSystem("flush_graphic", "true");
            
            window.DispLine(-100.0, -100.0, -101.0, -101.0);
 
        }
 
        public void ClearHObjListindex(int X)
        {
            if (HObjList.Count > X)
                HObjList.RemoveAt(X);
        }
        public void ClearHObjListExceptImage()
        {
            if (HObjList.Count > 1)
            {
 
                HObjList.RemoveRange(1, HObjList.Count - 1);
 
            }
        }
 
        /// <summary>
        /// Clears all entries from the graphics stack 
        /// </summary>
        public void ClearHObjList()
        {
            HObjList.Clear();
        }
 
        /// <summary>
        /// Returns the number of items on the graphics stack
        /// </summary>
        public int getListCount()
        {
            return HObjList.Count;
        }
 
        /// <summary>
        /// Changes the current graphical context by setting the specified mode
        /// (constant starting by GC_*) to the specified value.
        /// </summary>
        /// <param name="mode">
        /// Constant that is provided by the class GraphicsContext
        /// and describes the mode that has to be changed, 
        /// e.g., GraphicsContext.GC_COLOR
        /// </param>
        /// <param name="val">
        /// Value, provided as a string, 
        /// the mode is to be changed to, e.g., "blue" 
        /// </param>
        public void changeGraphicSettings(string mode, string val)
        {
            switch (mode)
            {
                case GraphicsContext.GC_COLOR:
                    mGC.setColorAttribute(val);
                    break;
                case GraphicsContext.GC_DRAWMODE:
                    mGC.setDrawModeAttribute(val);
                    break;
                case GraphicsContext.GC_LUT:
                    mGC.setLutAttribute(val);
                    break;
                case GraphicsContext.GC_PAINT:
                    mGC.setPaintAttribute(val);
                    break;
                case GraphicsContext.GC_SHAPE:
                    mGC.setShapeAttribute(val);
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// Changes the current graphical context by setting the specified mode
        /// (constant starting by GC_*) to the specified value.
        /// </summary>
        /// <param name="mode">
        /// Constant that is provided by the class GraphicsContext
        /// and describes the mode that has to be changed, 
        /// e.g., GraphicsContext.GC_LINEWIDTH
        /// </param>
        /// <param name="val">
        /// Value, provided as an integer, the mode is to be changed to, 
        /// e.g., 5 
        /// </param>
        public void changeGraphicSettings(string mode, int val)
        {
            switch (mode)
            {
                case GraphicsContext.GC_COLORED:
                    mGC.setColoredAttribute(val);
                    break;
                case GraphicsContext.GC_LINEWIDTH:
                    mGC.setLineWidthAttribute(val);
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// Changes the current graphical context by setting the specified mode
        /// (constant starting by GC_*) to the specified value.
        /// </summary>
        /// <param name="mode">
        /// Constant that is provided by the class GraphicsContext
        /// and describes the mode that has to be changed, 
        /// e.g.,  GraphicsContext.GC_LINESTYLE
        /// </param>
        /// <param name="val">
        /// Value, provided as an HTuple instance, the mode is 
        /// to be changed to, e.g., new HTuple(new int[]{2,2})
        /// </param>
        public void changeGraphicSettings(string mode, HTuple val)
        {
            switch (mode)
            {
                case GraphicsContext.GC_LINESTYLE:
                    mGC.setLineStyleAttribute(val);
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// Clears all entries from the graphical context list
        /// </summary>
        public void clearGraphicContext()
        {
            mGC.clear();
        }
 
        /// <summary>
        /// Returns a clone of the graphical context list (hashtable)
        /// </summary>
        public Hashtable getGraphicContext()
        {
            return mGC.copyContextList();
        }
 
        private int widthImagePart;
        private int heightImagePart;
        // width and height of the current image, after applying 
        // manipulation (zooming)
        private int widthDisplayedImage;
        private int heightDisplayedImage;
        // width and height of current image
 
        /* Initial image coordinates, which describe the image part that is displayed  
        in the HALCON window by adding new image to graphic stack */
        private double InitImgRow1, InitImgCol1, InitImgRow2, InitImgCol2;
 
 
        /// <summary>
        /// This event is fired, if the displayed iconic objects 
        /// were moved
        /// </summary>
        public event OnIconicObjectMovedHandler OnImageMoved;
 
        /// <summary>
        /// This event is fired, if the displayed iconic objects 
        /// was zoomed
        /// </summary>
        public event OnIconicObjectZoomedHandler OnImageZoomed;
 
        /// <summary>
        /// boolean variable set to false if image size shall not change with window size
        /// </summary>
        public bool adaptSize;
 
        /// <summary>
        /// initialize the variables, that are used for image part descriotion, 
        /// with the initial values of HWindowControl
        /// </summary>
        private void initializeDisplayImagePart()
        {
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            ImgCol1 = rect.X;
            ImgRow1 = rect.Y;
            ImgRow2 = rect.Height - 1;
            ImgCol2 = rect.Width - 1;
 
            widthImagePart = Convert.ToInt32(Math.Ceiling(ImgCol2 - ImgCol1));
            heightImagePart = Convert.ToInt32(Math.Ceiling(ImgRow2 - ImgRow1));
        }
 
 
        /// <summary>
        /// The Properties define the actual magnification value or diminution 
        /// factor of the image displayed in the graphic window in per cent [%]
        /// </summary>
        public double ZoomFactor
        {
            get
            {
                return (this.zoomWndFactor * 100.0);
            }
        }
 
        public int ShowROI
        {
            get
            {
                return dispROI;
            }
            set
            {
                if ((value == MODE_INCLUDE_ROI) ||
                    (value == MODE_EXCLUDE_ROI))
                    dispROI = value;
            }
        }
 
        // MODIFIED
        /// <summary>
        /// get window width
        /// </summary>
        public void getWindowSize()
        {
            windowWidth = viewPort.Size.Width;
            windowHeight = viewPort.Size.Height;
        }
 
        public void setZoomWndFactor2(double imageWidth0)
        {
            zoomWndFactor = ((double)imageWidth0 / viewPort.Width);
        }
 
        /// <summary>
        /// Move image according the displacement values given in 
        /// motionX and motionY
        /// </summary>
        public void moveImageFromInit(double motionX, double motionY)
        {
            ImgRow1 = InitImgRow1 - motionY;
            ImgRow2 = InitImgRow2 - motionY;
 
            ImgCol1 = InitImgCol1 - motionX;
            ImgCol2 = InitImgCol2 - motionX;
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)Math.Round(ImgCol1);
            rect.Y = (int)Math.Round(ImgRow1);
            viewPort.ImagePart = rect;
        }
 
        /// <summary>
        /// Move image according the displacement and axis 
        /// </summary>
        public void moveImageFromInit(double motion, string axis)
        {
            if (axis == "y")
            {
                ImgRow1 = InitImgRow1 - motion;
                ImgRow2 = InitImgRow2 - motion;
            }
            else if (axis == "x")
            {
                ImgCol1 = InitImgCol1 - motion;
                ImgCol2 = InitImgCol2 - motion;
            }
            else
                throw new
                  ArgumentException("Invalid name of the parameter axis.",
                                     "axis");
 
            System.Drawing.Rectangle rect = viewPort.ImagePart;
            rect.X = (int)Math.Round(ImgCol1);
            rect.Y = (int)Math.Round(ImgRow1);
            viewPort.ImagePart = rect;
        }
 
        public void mouseWheel(object sender, HalconDotNet.HMouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                zoomImage(e.X, e.Y, 0.9);
                if (OnImageZoomed != null)
                {
                    OnImageZoomed(sender, e.X, e.Y, 0.9);
                }
            }
 
            else
            {
                zoomImage(e.X, e.Y, 1.1);
                if (OnImageZoomed != null)
                {
                    OnImageZoomed(sender, e.X, e.Y, 1.1);
                }
            }
        }
 
        /// <summary>
        /// Zooms the image by the value valF supplied by the GUI component
        /// </summary>
        public void zoomByGUIHandleInitial(double valF, double prevScaleC)
        {
            double x, y, scale;
            //double prevScaleC;
 
 
            x = (ImgCol1 + (ImgCol2 - ImgCol1) / 2);
            y = (ImgRow1 + (ImgRow2 - ImgRow1) / 2);
 
            //prevScaleC = (double)((ImgCol2 - ImgCol1) / imageWidth);
            scale = ((double)1.0 / prevScaleC * (100.0 / valF));
 
            zoomImage(x, y, scale);
        }
 
        /********************************************************************/
        /*                      GRAPHICSSTACK     -KEEP                     */
        /********************************************************************/
        /// <summary>
        /// Adds an iconic object to the graphics stack similar to the way
        /// it is defined for the HDevelop graphics stack.
        /// </summary>
        /// <param name="obj">Iconic object</param>
        public void addIconicVarKeepSettings(HObject obj)
        {
            HObjectEntry entry;
 
            if (obj == null)
                return;
 
            if ((obj is HImage) && (obj != null))
            {
                double r, c;
                int h, w, area;
                string s;
 
                area = ((HImage)obj).GetDomain().AreaCenter(out r, out c);
                ((HImage)obj).GetImagePointer1(out s, out w, out h);
 
                if (HObjList.Count == 0)
                {
                    heightImagePart = h;
                    widthImagePart = w;
                    adaptSize = true;
 
                    zoomWndFactor = (double)widthDisplayedImage / viewPort.Width;
 
                    resetImagePart(widthImagePart, heightImagePart);
                }
 
                if (area == (w * h))
                {
                    ClearHObjList();
 
                    if ((h != heightImagePart) || (w != widthImagePart))
                    {
                        heightImagePart = h;
                        widthImagePart = w;
 
                        if ((widthDisplayedImage != w) || (heightDisplayedImage != h))
                            resetImagePart(w, h);
 
                        zoomWndFactor = (double)widthDisplayedImage / viewPort.Width;
                    }
                }//if
                widthDisplayedImage = w;
                heightDisplayedImage = h;
            }//if
            entry = new HObjectEntry(obj, mGC.copyContextList(),HobjType.IMAGE);
            
            HObjList.Add(entry);
 
            if (HObjList.Count > MAXNUMOBJLIST)
                HObjList.RemoveAt(1);
        }
 
 
        /// <summary>
        /// Set the displayed image part of the 
        /// </summary>
        public void resetImagePart(int imageWidth0, int imageHeight0)
        {
            getWindowSize();
            if (adaptSize == true)
            {
                setImagePartAdaptSize(imageWidth0, imageHeight0);
            }
            else
            {
                setImagePartConst(imageWidth0, imageHeight0);
            }
        }
 
        // MODIFIED
 
        /// <summary>
        /// calculate and set image part according new dimensions of HalconWindow
        /// (the image is displayed with correct aspect ratio and one side is scaled to the 
        /// corresponding window size)
        /// </summary>
        /// <param name="imageWidth0"></param>
        /// <param name="imageHeight0"></param>
        public void setImagePartAdaptSize(int imWidth, int imHeight)
        {
            //
            // fit to best size with aspect ratio 1:1
            //
            int r1, r2, c1, c2;
 
            // The image part is calculated, so the displayed image is adapted
            // to the window size, but the aspect ratio of the image
            // is kept. 
            // The following condition decides, if the height or width
            // of the displayed image is adapted to window height/width and 
            // the value of width/height will be calculated and adapted 
            // so the image is displayed in correct aspect ratio.
            if ((windowWidth == 0) || (windowHeight == 0))
                return;
 
            if (1.0 * imWidth / imHeight < 1.0 * windowWidth / windowHeight)
            {
                // scale imageHeight to windowHeight
                c1 = (int)(-0.5 * (1.0 * imHeight * windowWidth / windowHeight - imWidth));
                c2 = (int)(imWidth + Math.Abs(c1) - 1);
                r1 = 0;
                r2 = imHeight - 1;
            }
            else
            {
                // scale imageWidth to windowWidth
                r1 = (int)(-0.5 * (1.0 * imWidth * windowHeight / windowWidth - imHeight));
                r2 = (int)(imHeight + Math.Abs(r1) - 1);
                c1 = 0;
                c2 = imWidth - 1;
            }
 
            // Check, if the values of image part coordinates
            // have valid values
            if (c2 < c1)
            {
                c1 = 0;
                c2 = imWidth;
            }
            if (r2 < r1)
            {
                r1 = 0;
                r2 = imHeight;
            }
 
            // adapt the current image part to the 
            // new size of window
            System.Drawing.Rectangle rect = viewPort.ImagePart;
 
            rect.X = c1;
            rect.Y = r1;
 
            rect.Width = c2 - c1 + 1;
            rect.Height = r2 - r1 + 1;
 
            InitImgRow1 = ImgRow1 = rect.Y;
            InitImgCol1 = ImgCol1 = rect.X;
            InitImgRow2 = ImgRow2 = rect.Y + rect.Height;
            InitImgCol2 = ImgCol2 = rect.X + rect.Width;
 
 
            viewPort.ImagePart = rect;
 
            heightImagePart = rect.Height;
            widthImagePart = rect.Width;
 
            zoomWndFactor = (double)widthImagePart / viewPort.Width;
            
        }
 
        // MODIFIED
 
        /// <summary>
        /// The image part is set so that the image size remains constant 
        /// independent from the window size (image is not scaled)
        /// </summary>
        /// <param name="imageWidth0"></param>
        /// <param name="imageHeight0"></param>
        public void setImagePartConst(int imWidth, int imHeight)
        {
            //
            // fit to best size with aspect ratio 1:1
            //
            int r1, r2, c1, c2;
 
            /*calculate the coordinates for displaying the image 
             * centered in the Halcon window*/
            c1 = (int)(0.5 * (imWidth - windowWidth));
            c2 = imWidth - c1;
            r1 = (int)(0.5 * (imHeight - windowHeight));
            r2 = imHeight - r1;
 
 
            // Check, if the values of image part coordinates
            // have valid values
            if (c2 <= c1)
            {
                c1 = 0;
                c2 = imWidth;
            }
            if (r2 <= r1)
            {
                r1 = 0;
                r2 = imHeight;
            }
 
 
            /* Assign the calculated values to image part of Halcon window*/
            System.Drawing.Rectangle rect = viewPort.ImagePart;
 
            rect.X = c1;
            rect.Y = r1;
            rect.Width = c2 - c1;
            rect.Height = r2 - r1 + 1;
 
            // set the image part values to member variables
            InitImgRow1 = ImgRow1 = rect.Y;
            InitImgCol1 = ImgCol1 = rect.X;
            InitImgRow2 = ImgRow2 = rect.Y + rect.Height;
            InitImgCol2 = ImgCol2 = rect.X + rect.Width;
 
            viewPort.ImagePart = rect;
 
            heightImagePart = rect.Height;
            widthImagePart = rect.Width;
 
            zoomWndFactor = (double)widthImagePart / viewPort.Width;
        }
 
    }//end of class
}//end of namespace