/// <summary> /// This raises the image area <see cref="DrawImage"/> event /// </summary> /// <param name="e">The event arguments</param> /// <include file="IMExamples.xml" path="Examples/ImageMap/HelpEx[@name='Ex4']/*" /> protected virtual void OnDrawImage(DrawImageEventArgs e) { ImageAreaCircle c; var handler = DrawImage; if (handler != null) { handler(this, e); } else { Rectangle r; switch (this.Shape) { case ImageAreaShape.Rectangle: r = ((ImageAreaRectangle)this).Rectangle; break; case ImageAreaShape.Ellipse: r = ((ImageAreaEllipse)this).Ellipse; break; case ImageAreaShape.Circle: c = (ImageAreaCircle)this; r = new Rectangle(c.CenterPoint, new Size(0, 0)); r.Width = r.Height = c.Radius * 2; r.X -= r.Width / 2; r.Y -= r.Height / 2; break; default: // Polygon, use first point and a fixed size r = new Rectangle(((ImageAreaPolygon)this).Points[0], new Size(100, 100)); break; } // Add the offset r.X += e.ImageOffset.X; r.Y += e.ImageOffset.Y; using (Font font = new Font("Microsoft Sans Serif", 7.8f)) { e.Graphics.FillRectangle(Brushes.Beige, r); e.Graphics.DrawString("ImageArea: DrawImageEvent not implemented", font, Brushes.Black, r.X, r.Y); } } }
/// <summary> /// Draw the "Visit web site" image area /// </summary> /// <param name="sender">The sender of the event (the image map or image area in this demo)</param> /// <param name="e">The event arguments</param> private void VisitWebSite_DrawImage(object sender, DrawImageEventArgs e) { Font f = imMap.Font; // Use image map font if not focused ImageMap m = (sender as ImageMap); if(m != null) { // If this area is focused or contains the mouse, underline the hyperlink text if(m.FocusedArea == 3 || m.MouseArea == 3) { f = hyperlinkFont; // Tell the image map not to draw the focus. It's true by default in the image map's owner // draw event. if(m.FocusedArea == 3) e.DrawFocus = false; } } else { // If the image area is drawing itself, use the event arguments to determine the draw state. If // hot lighting or focused, use the underlined font. if(e.DrawState == DrawState.HotLight || e.DrawState == DrawState.Focus) f = hyperlinkFont; } Rectangle r = areaVisitWebSite.Rectangle; // Offset the area rectangle by the draw event offset r.Offset(e.ImageOffset.X, e.ImageOffset.Y); e.Graphics.DrawString("https://github.com/EWSoftware/ImageMaps", f, Brushes.Blue, r, sfFormat); r.Width = (int)e.Graphics.MeasureString("Double-click to go to ", imMap.Font).Width + 4; r.X = r.X - r.Width + 10; e.Graphics.DrawString("&Double-click to go to ", imMap.Font, Brushes.Black, r, sfFormat); }
/// <summary> /// Draw the "Owner Draw" toggle image area /// </summary> /// <param name="sender">The sender of the event (the image map or image area in this demo)</param> /// <param name="e">The event arguments</param> private void OwnerDrawOnOff_DrawImage(object sender, DrawImageEventArgs e) { Rectangle r = areaOwnerDrawOnOff.Rectangle; r.Inflate(-2, -2); // Offset the area rectangle by the draw event offset r.Offset(e.ImageOffset.X, e.ImageOffset.Y); if(sender is ImageMap) { ControlPaint.DrawCheckBox(e.Graphics, r.X + 5, r.Y, r.Height, r.Height, ButtonState.Checked); r.X += 5 + r.Height; r.Width -= 5 + r.Height; // Use the image map's font e.Graphics.DrawString("&Image map owner draw ON", imMap.Font, Brushes.Black, r, sfFormat); } else { ControlPaint.DrawCheckBox(e.Graphics, r.X + 5, r.Y, r.Height, r.Height, ButtonState.Normal); r.X += 5 + r.Height; r.Width -= 5 + r.Height; e.Graphics.DrawString("&Image map owner draw OFF", imMap.Font, Brushes.Black, r, sfFormat); // Tell the image map to draw the focus rectangle if this is the focused area. It's false by // default if the area is drawing itself. e.DrawFocus = true; } }
/// <summary> /// Draw the mouse events image area /// </summary> /// <param name="sender">The sender of the event (the image area)</param> /// <param name="e">The event arguments</param> private void MouseArea_DrawImage(object sender, DrawImageEventArgs e) { Graphics g = e.Graphics; ImageAreaCircle a = (ImageAreaCircle)sender; int nShineOffset = a.Radius / 2; Rectangle r = new Rectangle(a.CenterPoint.X - a.Radius, a.CenterPoint.Y - a.Radius, a.Radius * 2, a.Radius * 2); r.Inflate(-2, -2); // Offset the area rectangle by the draw event offset r.Offset(e.ImageOffset.X, e.ImageOffset.Y); using(GraphicsPath pth = new GraphicsPath()) { pth.AddEllipse(r); using(PathGradientBrush pgb = new PathGradientBrush(pth)) { pgb.CenterColor = Color.White; pgb.SurroundColors = new Color [] { Color.BurlyWood }; Point shine = new Point(a.CenterPoint.X - nShineOffset, a.CenterPoint.Y - nShineOffset); shine.Offset(e.ImageOffset.X, e.ImageOffset.Y); pgb.CenterPoint = shine; g.FillEllipse(pgb, r); g.DrawString((string)a.Tag, buttonFont, Brushes.Black, r, sfFormat); } } // We'll let the image map draw the focus frame when needed e.DrawFocus = true; }
/// <summary> /// When the image map is owner drawn, you can draw just the background /// or you can draw the image areas too. /// </summary> /// <param name="sender">The sender of the event (the image map)</param> /// <param name="e">The event arguments</param> private void imMap_DrawImage(object sender, DrawImageEventArgs e) { // For this demo, we'll just draw a gradient fill background. // You could get the image using the ImageMap.Image property too. // Offset the drawing rectangle by the image offset in the event // arguments. This indicates the true position of the image when // centered or scrolled. Rectangle r = new Rectangle(e.ImageOffset.X, e.ImageOffset.Y, imMap.ImageMapWidth, imMap.ImageMapHeight); using(LinearGradientBrush lgBrush = new LinearGradientBrush(r, Color.White, Color.SteelBlue, LinearGradientMode.ForwardDiagonal)) { e.Graphics.FillRectangle(lgBrush, r); } e.Graphics.DrawString("All items seen on this form are drawn by " + "the image map or image area DrawImage event handlers. " + "Even with owner draw disabled for the image map, image " + "areas can still be set to owner drawn.", imMap.Font, Brushes.Black, new Rectangle(r.X + 250, r.Y + 10, 325, 100), sfFormat); // The image map can draw areas too. In this case, we defer to // the event handlers as they'll take over if owner drawing of the // image map is turned off. this.OwnerDrawOnOff_DrawImage(sender, e); this.VisitWebSite_DrawImage(sender, e); }
/// <summary> /// Draw the focus enter/leave image area /// </summary> /// <param name="sender">The sender of the event (the image area)</param> /// <param name="e">The event arguments</param> private void FocusArea_DrawImage(object sender, DrawImageEventArgs e) { Graphics g = e.Graphics; ImageAreaPolygon a = (ImageAreaPolygon)sender; Rectangle r = a.BoundingRectangle; // Offset the area rectangle by the draw event offset r.Offset(e.ImageOffset.X, e.ImageOffset.Y); Point[] pts = new Point[a.Points.Count]; a.Points.CopyTo(pts, 0); using(PathGradientBrush pgb = new PathGradientBrush(pts)) { // Translate the brush coordinates to account for the offset using(Matrix m = new Matrix()) { m.Translate((float)e.ImageOffset.X, (float)e.ImageOffset.Y); pgb.Transform = m; if(e.DrawState == DrawState.Focus) pgb.SurroundColors = new Color[] { Color.DarkOrange }; else pgb.SurroundColors = new Color[] { Color.Navy }; pgb.CenterColor = Color.Gray; g.FillRectangle(pgb, r); r.Y += 20; g.DrawString((string)a.Tag, buttonFont, Brushes.Yellow, r, sfFormat); } } }
/// <summary> /// Draw the "button" image areas /// </summary> /// <param name="sender">The sender of the event (the image area)</param> /// <param name="e">The event arguments</param> private void Button_DrawImage(object sender, DrawImageEventArgs e) { ImageAttributes ia; Graphics g = e.Graphics; // All are ellipse image areas ImageAreaEllipse a = (ImageAreaEllipse)sender; Rectangle r = a.Ellipse; // Offset the area rectangle by the draw event offset r.Offset(e.ImageOffset.X, e.ImageOffset.Y); if(!a.Enabled) ia = iaDisabled; else ia = iaNormal; using(TextureBrush tb = new TextureBrush(imgFiller, new Rectangle(0, 0, imgFiller.Width, imgFiller.Height), ia)) { tb.WrapMode = WrapMode.Tile; // Translate the brush coordinates to account for the offset using(Matrix m = new Matrix()) { m.Translate((float)r.X, (float)r.Y); tb.Transform = m; // If the area is focused or hot lighted, give it a glow effect if(e.DrawState == DrawState.Focus || e.DrawState == DrawState.HotLight) { using(GraphicsPath pth = new GraphicsPath()) { pth.AddEllipse(r); using(PathGradientBrush pgb = new PathGradientBrush(pth)) { pgb.CenterColor = Color.LightSteelBlue; if(e.DrawState == DrawState.Focus) pgb.SurroundColors = new Color[] { Color.Yellow }; else pgb.SurroundColors = new Color[] { Color.Blue }; pgb.FocusScales = new PointF(0.8f, 0.8f); g.FillEllipse(pgb, r); } } } // Draw the filler g.FillEllipse(tb, r); g.DrawString((string)a.Tag, buttonFont, Brushes.Black, r, sfFormat); } } }
/// <summary> /// This raises the image map <see cref="DrawImage"/> event /// </summary> /// <param name="e">The event arguments</param> /// <include file="IMExamples.xml" path="Examples/ImageMap/HelpEx[@name='Ex5']/*" /> protected virtual void OnDrawImage(DrawImageEventArgs e) { var handler = DrawImage; if(handler != null) handler(this, e); else { e.Graphics.FillRectangle(Brushes.White, this.ClientRectangle); e.Graphics.DrawString("ImageMap: DrawImageEvent not implemented", base.Font, Brushes.Black, 0, 0); } }
/// <summary> /// This is overridden to display the image map /// </summary> /// <remarks>It fires the <see cref="DrawImage"/> event for the image map if it is owner drawn and the /// <see cref="ImageAreaBase.DrawImage"/> event for any owner drawn image areas.</remarks> /// <param name="e">The event arguments</param> protected override void OnPaint(PaintEventArgs e) { ImageAreaBase a; bool drawFrame = true, designMode = this.DesignMode, focused = this.Focused, enabled = this.Enabled; Graphics g = e.Graphics; Point p = CalculateImageOffset(); DrawImageEventArgs drawArgs = new DrawImageEventArgs(g, DrawState.Normal, p, true); // Set the image area paths if they need recalculating if(pathData.PointCount == 0) this.SetImageAreaPaths(); // Image animation is supported even for owner drawn image maps. The owner draw event can get the // image and display it using the Image property. if(image != null) { this.Animate(); ImageAnimator.UpdateFrames(); } if(!ownerDrawn || designMode) { // The width and height properties are used in case custom values have been specified. if(image != null) if(this.Enabled) g.DrawImage(image, p.X, p.Y, this.ImageMapWidth, this.ImageMapHeight); else g.DrawImage(image, new Rectangle(p.X, p.Y, this.ImageMapWidth, this.ImageMapHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, iaDisabled); } else { // Set the drawing state if(!enabled) drawArgs.DrawState = DrawState.Disabled; else if(focused) drawArgs.DrawState = DrawState.Focus; OnDrawImage(drawArgs); // If the owner draw event handled the focus frame, we won't draw one on non-owner drawn image // areas. drawFrame = drawArgs.DrawFocus; } // Raise the DrawImage event for all owner drawn image areas. Image areas can be owner draw even if // the image map is not. if(!designMode) for(int idx = 0; idx < this.Areas.Count; idx++) { // Assume no focus frame on owner drawn image areas drawArgs.DrawFocus = false; drawArgs.DrawState = DrawState.Normal; a = (ImageAreaBase)this.Areas[idx]; if(a.OwnerDraw) { // Set the image area's drawing state if(!enabled || !a.Enabled) drawArgs.DrawState = DrawState.Disabled; else if(focused && activeArea == idx) drawArgs.DrawState = DrawState.Focus; else if(mouseArea == idx) drawArgs.DrawState = DrawState.HotLight; a.RaiseEvent(ImageAreaEvent.DrawImage, drawArgs); // Draw focus frame? if(focused && idx == activeArea && drawArgs.DrawFocus) a.DrawFocusFrame(g, p); } else { // If we have the focus and this is the active area, draw a focus frame around it unless // overridden by the image map owner draw event. if(focused && idx == activeArea && drawFrame) a.DrawFocusFrame(g, p); } } }