英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论