将 Windows 窗体 OnPaint 转换为等效的 WPF 形状

发布于 2024-09-05 17:38:49 字数 3069 浏览 3 评论 0原文

我有一个现有的 OnPaint 方法,它可以绘制一些形状:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        g.CompositingQuality = CompositingQuality.HighQuality;

        Brush greenBrush = new SolidBrush(Color.FromArgb(73,172,113));
        Brush blueBrush = new SolidBrush(Color.FromArgb(197,232,221));
        Brush whiteBrush = new SolidBrush(Color.White);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TextRenderingHint = TextRenderingHint.AntiAlias; 

        Rectangle newRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
            ClientRectangle.Width, ClientRectangle.Height);

        // Green background
        g.FillRectangle(greenBrush, newRect);

        // Blue ellipse
        int rectX = System.Convert.ToInt32(newRect.Width * .244) * -1;
        int rectY = System.Convert.ToInt32(newRect.Height * .025);
        int rectWidth = System.Convert.ToInt32(newRect.Width * 1.416);
        int rectHeight = System.Convert.ToInt32(newRect.Height * 1.685);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // Green ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .283) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .035);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.26);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.35);
        g.FillEllipse(greenBrush, rectX, rectY, rectWidth, rectHeight);

        // Blue ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .009) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .175);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.094);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.755);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // White ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .146);
        rectY = System.Convert.ToInt32(newRect.Height * .225);
        rectWidth = System.Convert.ToInt32(newRect.Width * .928);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.545);
        g.FillEllipse(whiteBrush, rectX, rectY, rectWidth, rectHeight);

}

我已将其转换为 - 在 WPF 中:

<Grid>
    <Viewbox HorizontalAlignment="Left" VerticalAlignment="Top"  >
        <Canvas Width="1680" Height="1050">
            <Rectangle    Fill="#FF49AC71" Width="1780" Height="1050" VerticalAlignment="Stretch" />
        <Ellipse  Fill="#FFC5E8DD"  Width="2379" Height="1770" Canvas.Left="-410" Canvas.Top="25"/>
            <Ellipse   Fill=  "#FF49AC71"  Width="2116" Height="1417" Canvas.Left="-475" Canvas.Top="37"/>
            <Ellipse    Fill="#FFC5E8DD"  Width="1837" Height="1842" Canvas.Left="-15" Canvas.Top="183"/>
            <Ellipse   Fill="White"  Width="1559" Height="1622" Canvas.Left="245" Canvas.Top="236"/>
        </Canvas>
    </Viewbox>

调整大小的行为并不相同。有人可以告诉我如何解决这个问题,或者提供一个更好的 WPF 示例吗?

I have an existing OnPaint method which draws some shapes:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        g.CompositingQuality = CompositingQuality.HighQuality;

        Brush greenBrush = new SolidBrush(Color.FromArgb(73,172,113));
        Brush blueBrush = new SolidBrush(Color.FromArgb(197,232,221));
        Brush whiteBrush = new SolidBrush(Color.White);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TextRenderingHint = TextRenderingHint.AntiAlias; 

        Rectangle newRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
            ClientRectangle.Width, ClientRectangle.Height);

        // Green background
        g.FillRectangle(greenBrush, newRect);

        // Blue ellipse
        int rectX = System.Convert.ToInt32(newRect.Width * .244) * -1;
        int rectY = System.Convert.ToInt32(newRect.Height * .025);
        int rectWidth = System.Convert.ToInt32(newRect.Width * 1.416);
        int rectHeight = System.Convert.ToInt32(newRect.Height * 1.685);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // Green ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .283) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .035);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.26);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.35);
        g.FillEllipse(greenBrush, rectX, rectY, rectWidth, rectHeight);

        // Blue ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .009) * -1;
        rectY = System.Convert.ToInt32(newRect.Height * .175);
        rectWidth = System.Convert.ToInt32(newRect.Width * 1.094);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.755);
        g.FillEllipse(blueBrush, rectX, rectY, rectWidth, rectHeight);

        // White ellipse
        rectX = System.Convert.ToInt32(newRect.Width * .146);
        rectY = System.Convert.ToInt32(newRect.Height * .225);
        rectWidth = System.Convert.ToInt32(newRect.Width * .928);
        rectHeight = System.Convert.ToInt32(newRect.Height * 1.545);
        g.FillEllipse(whiteBrush, rectX, rectY, rectWidth, rectHeight);

}

I have converted this to - in WPF:

<Grid>
    <Viewbox HorizontalAlignment="Left" VerticalAlignment="Top"  >
        <Canvas Width="1680" Height="1050">
            <Rectangle    Fill="#FF49AC71" Width="1780" Height="1050" VerticalAlignment="Stretch" />
        <Ellipse  Fill="#FFC5E8DD"  Width="2379" Height="1770" Canvas.Left="-410" Canvas.Top="25"/>
            <Ellipse   Fill=  "#FF49AC71"  Width="2116" Height="1417" Canvas.Left="-475" Canvas.Top="37"/>
            <Ellipse    Fill="#FFC5E8DD"  Width="1837" Height="1842" Canvas.Left="-15" Canvas.Top="183"/>
            <Ellipse   Fill="White"  Width="1559" Height="1622" Canvas.Left="245" Canvas.Top="236"/>
        </Canvas>
    </Viewbox>

The resizing behaviour is not the same. Can someone give me a clue how I can fix this, or alternatively a better example of WPF!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2024-09-12 17:38:49

您想要设置 拉伸要填充的 ViewBox 的 属性。

<Viewbox Stretch="Fill">

默认值为 Uniform,这将保留纵横比。请参阅此博客条目如果您想了解有关如何使用 Stretch 的各种值的更多详细信息。

You want to set the Stretch property of the ViewBox to Fill.

<Viewbox Stretch="Fill">

The default value is Uniform, which will preserve the aspect ratio. See this blog entry if you want more detail on how to use the various values of Stretch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文