Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new instance the given <paramref name="from">source node</paramref>
        /// and the possible target <paramref name="nodes"/>.
        /// </summary>
        /// <remarks>
        /// Pressing the "Show Path" button will set the DialogResult.OK and close the dialog.
        /// It is up to the caller to fetch the ToItem and get the shortest path.
        /// </remarks>
        /// <param name="from">The source node.</param>
        /// <param name="nodes">The nodes to choose the target from.</param>
        public ShortestPathDialog(INode from, List <INode> nodes)
        {
            From  = from;
            Nodes = nodes;

            InitializeComponent();
            toComboBox.DataSource = Nodes;

            // render the From node in the canvasControl
            var bounds = new RectD(0, 0, fromControl.Width, fromControl.Height);
            var layout = new RectD(0, 0, 240, 80);

            canvasControl.Size = new Size((int)bounds.Width, (int)bounds.Height);
            canvasControl.HorizontalScrollBarPolicy = ScrollBarVisibility.Never;
            canvasControl.VerticalScrollBarPolicy   = ScrollBarVisibility.Never;
            canvasControl.Graph.CreateNode(layout, from.Style, from.Tag);
            canvasControl.ContentRect = bounds;
            canvasControl.FitContent();

            // render the canvasControl in a Bitmap
            bitmap = new Bitmap(canvasControl.Size.Width, canvasControl.Size.Height, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bitmap)) {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.Clear(Color.Transparent);
                ContextConfigurator cc = new ContextConfigurator(canvasControl.ContentRect);
                var renderContext      = cc.CreateRenderContext(canvasControl, g);
                canvasControl.RenderContent(renderContext, g);
            }
            fromControl.Image = bitmap;
        }
        /// <summary>
        /// Paint the style representation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void styleListBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            ListBox listBox = (ListBox)sender;
            int     i       = e.Index;

            e.DrawBackground();
            INode node = listBox.Items[i] as INode;

            var     bounds = e.Bounds;
            InsetsD insets = new InsetsD(2);

            var canvasControl = new GraphControl();

            canvasControl.Size = new Size(bounds.Width - (int)insets.HorizontalInsets,
                                          bounds.Height - (int)insets.VerticalInsets);
            Bitmap   bm = new Bitmap(canvasControl.Size.Width, canvasControl.Size.Height);
            Graphics g  = Graphics.FromImage(bm);

            canvasControl.HorizontalScrollBarPolicy = ScrollBarVisibility.Never;
            canvasControl.VerticalScrollBarPolicy   = ScrollBarVisibility.Never;
            var dummyNode = canvasControl.Graph.CreateNode(node.Layout.ToRectD(), node.Style, node.Tag);

            foreach (var label in node.Labels)
            {
                canvasControl.Graph.AddLabel(dummyNode, label.Text, label.LayoutParameter, label.Style, label.PreferredSize, label.Tag);
            }
            foreach (var port in node.Ports)
            {
                canvasControl.Graph.AddPort(dummyNode, port.LocationParameter, port.Style, port.Tag);
            }
            canvasControl.ContentRect = new RectD(0, 0, 70, 70);
            canvasControl.FitContent();
            e.DrawBackground();
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.White);

            ContextConfigurator cc = new ContextConfigurator(canvasControl.ContentRect);
            var renderContext      = cc.CreateRenderContext(canvasControl, g);

            canvasControl.RenderContent(renderContext, g);
            var listGraphics = e.Graphics;
            var oldClip      = listGraphics.Clip;

            listGraphics.IntersectClip(bounds);
            listGraphics.Clear(listBox.BackColor);
            listGraphics.DrawImage(bm, bounds.X + (int)insets.Left, bounds.Y + (int)insets.Top, bm.Width, bm.Height);
            listGraphics.Clip = oldClip;
            e.DrawFocusRectangle();

            canvasControl.Dispose();
            g.Dispose();
            bm.Dispose();
        }