/// <summary> /// 刷新缩略图绘图板 /// </summary> public void RefreshAbbreviateCanvas() { if (abbreviateCanvas != null && abbreviateCanvas.Visible && refreshAbbreviateBitmap) // 需要刷新缩略图 { abbreviateCanvas.PaintCanvas = false; // 避免使用释放的缩略图片资源的问题 AbbreviateCanvas tempAbbreviateCanvas = abbreviateCanvas; abbreviateCanvas = null; if (canvasAbbreviateBitmap != null) { canvasAbbreviateBitmap.Dispose(); // 释放图片资源 } canvasAbbreviateBitmap = new Bitmap(background.CanvasSize.Width, background.CanvasSize.Height); Graphics g = Graphics.FromImage(canvasAbbreviateBitmap); g.Clear(Color.White); // 绘制图元 foreach (GraphElement graphElement in graphElementList) { graphElement.AbbreviativePaint(g); } refreshAbbreviateBitmap = false; g.Dispose(); // 释放绘图资源 tempAbbreviateCanvas.AbbreciateImage = canvasAbbreviateBitmap; tempAbbreviateCanvas.Invalidate(); abbreviateCanvas = tempAbbreviateCanvas; abbreviateCanvas.PaintCanvas = true; // 避免使用释放的缩略图片资源的问题 } }
/// <summary> /// 绘制的方法 /// </summary> /// <param name="e">绘制事件参数</param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (paintCanvas) // 绘制绘图板 { // 获取绘图对象 Graphics g = e.Graphics; visibleRectangle.Location = ClientRectangle.Location - new Size(AutoScrollPosition); visibleRectangle.Size = ClientSize; // 设置绘图参数 g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighSpeed; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; // 提高文本绘制质量 g.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); // 绘制缩略图元的连接线 if (showAbbreviatGraphElement && autoConnect) { abbreviateLine.AbbreviativePaint(g); } // 绘制图元 foreach (GraphElement graphElement in graphElementList) { Rectangle rectangle = new Rectangle(graphElement.Location, graphElement.ElementSize); if (graphElement.Visible && (visibleRectangle.Contains(rectangle) || visibleRectangle.IntersectsWith(rectangle))) // 当图元在可视区域内再进行绘制 { graphElement.Paint(g); } } // 绘制缩略的图元 if (showAbbreviatGraphElement) { abbreviatGraphElement.Paint(g); } // 绘制多选框 if (multiSelect) { g.FillRectangle(multiSelectBrush, multiSelectRectangle); g.DrawRectangle(multiSelectPen, multiSelectRectangle); } // 绘制多选图元框选区域 if (showMultiSelectRegion) { g.DrawRectangle(multiSelectRegionPen, multiSelectRegion); } // 绘制缩略图控件 if (abbreviateCanvas != null && abbreviateCanvas.Visible) { abbreviateCanvas.AbbreciateImage = GetBitMap(); abbreviateCanvas.VisibleRegionLocation = this.AutoScrollPosition; abbreviateCanvas.VisibleRegionSize = this.ClientSize; abbreviateCanvas.EntireRegionSize = background.CanvasSize; abbreviateCanvas.Invalidate(); } // 绘制冲突区域 if (showConflictRectangle) { g.FillRectangle(multiSelectBrush, conflictRectangle); showConflictRectangle = false; } // 绘制提示的图片 if (showTooltipImage) { g.DrawImage(tooltipImage, tooltipImageRectangle); showTooltipImage = false; // 只显示一次 } // 绘制提示文本 if (background.ShowTooltipText) { g.FillRectangle(backgroundBrush, tooltipTextRectangle); g.DrawRectangle(new Pen(Color.Black), tooltipTextRectangle); g.DrawString(background.TooltipText, background.TooltipFont, textBrush, tooltipTextLocation); } } }