简体   繁体   English

在WPF中的MainWindow上绘画

[英]Painting on the MainWindow in WPF

I try to paint on my MainWindow. 我试着在我的MainWindow上画画。 I use this sample code: 我用这个示例代码:

MainWindow.xaml.cs MainWindow.xaml.cs

namespace WpfApplication4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Trace.WriteLine("OnRender");

            drawingContext.DrawRectangle(Brushes.Red, new Pen(Brushes.Black, 5), new Rect(20, 20, 250, 250));

            base.OnRender(drawingContext);
        }
    }
}

MainWindow.xaml MainWindow.xaml

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="600">
</Window>

In the output window I see the message "OnRender", but nothing is drawn. 在输出窗口中,我看到消息“OnRender”,但没有绘制任何内容。 MainWindow和输出

What am I doing wrong? 我究竟做错了什么?

Set Background="Transparent" in the Window's XAML. 在Window的XAML中设置Background="Transparent"

And always call base.OnRender before your own drawings, since you want to draw over the base class drawings (if any). 并始终调用base.OnRender自己的图纸之前,因为要绘制基类图纸(如果有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM