首页 > 代码库 > ZedGraph 柱状图、饼图、折线图演示源码

ZedGraph 柱状图、饼图、折线图演示源码

http://code1.okbase.net/codefile/ZedGraphControl.ContextMenu.cs_201211225626_97.htm
  1. //
  2. //This library is free software; you can redistribute it and/or
  3. //modify it under the terms of the GNU Lesser General Public
  4. //License as published by the Free Software Foundation; either
  5. //version 2.1 of the License, or (at your option) any later version.
  6. //
  7. //This library is distributed in the hope that it will be useful,
  8. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10. //Lesser General Public License for more details.
  11. //
  12. //You should have received a copy of the GNU Lesser General Public
  13. //License along with this library; if not, write to the Free Software
  14. //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  15. //=============================================================================
  16.  
  17. using System;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Drawing.Drawing2D;
  21. using System.Drawing.Text;
  22. using System.Windows.Forms;
  23. using System.Threading;
  24. using System.Drawing.Imaging;
  25. using System.IO;
  26. using System.Text;
  27.  
  28. using System.Runtime.InteropServices;
  29. //using System.Diagnostics;
  30.  
  31. namespace ZedGraph
  32. {
  33.     partial class ZedGraphControl
  34.     {
  35.  
  36.     #region ContextMenu
  37.  
  38.         // Revision: JCarpenter 10/06
  39.         /// <summary>
  40.         /// Public enumeration that specifies the type of
  41.         /// object present at the Context Menu‘s mouse location
  42.         /// </summary>
  43.         public enum ContextMenuObjectState
  44.         {
  45.             /// <summary>
  46.             /// The object is an Inactive Curve Item at the Context Menu‘s mouse position
  47.             /// </summary>
  48.             InactiveSelection,
  49.             /// <summary>
  50.             /// The object is an active Curve Item at the Context Menu‘s mouse position
  51.             /// </summary>
  52.             ActiveSelection,
  53.             /// <summary>
  54.             /// There is no selectable object present at the Context Menu‘s mouse position
  55.             /// </summary>
  56.             Background
  57.         }
  58.  
  59.         //Revision: JCarpenter 10/06
  60.         /// <summary>
  61.         /// Find the object currently under the mouse cursor, and return its state.
  62.         /// </summary>
  63.         private ContextMenuObjectState GetObjectState()
  64.         {
  65.             ContextMenuObjectState objState = ContextMenuObjectState.Background;
  66.  
  67.             // Determine object state
  68.             Point mousePt = this.PointToClient( Control.MousePosition );
  69.             int iPt;
  70.             GraphPane pane;
  71.             object nearestObj;
  72.  
  73.             using ( Graphics g = this.CreateGraphics() )
  74.             {
  75.                 if ( this.MasterPane.FindNearestPaneObject( mousePt, g, out pane,
  76.                         out nearestObj, out iPt ) )
  77.                 {
  78.                     CurveItem item = nearestObj as CurveItem;
  79.  
  80.                     if ( item != null && iPt >= 0 )
  81.                     {
  82.                         if ( item.IsSelected )
  83.                             objState = ContextMenuObjectState.ActiveSelection;
  84.                         else
  85.                             objState = ContextMenuObjectState.InactiveSelection;
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             return objState;
  91.         }
  92.  
  93.         /// <summary>
  94.         /// protected method to handle the popup context menu in the <see cref="ZedGraphControl"/>.
  95.         /// </summary>
  96.         /// <param name="sender"></param>
  97.         /// <param name="e"></param>
  98.         private void contextMenuStrip1_Opening( object sender, CancelEventArgs e )
  99.         {
  100.             // disable context menu by default
  101.             e.Cancel = true;
  102.             ContextMenuStrip menuStrip = sender as ContextMenuStrip;
  103.  
  104.             //Revision: JCarpenter 10/06
  105.             ContextMenuObjectState objState = GetObjectState();
  106.  
  107.             if ( _masterPane != null && menuStrip != null )
  108.             {
  109.                 menuStrip.Items.Clear();
  110.  
  111.                 _isZooming = false;
  112.                 _isPanning = false;
  113.                 Cursor.Current = Cursors.Default;
  114.  
  115.                 _menuClickPt = this.PointToClient( Control.MousePosition );
  116.                 GraphPane pane = _masterPane.FindPane( _menuClickPt );
  117.  
  118.                 if ( _isShowContextMenu )
  119.                 {
  120.                     string menuStr = string.Empty;
  121.  
  122.                     ToolStripMenuItem item = new ToolStripMenuItem();
  123.                     item.Name = "copy";
  124.                     item.Tag = "copy";
  125.                     item.Text = _resourceManager.GetString( "copy" );
  126.                     item.Click += new System.EventHandler( this.MenuClick_Copy );
  127.                     menuStrip.Items.Add( item );
  128.  
  129.                     item = new ToolStripMenuItem();
  130.                     item.Name = "save_as";
  131.                     item.Tag = "save_as";
  132.                     item.Text = _resourceManager.GetString( "save_as" );
  133.                     item.Click += new System.EventHandler( this.MenuClick_SaveAs );
  134.                     menuStrip.Items.Add( item );
  135.  
  136.                     item = new ToolStripMenuItem();
  137.                     item.Name = "page_setup";
  138.                     item.Tag = "page_setup";
  139.                     item.Text = _resourceManager.GetString( "page_setup" );
  140.                     item.Click += new System.EventHandler( this.MenuClick_PageSetup );
  141.                     menuStrip.Items.Add( item );
  142.  
  143.                     item = new ToolStripMenuItem();
  144.                     item.Name = "print";
  145.                     item.Tag = "print";
  146.                     item.Text = _resourceManager.GetString( "print" );
  147.                     item.Click += new System.EventHandler( this.MenuClick_Print );
  148.                     menuStrip.Items.Add( item );
  149.  
  150.                     item = new ToolStripMenuItem();
  151.                     item.Name = "show_val";
  152.                     item.Tag = "show_val";
  153.                     item.Text = _resourceManager.GetString( "show_val" );
  154.                     item.Click += new System.EventHandler( this.MenuClick_ShowValues );
  155.                     item.Checked = this.IsShowPointValues;
  156.                     menuStrip.Items.Add( item );
  157.  
  158.                     item = new ToolStripMenuItem();
  159.                     item.Name = "unzoom";
  160.                     item.Tag = "unzoom";
  161.  
  162.                     if ( pane == null || pane.ZoomStack.IsEmpty )
  163.                         menuStr = _resourceManager.GetString( "unzoom" );
  164.                     else
  165.                     {
  166.                         switch ( pane.ZoomStack.Top.Type )
  167.                         {
  168.                             case ZoomState.StateType.Zoom:
  169.                             case ZoomState.StateType.WheelZoom:
  170.                                 menuStr = _resourceManager.GetString( "unzoom" );
  171.                                 break;
  172.                             case ZoomState.StateType.Pan:
  173.                                 menuStr = _resourceManager.GetString( "unpan" );
  174.                                 break;
  175.                             case ZoomState.StateType.Scroll:
  176.                                 menuStr = _resourceManager.GetString( "unscroll" );
  177.                                 break;
  178.                         }
  179.                     }
  180.  
  181.                     //menuItem.Text = "Un-" + ( ( pane == null || pane.zoomStack.IsEmpty ) ?
  182.                     //  "Zoom" : pane.zoomStack.Top.TypeString );
  183.                     item.Text = menuStr;
  184.                     item.Click += new EventHandler( this.MenuClick_ZoomOut );
  185.                     if ( pane == null || pane.ZoomStack.IsEmpty )
  186.                         item.Enabled = false;
  187.                     menuStrip.Items.Add( item );
  188.  
  189.                     item = new ToolStripMenuItem();
  190.                     item.Name = "undo_all";
  191.                     item.Tag = "undo_all";
  192.                     menuStr = _resourceManager.GetString( "undo_all" );
  193.                     item.Text = menuStr;
  194.                     item.Click += new EventHandler( this.MenuClick_ZoomOutAll );
  195.                     if ( pane == null || pane.ZoomStack.IsEmpty )
  196.                         item.Enabled = false;
  197.                     menuStrip.Items.Add( item );
  198.  
  199.                     item = new ToolStripMenuItem();
  200.                     item.Name = "set_default";
  201.                     item.Tag = "set_default";
  202.                     menuStr = _resourceManager.GetString( "set_default" );
  203.                     item.Text = menuStr;
  204.                     item.Click += new EventHandler( this.MenuClick_RestoreScale );
  205.                     if ( pane == null )
  206.                         item.Enabled = false;
  207.                     menuStrip.Items.Add( item );
  208.  
  209.                     // if e.Cancel is set to false, the context menu does not display
  210.                     // it is initially set to false because the context menu has no items
  211.                     e.Cancel = false;
  212.  
  213.                     // Provide Callback for User to edit the context menu
  214.                     //Revision: JCarpenter 10/06 - add ContextMenuObjectState objState
  215.                     if ( this.ContextMenuBuilder != null )
  216.                         this.ContextMenuBuilder( this, menuStrip, _menuClickPt, objState );
  217.                 }
  218.             }
  219.         }
  220.  
  221.         /// <summary>
  222.         /// Handler for the "Copy" context menu item.  Copies the current image to a bitmap on the
  223.         /// clipboard.
  224.         /// </summary>
  225.         /// <param name="sender"></param>
  226.         /// <param name="e"></param>
  227.         protected void MenuClick_Copy( System.Object sender, System.EventArgs e )
  228.         {
  229.             Copy( _isShowCopyMessage );
  230.         }
  231.  
  232.         /// <summary>
  233.         /// Handler for the "Copy" context menu item.  Copies the current image to a bitmap on the
  234.         /// clipboard.
  235.         /// </summary>
  236.         /// <param name="isShowMessage">boolean value that determines whether or not a prompt will be
  237.         /// displayed.  true to show a message of "Image Copied to ClipBoard".</param>
  238.         public void Copy( bool isShowMessage )
  239.         {
  240.             if ( _masterPane != null )
  241.             {
  242.                 //Clipboard.SetDataObject( _masterPane.GetImage(), true );
  243.  
  244.                 // Threaded copy mode to avoid crash with MTA
  245.                 // Contributed by Dave Moor
  246.                 Thread ct = new Thread( new ThreadStart( this.ClipboardCopyThread ) );
  247.                 //ct.ApartmentState = ApartmentState.STA;
  248.                 ct.SetApartmentState( ApartmentState.STA );
  249.                 ct.Start();
  250.                 ct.Join();
  251.  
  252.                 if ( isShowMessage )
  253.                 {
  254.                     string str = _resourceManager.GetString( "copied_to_clip" );
  255.                     //MessageBox.Show( "Image Copied to ClipBoard" );
  256.                     MessageBox.Show( str );
  257.                 }
  258.             }
  259.         }
  260.  
  261.         /// <summary>
  262.         /// A threaded version of the copy method to avoid crash with MTA
  263.         /// </summary>
  264.         private void ClipboardCopyThread()
  265.         {
  266.             Clipboard.SetDataObject( ImageRender(), true );
  267.         }
  268.  
  269.         //
  270.         /// <summary>
  271.         /// Setup for creation of a new image, applying appropriate anti-alias properties and
  272.         /// returning the resultant image file
  273.         /// </summary>
  274.         /// <returns></returns>
  275.         private Image ImageRender()
  276.         {
  277.             return _masterPane.GetImage( _masterPane.IsAntiAlias );
  278.         }
  279.  
  280.         /// <summary>
  281.         /// Special handler that copies the current image to an Emf file on the clipboard.
  282.         /// </summary>
  283.         /// <remarks>This version is similar to the regular <see cref="Copy" /> method, except that
  284.         /// it will place an Emf image (vector) on the ClipBoard instead of the regular bitmap.</remarks>
  285.         /// <param name="isShowMessage">boolean value that determines whether or not a prompt will be
  286.         /// displayed.  true to show a message of "Image Copied to ClipBoard".</param>
  287.         public void CopyEmf(bool isShowMessage)
  288.         {
  289.             if (_masterPane != null)
  290.             {
  291.                 // Threaded copy mode to avoid crash with MTA
  292.                 // Contributed by Dave Moor
  293.                 Thread ct = new Thread(new ThreadStart(this.ClipboardCopyThreadEmf));
  294.                 //ct.ApartmentState = ApartmentState.STA;
  295.                 ct.SetApartmentState(ApartmentState.STA);
  296.                 ct.Start();
  297.                 ct.Join();
  298.  
  299.                 if (isShowMessage)
  300.                 {
  301.                     string str = _resourceManager.GetString("copied_to_clip");
  302.                     MessageBox.Show(str);
  303.                 }
  304.             }
  305.         }
  306.  
  307.         /// <summary>
  308.         /// A threaded version of the copy method to avoid crash with MTA
  309.         /// </summary>
  310.         private void ClipboardCopyThreadEmf()
  311.         {
  312.             using (Graphics g = this.CreateGraphics())
  313.             {
  314.                 IntPtr hdc = g.GetHdc();
  315.                 Metafile metaFile = new Metafile(hdc, EmfType.EmfPlusOnly);
  316.                 g.ReleaseHdc(hdc);
  317.  
  318.                 using (Graphics gMeta = Graphics.FromImage(metaFile))
  319.                 {
  320.                     this._masterPane.Draw( gMeta );
  321.                 }
  322.  
  323.                 //IntPtr hMeta = metaFile.GetHenhmetafile();
  324.                 ClipboardMetafileHelper.PutEnhMetafileOnClipboard( this.Handle, metaFile );
  325.                 //System.Windows.Forms.Clipboard.SetDataObject(hMeta, true);
  326.  
  327.                 //g.Dispose();
  328.             }
  329.         }
  330.  
  331.         /// <summary>
  332.         /// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
  333.         /// file.
  334.         /// </summary>
  335.         /// <param name="sender"></param>
  336.         /// <param name="e"></param>
  337.         protected void MenuClick_SaveAs( System.Object sender, System.EventArgs e )
  338.         {
  339.             SaveAs();
  340.         }
  341.  
  342.         /// <summary>
  343.         /// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
  344.         /// file in either the Emf (vector), or a variety of Bitmap formats.
  345.         /// </summary>
  346.         /// <remarks>
  347.         /// Note that <see cref="SaveAsBitmap" /> and <see cref="SaveAsEmf" /> methods are provided
  348.         /// which allow for Bitmap-only or Emf-only handling of the "Save As" context menu item.
  349.         /// </remarks>
  350.         public void SaveAs()
  351.         {
  352.             SaveAs( null );
  353.         }
  354.  
  355.         /// <summary>
  356.         /// Copies the current image to the selected file in 
  357.         /// Emf (vector), or a variety of Bitmap formats.
  358.         /// </summary>
  359.         /// <param name="DefaultFileName">
  360.         /// Accepts a default file name for the file dialog (if "" or null, default is not used)
  361.         /// </param>
  362.         /// <returns>
  363.         /// The file name saved, or "" if cancelled.
  364.         /// </returns>
  365.         /// <remarks>
  366.         /// Note that <see cref="SaveAsBitmap" /> and <see cref="SaveAsEmf" /> methods are provided
  367.         /// which allow for Bitmap-only or Emf-only handling of the "Save As" context menu item.
  368.         /// </remarks>
  369.         public String SaveAs( String DefaultFileName )
  370.         {
  371.             if ( _masterPane != null )
  372.             {
  373.                 _saveFileDialog.Filter =
  374.                     "Emf Format (*.emf)|*.emf|" +
  375.                     "PNG Format (*.png)|*.png|" +
  376.                     "Gif Format (*.gif)|*.gif|" +
  377.                     "Jpeg Format (*.jpg)|*.jpg|" +
  378.                     "Tiff Format (*.tif)|*.tif|" +
  379.                     "Bmp Format (*.bmp)|*.bmp";
  380.  
  381.                 if ( DefaultFileName != null && DefaultFileName.Length > 0 )
  382.                 {
  383.                     String ext = System.IO.Path.GetExtension( DefaultFileName ).ToLower();
  384.                     switch (ext)
  385.                     {
  386.                         case ".emf": _saveFileDialog.FilterIndex = 1; break;
  387.                         case ".png": _saveFileDialog.FilterIndex = 2; break;
  388.                         case ".gif": _saveFileDialog.FilterIndex = 3; break;
  389.                         case ".jpeg":
  390.                         case ".jpg": _saveFileDialog.FilterIndex = 4; break;
  391.                         case ".tiff":
  392.                         case ".tif": _saveFileDialog.FilterIndex = 5; break;
  393.                         case ".bmp": _saveFileDialog.FilterIndex = 6; break;
  394.                     }
  395.                     //If we were passed a file name, not just an extension, use it
  396.                     if ( DefaultFileName.Length > ext.Length )
  397.                     {
  398.                         _saveFileDialog.FileName = DefaultFileName;
  399.                     }
  400.                 }
  401.  
  402.                 if ( _saveFileDialog.ShowDialog() == DialogResult.OK )
  403.                 {
  404.                     Stream myStream = _saveFileDialog.OpenFile();
  405.                     if ( myStream != null )
  406.                     {
  407.                         if ( _saveFileDialog.FilterIndex == 1 )
  408.                         {
  409.                             myStream.Close();
  410.                             SaveEmfFile( _saveFileDialog.FileName );
  411.                         }
  412.                         else
  413.                         {
  414.                             ImageFormat format = ImageFormat.Png;
  415.                             switch (_saveFileDialog.FilterIndex)
  416.                             {
  417.                                 case 2: format = ImageFormat.Png; break;
  418.                                 case 3: format = ImageFormat.Gif; break;
  419.                                 case 4: format = ImageFormat.Jpeg; break;
  420.                                 case 5: format = ImageFormat.Tiff; break;
  421.                                 case 6: format = ImageFormat.Bmp; break;
  422.                             }
  423.  
  424.                             ImageRender().Save( myStream, format );
  425.                             //_masterPane.GetImage().Save( myStream, format );
  426.                             myStream.Close();
  427.                         }
  428.                         return _saveFileDialog.FileName;
  429.                     }
  430.                 }
  431.             }
  432.             return "";
  433.         }
  434.  
  435.         /// <summary>
  436.         /// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
  437.         /// Bitmap file.
  438.         /// </summary>
  439.         /// <remarks>
  440.         /// Note that this handler saves as a bitmap only.  The default handler is
  441.         /// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats
  442.         /// </remarks>
  443.         public void SaveAsBitmap()
  444.         {
  445.             if ( _masterPane != null )
  446.             {
  447.                 _saveFileDialog.Filter =
  448.                     "PNG Format (*.png)|*.png|" +
  449.                     "Gif Format (*.gif)|*.gif|" +
  450.                     "Jpeg Format (*.jpg)|*.jpg|" +
  451.                     "Tiff Format (*.tif)|*.tif|" +
  452.                     "Bmp Format (*.bmp)|*.bmp";
  453.  
  454.                 if ( _saveFileDialog.ShowDialog() == DialogResult.OK )
  455.                 {
  456.                     ImageFormat format = ImageFormat.Png;
  457.                     if ( _saveFileDialog.FilterIndex == 2 )
  458.                         format = ImageFormat.Gif;
  459.                     else if ( _saveFileDialog.FilterIndex == 3 )
  460.                         format = ImageFormat.Jpeg;
  461.                     else if ( _saveFileDialog.FilterIndex == 4 )
  462.                         format = ImageFormat.Tiff;
  463.                     else if ( _saveFileDialog.FilterIndex == 5 )
  464.                         format = ImageFormat.Bmp;
  465.  
  466.                     Stream myStream = _saveFileDialog.OpenFile();
  467.                     if ( myStream != null )
  468.                     {
  469.                         //_masterPane.GetImage().Save( myStream, format );
  470.                         ImageRender().Save( myStream, format );
  471.                         myStream.Close();
  472.                     }
  473.                 }
  474.             }
  475.         }
  476.  
  477.         /// <summary>
  478.         /// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
  479.         /// Emf format file.
  480.         /// </summary>
  481.         /// <remarks>
  482.         /// Note that this handler saves as an Emf format only.  The default handler is
  483.         /// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats.
  484.         /// </remarks>
  485.         public void SaveAsEmf()
  486.         {
  487.             if ( _masterPane != null )
  488.             {
  489.                 _saveFileDialog.Filter = "Emf Format (*.emf)|*.emf";
  490.  
  491.                 if ( _saveFileDialog.ShowDialog() == DialogResult.OK )
  492.                 {
  493.                     Stream myStream = _saveFileDialog.OpenFile();
  494.                     if ( myStream != null )
  495.                     {
  496.                         myStream.Close();
  497.                         //_masterPane.GetMetafile().Save( _saveFileDialog.FileName );
  498.                         SaveEmfFile(_saveFileDialog.FileName);
  499.                     }
  500.                 }
  501.             }
  502.         }
  503.  
  504.         /// <summary>
  505.         /// Save the current Graph to the specified filename in EMF (vector) format.
  506.         /// See <see cref="SaveAsEmf()" /> for public access.
  507.         /// </summary>
  508.         /// <remarks>
  509.         /// Note that this handler saves as an Emf format only.  The default handler is
  510.         /// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats.
  511.         /// </remarks>
  512.         internal void SaveEmfFile( string fileName )
  513.         {
  514.             using (Graphics g = this.CreateGraphics())
  515.             {
  516.                 IntPtr hdc = g.GetHdc();
  517.                 Metafile metaFile = new Metafile(hdc, EmfType.EmfPlusOnly);
  518.                 using (Graphics gMeta = Graphics.FromImage(metaFile))
  519.                 {
  520.                     //PaneBase.SetAntiAliasMode( gMeta, IsAntiAlias );
  521.                     //gMeta.CompositingMode = CompositingMode.SourceCopy;
  522.                     //gMeta.CompositingQuality = CompositingQuality.HighQuality;
  523.                     //gMeta.InterpolationMode = InterpolationMode.HighQualityBicubic;
  524.                     //gMeta.SmoothingMode = SmoothingMode.AntiAlias;
  525.                     //gMeta.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  526.                     this._masterPane.Draw(gMeta);
  527.                     //gMeta.Dispose();
  528.                 }
  529.  
  530.                 ClipboardMetafileHelper.SaveEnhMetafileToFile(metaFile, fileName );
  531.  
  532.                 g.ReleaseHdc(hdc);
  533.                 //g.Dispose();
  534.             }
  535.  
  536.         }
  537.  
  538.         internal class ClipboardMetafileHelper
  539.         {
  540.             [DllImport("user32.dll")]
  541.             static extern bool OpenClipboard(IntPtr hWndNewOwner);
  542.             [DllImport("user32.dll")]
  543.             static extern bool EmptyClipboard();
  544.             [DllImport("user32.dll")]
  545.             static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
  546.             [DllImport("user32.dll")]
  547.             static extern bool CloseClipboard();
  548.             [DllImport("gdi32.dll")]
  549.             static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, System.Text.StringBuilder hNULL);
  550.             [DllImport("gdi32.dll")]
  551.             static extern bool DeleteEnhMetaFile(IntPtr hemf);
  552.  
  553.             static internal bool SaveEnhMetafileToFile( Metafile mf, string fileName )
  554.             {
  555.                 bool bResult = false;
  556.                 IntPtr hEMF;
  557.                 hEMF = mf.GetHenhmetafile(); // invalidates mf
  558.                 if (!hEMF.Equals(new IntPtr(0)))
  559.                 {
  560.                     StringBuilder tempName = new StringBuilder(fileName);
  561.                     CopyEnhMetaFile(hEMF, tempName);
  562.                     DeleteEnhMetaFile(hEMF);
  563.                 }
  564.                 return bResult;
  565.             }
  566.  
  567.             static internal bool SaveEnhMetafileToFile(Metafile mf)
  568.             {
  569.                 bool bResult = false;
  570.                 IntPtr hEMF;
  571.                 hEMF = mf.GetHenhmetafile(); // invalidates mf
  572.                 if (!hEMF.Equals(new IntPtr(0)))
  573.                 {
  574.                     SaveFileDialog sfd = new SaveFileDialog();
  575.                     sfd.Filter = "Extended Metafile (*.emf)|*.emf";
  576.                     sfd.DefaultExt = ".emf";
  577.                     if (sfd.ShowDialog() == DialogResult.OK)
  578.                     {
  579.                         StringBuilder temp = new StringBuilder(sfd.FileName);
  580.                         CopyEnhMetaFile(hEMF, temp);
  581.                     }
  582.                     DeleteEnhMetaFile(hEMF);
  583.                 }
  584.                 return bResult;
  585.             }
  586.  
  587.             // Metafile mf is set to a state that is not valid inside this function.
  588.             static internal bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile mf)
  589.             {
  590.                 bool bResult = false;
  591.                 IntPtr hEMF, hEMF2;
  592.                 hEMF = mf.GetHenhmetafile(); // invalidates mf
  593.                 if (!hEMF.Equals(new IntPtr(0)))
  594.                 {
  595.                     hEMF2 = CopyEnhMetaFile(hEMF, null);
  596.                     if (!hEMF2.Equals(new IntPtr(0)))
  597.                     {
  598.                         if (OpenClipboard(hWnd))
  599.                         {
  600.                             if (EmptyClipboard())
  601.                             {
  602.                                 IntPtr hRes = SetClipboardData(14 /*CF_ENHMETAFILE*/, hEMF2);
  603.                                 bResult = hRes.Equals(hEMF2);
  604.                                 CloseClipboard();
  605.                             }
  606.                         }
  607.                     }
  608.                     DeleteEnhMetaFile(hEMF);
  609.                 }
  610.                 return bResult;
  611.             }
  612.         }
  613.  
  614.         /// <summary>
  615.         /// Handler for the "Show Values" context menu item.  Toggles the <see cref="IsShowPointValues"/>
  616.         /// property, which activates the point value tooltips.
  617.         /// </summary>
  618.         /// <param name="sender"></param>
  619.         /// <param name="e"></param>
  620.         protected void MenuClick_ShowValues( object sender, System.EventArgs e )
  621.         {
  622.             ToolStripMenuItem item = sender as ToolStripMenuItem;
  623.             if ( item != null )
  624.                 this.IsShowPointValues = !item.Checked;
  625.         }
  626.  
  627.         /// <summary>
  628.         /// Handler for the "Set Scale to Default" context menu item.  Sets the scale ranging to
  629.         /// full auto mode for all axes.
  630.         /// </summary>
  631.         /// <remarks>
  632.         /// This method differs from the <see cref="ZoomOutAll" /> method in that it sets the scales
  633.         /// to full auto mode.  The <see cref="ZoomOutAll" /> method sets the scales to their initial
  634.         /// setting prior to any user actions (which may or may not be full auto mode).
  635.         /// </remarks>
  636.         /// <param name="sender"></param>
  637.         /// <param name="e"></param>
  638.         protected void MenuClick_RestoreScale( object sender, EventArgs e )
  639.         {
  640.             if ( _masterPane != null )
  641.             {
  642.                 GraphPane pane = _masterPane.FindPane( _menuClickPt );
  643.                 RestoreScale( pane );
  644.             }
  645.         }
  646.  
  647.         /// <summary>
  648.         /// Handler for the "Set Scale to Default" context menu item.  Sets the scale ranging to
  649.         /// full auto mode for all axes.
  650.         /// </summary>
  651.         /// <remarks>
  652.         /// This method differs from the <see cref="ZoomOutAll" /> method in that it sets the scales
  653.         /// to full auto mode.  The <see cref="ZoomOutAll" /> method sets the scales to their initial
  654.         /// setting prior to any user actions (which may or may not be full auto mode).
  655.         /// </remarks>
  656.         /// <param name="primaryPane">The <see cref="GraphPane" /> object which is to have the
  657.         /// scale restored</param>
  658.         public void RestoreScale( GraphPane primaryPane )
  659.         {
  660.             if ( primaryPane != null )
  661.             {
  662.                 //Go ahead and save the old zoomstates, which provides an "undo"-like capability
  663.                 //ZoomState oldState = primaryPane.ZoomStack.Push( primaryPane, ZoomState.StateType.Zoom );
  664.                 ZoomState oldState = new ZoomState( primaryPane, ZoomState.StateType.Zoom );
  665.  
  666.                 using ( Graphics g = this.CreateGraphics() )
  667.                 {
  668.                     if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
  669.                     {
  670.                         foreach ( GraphPane pane in _masterPane._paneList )
  671.                         {
  672.                             pane.ZoomStack.Push( pane, ZoomState.StateType.Zoom );
  673.                             ResetAutoScale( pane, g );
  674.                         }
  675.                     }
  676.                     else
  677.                     {
  678.                         primaryPane.ZoomStack.Push( primaryPane, ZoomState.StateType.Zoom );
  679.                         ResetAutoScale( primaryPane, g );
  680.                     }
  681.  
  682.                     // Provide Callback to notify the user of zoom events
  683.                     if ( this.ZoomEvent != null )
  684.                         this.ZoomEvent( this, oldState, new ZoomState( primaryPane, ZoomState.StateType.Zoom ) );
  685.  
  686.                     //g.Dispose();
  687.                 }
  688.                 Refresh();
  689.             }
  690.         }
  691.  
  692.         private void ResetAutoScale( GraphPane pane, Graphics g )
  693.         {
  694.             pane.XAxis.ResetAutoScale( pane, g );
  695.             pane.X2Axis.ResetAutoScale( pane, g );
  696.             foreach ( YAxis axis in pane.YAxisList )
  697.                 axis.ResetAutoScale( pane, g );
  698.             foreach ( Y2Axis axis in pane.Y2AxisList )
  699.                 axis.ResetAutoScale( pane, g );
  700.         }
  701.  
  702.         /*
  703.                 public void RestoreScale( GraphPane primaryPane )
  704.                 {
  705.                     if ( primaryPane != null )
  706.                     {
  707.                         Graphics g = this.CreateGraphics();
  708.                         ZoomState oldState = new ZoomState( primaryPane, ZoomState.StateType.Zoom );
  709.                         //ZoomState newState = null;
  710.  
  711.                         if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
  712.                         {
  713.                             foreach ( GraphPane pane in _masterPane._paneList )
  714.                             {
  715.                                 if ( pane == primaryPane )
  716.                                 {
  717.                                     pane.XAxis.ResetAutoScale( pane, g );
  718.                                     foreach ( YAxis axis in pane.YAxisList )
  719.                                         axis.ResetAutoScale( pane, g );
  720.                                     foreach ( Y2Axis axis in pane.Y2AxisList )
  721.                                         axis.ResetAutoScale( pane, g );
  722.                                 }
  723.                             }
  724.                         }
  725.                         else
  726.                         {
  727.                             primaryPane.XAxis.ResetAutoScale( primaryPane, g );
  728.                             foreach ( YAxis axis in primaryPane.YAxisList )
  729.                                 axis.ResetAutoScale( primaryPane, g );
  730.                             foreach ( Y2Axis axis in primaryPane.Y2AxisList )
  731.                                 axis.ResetAutoScale( primaryPane, g );
  732.                         }
  733.  
  734.                         // Provide Callback to notify the user of zoom events
  735.                         if ( this.ZoomEvent != null )
  736.                             this.ZoomEvent( this, oldState, new ZoomState( primaryPane, ZoomState.StateType.Zoom ) );
  737.  
  738.                         g.Dispose();
  739.                         Refresh();
  740.                     }
  741.                 }
  742.         */
  743.         /*
  744.                 public void ZoomOutAll( GraphPane primaryPane )
  745.                 {
  746.                     if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
  747.                     {
  748.                         ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;
  749.  
  750.                         ZoomState oldState = new ZoomState( primaryPane, type );
  751.                         //ZoomState newState = pane.ZoomStack.PopAll( pane );
  752.                         ZoomState newState = null;
  753.                         if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
  754.                         {
  755.                             foreach ( GraphPane pane in _masterPane._paneList )
  756.                             {
  757.                                 ZoomState state = pane.ZoomStack.PopAll( pane );
  758.                                 if ( pane == primaryPane )
  759.                                     newState = state;
  760.                             }
  761.                         }
  762.                         else
  763.                             newState = primaryPane.ZoomStack.PopAll( primaryPane );
  764.  
  765.                         // Provide Callback to notify the user of zoom events
  766.                         if ( this.ZoomEvent != null )
  767.                             this.ZoomEvent( this, oldState, newState );
  768.  
  769.                         Refresh();
  770.                     }
  771.                 }
  772.  
  773.         */
  774.  
  775.         /// <summary>
  776.         /// Handler for the "UnZoom/UnPan" context menu item.  Restores the scale ranges to the values
  777.         /// before the last zoom or pan operation.
  778.         /// </summary>
  779.         /// <param name="sender"></param>
  780.         /// <param name="e"></param>
  781.         protected void MenuClick_ZoomOut( System.Object sender, System.EventArgs e )
  782.         {
  783.             if ( _masterPane != null )
  784.             {
  785.                 GraphPane pane = _masterPane.FindPane( _menuClickPt );
  786.                 ZoomOut( pane );
  787.             }
  788.         }
  789.  
  790.         /// <summary>
  791.         /// Handler for the "UnZoom/UnPan" context menu item.  Restores the scale ranges to the values
  792.         /// before the last zoom, pan, or scroll operation.
  793.         /// </summary>
  794.         /// <remarks>
  795.         /// Triggers a <see cref="ZoomEvent" /> for any type of undo (including pan, scroll, zoom, and
  796.         /// wheelzoom).  This method will affect all the
  797.         /// <see cref="GraphPane" /> objects in the <see cref="MasterPane" /> if
  798.         /// <see cref="IsSynchronizeXAxes" /> or <see cref="IsSynchronizeYAxes" /> is true.
  799.         /// </remarks>
  800.         /// <param name="primaryPane">The primary <see cref="GraphPane" /> object which is to be
  801.         /// zoomed out</param>
  802.         public void ZoomOut( GraphPane primaryPane )
  803.         {
  804.             if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
  805.             {
  806.                 ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;
  807.  
  808.                 ZoomState oldState = new ZoomState( primaryPane, type );
  809.                 ZoomState newState = null;
  810.                 if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
  811.                 {
  812.                     foreach ( GraphPane pane in _masterPane._paneList )
  813.                     {
  814.                         ZoomState state = pane.ZoomStack.Pop( pane );
  815.                         if ( pane == primaryPane )
  816.                             newState = state;
  817.                     }
  818.                 }
  819.                 else
  820.                     newState = primaryPane.ZoomStack.Pop( primaryPane );
  821.  
  822.                 // Provide Callback to notify the user of zoom events
  823.                 if ( this.ZoomEvent != null )
  824.                     this.ZoomEvent( this, oldState, newState );
  825.  
  826.                 Refresh();
  827.             }
  828.         }
  829.  
  830.         /// <summary>
  831.         /// Handler for the "Undo All Zoom/Pan" context menu item.  Restores the scale ranges to the values
  832.         /// before all zoom and pan operations
  833.         /// </summary>
  834.         /// <remarks>
  835.         /// This method differs from the <see cref="RestoreScale" /> method in that it sets the scales
  836.         /// to their initial setting prior to any user actions.  The <see cref="RestoreScale" /> method
  837.         /// sets the scales to full auto mode (regardless of what the initial setting may have been).
  838.         /// </remarks>
  839.         /// <param name="sender"></param>
  840.         /// <param name="e"></param>
  841.         protected void MenuClick_ZoomOutAll( System.Object sender, System.EventArgs e )
  842.         {
  843.             if ( _masterPane != null )
  844.             {
  845.                 GraphPane pane = _masterPane.FindPane( _menuClickPt );
  846.                 ZoomOutAll( pane );
  847.             }
  848.         }
  849.  
  850.         /// <summary>
  851.         /// Handler for the "Undo All Zoom/Pan" context menu item.  Restores the scale ranges to the values
  852.         /// before all zoom and pan operations
  853.         /// </summary>
  854.         /// <remarks>
  855.         /// This method differs from the <see cref="RestoreScale" /> method in that it sets the scales
  856.         /// to their initial setting prior to any user actions.  The <see cref="RestoreScale" /> method
  857.         /// sets the scales to full auto mode (regardless of what the initial setting may have been).
  858.         /// </remarks>
  859.         /// <param name="primaryPane">The <see cref="GraphPane" /> object which is to be zoomed out</param>
  860.         public void ZoomOutAll( GraphPane primaryPane )
  861.         {
  862.             if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
  863.             {
  864.                 ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;
  865.  
  866.                 ZoomState oldState = new ZoomState( primaryPane, type );
  867.                 //ZoomState newState = pane.ZoomStack.PopAll( pane );
  868.                 ZoomState newState = null;
  869.                 if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
  870.                 {
  871.                     foreach ( GraphPane pane in _masterPane._paneList )
  872.                     {
  873.                         ZoomState state = pane.ZoomStack.PopAll( pane );
  874.                         if ( pane == primaryPane )
  875.                             newState = state;
  876.                     }
  877.                 }
  878.                 else
  879.                     newState = primaryPane.ZoomStack.PopAll( primaryPane );
  880.  
  881.                 // Provide Callback to notify the user of zoom events
  882.                 if ( this.ZoomEvent != null )
  883.                     this.ZoomEvent( this, oldState, newState );
  884.  
  885.                 Refresh();
  886.             }
  887.         }
  888.  
  889.     #endregion
  890.  
  891.     }
  892. }

更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明 Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有

站长统计

ZedGraph 柱状图、饼图、折线图演示源码