.Net面板,可在面板上缩放';什么是图形?
如果我有一个面板,我正在其CreateGraphics()对象上绘制通用形状,如下所示:.Net面板,可在面板上缩放';什么是图形?,.net,panel,system.drawing,.net,Panel,System.drawing,如果我有一个面板,我正在其CreateGraphics()对象上绘制通用形状,如下所示: public static void DrawRoundRectWithText(Graphics g, Pen p, float x, float y, float width, float height, float radius, string textFirstLike, string textSecondsLine, bool hasGate, Color color)
public static void DrawRoundRectWithText(Graphics g, Pen p, float x, float y, float width, float height, float radius, string textFirstLike, string textSecondsLine, bool hasGate, Color color)
{
System.Drawing.Graphics formGraphics = g;
g.InterpolationMode = InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
drawBrush.Color = color;
Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
p.Width = RECTANGLE_BORDER;
formGraphics.FillRectangle(drawBrush, rect);
p.Color = RECTANGLE_COLOR;
formGraphics.DrawRectangle(p, rect);
DrawString(g, textFirstLike, x + TEXT_OFFSET_X, y + height / 4, PRIMARY_TEXT_FONT_SIZE, (int)width);
DrawString(g, textSecondsLine, x + TEXT_OFFSET_X, y + height / 2, SECONDARY_TEXT_FONT_SIZE, (int)width);
int gateWidth = QuestNode.GATE_RADIUS;
int gateHeight = QuestNode.GATE_RADIUS;
if (hasGate)
{
drawBrush.Color = GATE_COLOR;
Rectangle circle = new Rectangle((int)(x + width) - gateWidth/2, (int)(y + height) - gateHeight/2, gateWidth, gateHeight);
formGraphics.FillEllipse(drawBrush, circle);
formGraphics.DrawEllipse(p, circle);
}
Point[] diamondPoints = new Point[4];
diamondPoints[0] = new Point((int)(x - gateWidth / 2), (int)y);
diamondPoints[1] = new Point((int)x, (int)(y - gateHeight / 2));
diamondPoints[2] = new Point((int)(x + gateWidth / 2), (int)y);
diamondPoints[3] = new Point((int)x, (int)(y + gateHeight / 2));
drawBrush.Color = Color.LightGray;
formGraphics.FillPolygon(drawBrush, diamondPoints);
formGraphics.DrawPolygon(p, diamondPoints);
}
是否有一种方法可以放大/缩小面板,从而自动缩小形状?可以使用System.Drawing.Graphics.ScaleTransform()方法。不管怎样,让我们检查一下您正在做什么:每次重建资源(画笔和钢笔)都是非常低效的(而且您甚至没有显式地释放它们)。这非常有效。非常感谢你!我已经更新了用于绘制这些形状的完整代码,其中图形和画笔对象是在初始化时创建的,并且是用于所有形状的唯一代码。你认为代码可以优化吗?再次感谢。另外,当单击面板时,缩放栏窗体是否会影响MouseDown事件返回的值?不,MouseDown不会受到影响(因为缩放仅应用于图形)。您需要根据缩放因子(和偏移)缩放单击位置。重新指定画笔的“颜色”属性时,它比创建新画笔消耗的资源少,但不是那么理想:只需为每个恒定颜色保留一个画笔/笔(并使用字典作为“可变”颜色的缓存)。此外,在绘制文本时,您可以尝试使用StringFormat(但我没有看到您的DrawString实现,所以我只是猜测)。