WPF中的彩色叠加层

时间:2016-12-12 07:44:07

标签: wpf canvas overlay

我想在WPF应用程序的窗口的某个区域上绘制一些矩形。问题是如何以这样的方式做到这一点:在窗口大小改变期间,数字也会按比例调整大小? 任何提示都受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

您可以在窗口的代码后面执行类似的操作:

//Constructor
public MyWindow() {

   InitializeComponent();

   //Register the SizeChanged-event
   this.SizeChanged += OnSizeChanged;
}

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
   var parentWindow = (Window)sender;
   var sizeOfRectangle = CalculateSizeOfRectangle(parentWindow.actualHeight, parentWindow.actualWidth);

   // assuming you have a reference to your colored rectangle here (e.g. Name defined in XAML code)
   ColoredRectangle.Width = sizeOfRectangle.Width;
   ColoredRectangle.Height = sizeOfRectangle.Height;
}

private Size CalculateSizeOfRectangle(double height, double width)
{
    int height = ....; // Calculate height here
    int width = ....;  // Calculate width her
    return new Size(width, height);
}