2016-12-12 77 views
-1

我想在WPF應用程序的某個窗口的特定區域上繪製一些矩形。問題是如何做到這一點,在窗口的大小改變時,圖形也會按比例調整大小? 任何提示高度讚賞。WPF中的彩色覆蓋

+0

你能不能用新窗口座標的比例重新繪製矩形? – Dia

+0

'this.SizeChanged + =(_,__)=> {//在這裏進行計算並重繪矩形}? –

回答

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); 
}