class MyForm1 : Form { AnnContainer container; RasterImageViewer viewer; RasterCodecs codecs; AnnDesigner currentDesigner; public MyForm1(string title) { Text = title; Size = new Size(500, 200); viewer = new RasterImageViewer(); viewer.TransformChanged += new EventHandler(viewer_TransformChanged); viewer.PostImagePaint += new PaintEventHandler(viewer_PostImagePaint); viewer.MouseDown += new MouseEventHandler(viewer_MouseDown); viewer.MouseMove += new MouseEventHandler(viewer_MouseMove); viewer.MouseUp += new MouseEventHandler(viewer_MouseUp); viewer.LostFocus += new EventHandler(viewer_LostFocus); // load an image into the viewer RasterCodecs.Startup(); codecs = new RasterCodecs(); string fileName = LeadtoolsExamples.Common.ImagesPath.Path + "image1.cmp"; viewer.Image = codecs.Load(fileName); RasterCodecs.Shutdown(); // create and set up the container container = new AnnContainer(); container.Bounds = new AnnRectangle(0, 0, viewer.ImageSize.Width, viewer.ImageSize.Height); container.UnitConverter = new AnnUnitConverter(viewer.ImageDpiX, viewer.ImageDpiY); // Add the viewer viewer.Dock = DockStyle.Fill; Controls.Add(viewer); viewer.BringToFront(); // add a few objects to the container AnnRectangleObject rectObj = new AnnRectangleObject(); rectObj.Bounds = new AnnRectangle(100, 100, 200, 200, AnnUnit.Pixel); rectObj.Pen = new AnnPen(Color.Blue, new AnnLength(2, AnnUnit.Pixel)); rectObj.Brush = new AnnSolidBrush(Color.Yellow); container.Objects.Add(rectObj); AnnButtonObject buttonObj = new AnnButtonObject(); buttonObj.Bounds = new AnnRectangle(100, 320, 200, 24, AnnUnit.Pixel); buttonObj.Text = "Goto Leadtools website"; buttonObj.TextColor = Color.Black; buttonObj.Font = new AnnFont("Arial", new AnnLength(8, AnnUnit.Point), FontStyle.Regular); buttonObj.Hyperlink = @"http://www.leadtools.com"; container.Objects.Add(buttonObj); } private void viewer_TransformChanged(object sender, EventArgs e) { // set up the container transformation if(viewer.Image != null && container != null) container.Transform = viewer.Transform.Clone(); } private void viewer_PostImagePaint(object sender, PaintEventArgs e) { // draw the container and its objects on this viewer if(viewer.Image != null && container != null) container.Draw(e.Graphics); } private void viewer_MouseDown(object sender, MouseEventArgs e) { bool handled = false; // see if a designer is currently running, if so, let it handle this event if(currentDesigner != null) handled = currentDesigner.MouseDown(e); if(!handled) { // the mouse click was not handled by a designer // check if the click was on top of an existing object that we can start running AnnPoint pt = new AnnPoint(e.X, e.Y); AnnObject obj = container.HitTest(pt, 2); if(obj != null) { // yes, start the run designer for this object if(obj is AnnButtonObject) { AnnButtonRunDesigner buttonRunDesigner = new AnnButtonRunDesigner(); StartRunning(buttonRunDesigner, obj, e); } else { AnnRunDesigner runDesigner = new AnnRunDesigner(); StartRunning(runDesigner, obj, e); } } } } private void StartRunning(AnnRunDesigner runDesigner, AnnObject obj, MouseEventArgs e) { // set up the current designer runDesigner.Run += new EventHandler<AnnRunDesignerEventArgs>(OnDesignerRun); runDesigner.Owner = viewer; runDesigner.Container = container; runDesigner.ClipCursor = true; runDesigner.Object = obj; runDesigner.HitTestBuffer = 2; runDesigner.Start(); currentDesigner = runDesigner; currentDesigner.MouseDown(e); } private void OnDesignerRun(object sender, AnnRunDesignerEventArgs e) { // show information on the current edit operation Console.WriteLine("Object: {0}, Status: {1}, ", e.Object.GetType().Name, e.OperationStatus); if(e.OperationStatus == AnnDesignerOperationStatus.End) { // check if the object does not have a hyperlink, if so, show a message box if(e.Object.Hyperlink == null || e.Object.Hyperlink == string.Empty) MessageBox.Show(String.Format("You clicked an object of type {0} that does not have a hyperlink", e.Object.GetType().Name)); } } private void viewer_MouseMove(object sender, MouseEventArgs e) { bool handled = false; // see if a designer is currently running, if so, let it handle this event if(currentDesigner != null) handled = currentDesigner.MouseMove(e); } private void viewer_MouseUp(object sender, MouseEventArgs e) { bool handled = false; // see if a designer is currently running, if so, let it handle this event if(currentDesigner != null) handled = currentDesigner.MouseUp(e); } private void viewer_LostFocus(object sender, EventArgs e) { // see if a designer is currently running, if so, cancel it if(currentDesigner != null) currentDesigner.Cancel(); } } public void AnnRunDesigner_AnnRunDesigner(string title) { MyForm1 form = new MyForm1(title); form.ShowDialog(); } |