/// <summary> /// This is used to add a dynamic image area on the initial page load /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { lblEnabledMsg.Text = "Image map enabled"; // Add a dynamic image area. This is only created once when the page is first loaded. On post // backs, the area is recreated from view state. ImageAreaRectangle r = new ImageAreaRectangle(new Rectangle(40, 57, 20, 20)); r.Action = AreaClickAction.PostBack; r.ToolTip = "Area 5 (Dynamically added)"; r.AccessKey = "5"; r.Attributes.Add("onmouseover", "javascript: IM_Highlight(5);"); r.Attributes.Add("onmouseout", "javascript: IM_Highlight(0);"); // Tell the item to mark all properties as dirty so that it is restored entirely from view state // on post backs. If not done, you must recreate the area on post backs. r.MarkAsDirty(); imClickMap.Areas.Add(r); } // The Visual Studio designer loses the custom attributes on the image area controls in the HTML when // the collection editor is used. This happens to ListItem objects in controls such as the ListBox // and DropDown controls too. The only workaround is to set such attributes in code as shown below // when the page loads. ((ImageAreaBase)imClickMap.Areas[0]).Attributes.Add("onmouseover", "javascript: IM_Highlight(1);"); ((ImageAreaBase)imClickMap.Areas[0]).Attributes.Add("onmouseout", "javascript: IM_Highlight(0);"); ((ImageAreaBase)imClickMap.Areas[1]).Attributes.Add("onmouseover", "javascript: IM_Highlight(2);"); ((ImageAreaBase)imClickMap.Areas[1]).Attributes.Add("onmouseout", "javascript: IM_Highlight(0);"); ((ImageAreaBase)imClickMap.Areas[2]).Attributes.Add("onmouseover", "javascript: IM_Highlight(3);"); ((ImageAreaBase)imClickMap.Areas[2]).Attributes.Add("onmouseout", "javascript: IM_Highlight(0);"); ((ImageAreaBase)imClickMap.Areas[3]).Attributes.Add("onmouseover", "javascript: IM_Highlight(4);"); ((ImageAreaBase)imClickMap.Areas[3]).Attributes.Add("onmouseout", "javascript: IM_Highlight(0);"); }
//===================================================================== /// <summary> /// Constructor /// </summary> public ImageMapEventsForm() { InitializeComponent(); // This is used to format text for various image areas sfFormat = new StringFormat(); sfFormat.Alignment = StringAlignment.Center; sfFormat.LineAlignment = StringAlignment.Center; sfFormat.HotkeyPrefix = HotkeyPrefix.Show; // A couple of fonts for some of the image areas buttonFont = new Font("Microsoft Sans Serif", 7.8f, FontStyle.Bold); hyperlinkFont = new Font("Microsoft Sans Serif", 7.8f, FontStyle.Underline); // Get the image for the "buttons" and define the attributes used to given them their various draw // state effects. imgFiller = new Bitmap(GetType(), "ImageMapEventsForm.Filler.png"); // Image attributes used to lighten normal buttons cmNormal = new ColorMatrix(); cmNormal.Matrix33 = 0.5f; iaNormal = new ImageAttributes(); iaNormal.SetColorMatrix(cmNormal, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // Image attributes that lighten and desaturate disabled buttons cmDisabled = new ColorMatrix(); cmDisabled.Matrix00 = cmDisabled.Matrix01 = cmDisabled.Matrix02 = cmDisabled.Matrix10 = cmDisabled.Matrix11 = cmDisabled.Matrix12 = cmDisabled.Matrix20 = cmDisabled.Matrix21 = cmDisabled.Matrix22 = 0.33f; cmDisabled.Matrix33 = 0.5f; iaDisabled = new ImageAttributes(); iaDisabled.SetColorMatrix(cmDisabled, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // Hook up the event handlers. Since they are not accessible in the designer, we must do it // manually. Hang on to the references as we use them fairly often in here. areaOwnerDrawOnOff = (ImageAreaRectangle)imMap.Areas[0]; areaOwnerDrawOnOff.Click += areaOwnerDrawOnOff_Click; areaOwnerDrawOnOff.DrawImage += OwnerDrawOnOff_DrawImage; areaVisitWebSite = (ImageAreaRectangle)imMap.Areas[3]; areaVisitWebSite.DrawImage += VisitWebSite_DrawImage; areaVisitWebSite.DoubleClick += VisitWebSite_DoubleClick; // The same draw event handler is used for the three "buttons". The image map Click event handles // the area click events. ImageAreaBase a = (ImageAreaBase)imMap.Areas[4]; a.DrawImage += Button_DrawImage; a = (ImageAreaBase)imMap.Areas[5]; a.DrawImage += Button_DrawImage; a = (ImageAreaBase)imMap.Areas[6]; a.DrawImage += Button_DrawImage; // These two just demonstrate mouse and focus event handling by the image areas a = (ImageAreaBase)imMap.Areas[1]; a.DrawImage += MouseArea_DrawImage; a.MouseEnter += MouseArea_MouseEnter; a.MouseDown += MouseArea_MouseDown; a.MouseUp += MouseArea_MouseUp; a.MouseLeave += MouseArea_MouseLeave; // These two tend to obscure the other mouse events. // You can uncomment them to see them in action. // a.MouseMove += MouseArea_MouseMove; // a.MouseHover += MouseArea_MouseHover; a = (ImageAreaBase)imMap.Areas[2]; a.DrawImage += FocusArea_DrawImage; a.Enter += FocusArea_Enter; a.Leave += FocusArea_Leave; }