在 UWP 应用中,OnPaint 方法是什么?

huangapple go评论74阅读模式
英文:

What Is The OnPaint Method In UWP Apps

问题

我正在开发一个UWP应用程序,并且正在创建自己的UI组件。到目前为止,我一直在使用WinForms,那里有一个OnPaint方法,我可以用它来重写自定义组件的绘制方式。现在我在使用UWP,我想知道是否有类似的方法可以用来绘制自定义组件。

我的一个OnPaint方法过去是这样的:

protected override void OnPaint(PaintEventArgs e)
{
    int buttonHeight = Height;
    int buttonWidth = Width;

    int imgHeight = Height;
    int imgWidth = Height;
    int textPadding = (Height - FontHeight) / 2;
    int textStartX = imgWidth + textPadding;
    int textStartY = textPadding - 3; // 必须减去3,否则它会偏离中心


    Graphics g = e.Graphics;

    FillRoundedRectangle(g, new SolidBrush(ButtonColor()), new Rectangle(0, 0, Width, Height), 10); // 背景

    if (ImageOnly == Mode.True)
    {
        g.DrawImage(Image, new Rectangle((buttonWidth / 2) - (imgWidth / 2), (buttonHeight / 2) - (imgHeight / 2), imgWidth, imgHeight));
    }
    else
    {
        g.DrawImage(Image, new Rectangle(0, 0, imgWidth, imgHeight));
        g.DrawString(Text, ButtonFont, new SolidBrush(Foreground), new Point(textStartX, textStartY));
    }
}
英文:

I'm working on a UWP app, and I'm creating my own UI components. Until now, I've been working with WinForms, and there's the OnPaint method, which I could use to override how a custom component would be drawn. Now that I'm working with UWP, I was wondering if there is a similar method that I could use to draw custom components.

One of my OnPaint methods used to look like this:

protected override void OnPaint(PaintEventArgs e)
{
    int buttonHeight = Height;
    int buttonWidth = Width;

    int imgHeight = Height;
    int imgWidth = Height;
    int textPadding = (Height - FontHeight) / 2;
    int textStartX = imgWidth + textPadding;
    int textStartY = textPadding - 3; // Have to get rid of 3, otherwise it's off center


    Graphics g = e.Graphics;

    FillRoundedRectangle(g, new SolidBrush(ButtonColor()), new Rectangle(0, 0, Width, Height), 10); // Background

    if (ImageOnly == Mode.True)
    {
        g.DrawImage(Image, new Rectangle((buttonWidth / 2) - (imgWidth / 2), (buttonHeight / 2) - (imgHeight / 2), imgWidth, imgHeight));
    }
    else
    {
        g.DrawImage(Image, new Rectangle(0, 0, imgWidth, imgHeight));
        g.DrawString(Text, ButtonFont, new SolidBrush(Foreground), new Point(textStartX, textStartY));
    }
}

答案1

得分: 1

UWP没有像OnPaint这样的方法,但您可以尝试在UWP的用户控件中使用Win2D CanvasDraw

右键单击您的项目-> 选择添加 -> 新项目 -> 选择用户控件

MyUserControl1.xaml
<Grid>
    <canvas:CanvasControl Draw="CanvasControl_Draw" ClearColor="CornflowerBlue"/>
</Grid>

MyUserControl1.xaml.cs
public sealed partial class MyUserControl1 : UserControl
{
    public MyUserControl1()
    {
        this.InitializeComponent();

    }

    void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
    {
        args.DrawingSession.DrawEllipse(155, 115, 80, 30, Colors.Black, 3);
        args.DrawingSession.DrawText("Hello, world!", 100, 100, Colors.Yellow);
    }
}
MainPage.xaml
<Grid>
   <local:MyUserControl1/>
</Grid>
英文:

UWP doesn't have a method like OnPaint, but you can try using Win2D CanvasDraw in UWP User Control.

Right-click your project-> Select Add -> New Item -> Choose User Control

MyUserControl1.xaml
<Grid>
    <canvas:CanvasControl Draw="CanvasControl_Draw" ClearColor="CornflowerBlue"/>
</Grid>

MyUserControl1.xaml.cs
public sealed partial class MyUserControl1 : UserControl
{
    public MyUserControl1()
    {
        this.InitializeComponent();

    }

    void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
    {
        args.DrawingSession.DrawEllipse(155, 115, 80, 30, Colors.Black, 3);
        args.DrawingSession.DrawText("Hello, world!", 100, 100, Colors.Yellow);
    }
}
MainPage.xaml
<Grid>
   <local:MyUserControl1/>
</Grid>

huangapple
  • 本文由 发表于 2023年6月14日 23:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定