2011-10-29 211 views
0

我在Visual Studio中的wpf畫布上繪製形狀。在矩形的內部右側「添加」兩個小圓圈的(最佳)方式是什麼?我希望它們在用戶面前看起來像是矩形上的「小洞」。我是否應該獲得矩形右側的座標並通過計算圓心的相應要求座標(我希望它們對稱地位於矩形中間的上方和下方)繪製圓? Canvas是GetRight獲取矩形右側座標的合適方法嗎?我如何將它應用於代碼上:在矩形上繪製圓形

shapeToRender = new Rectangle() { Fill = Brushes.Red, Height = 50, Width = 50, RadiusX = 10, RadiusY = 10 }; 

Canvas.SetLeft(shapeToRender, e.GetPosition(canvasDrawingArea).X - rectWidth/2); 
Canvas.SetTop(shapeToRender, e.GetPosition(canvasDrawingArea).Y - rectHeight/2); 

canvasDrawingArea.Children.Add(shapeToRender); 

形狀由MouseEnter事件創建。

+1

最好的方法是什麼?計算最少的方法或最容易理解的方法? – AlexSavAlexandrov

回答

3

這是你想要的類型嗎?

<Grid x:Class="yourNs.RectangleWithCircles" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Height="50" Width="50"> 
    <Rectangle Fill="Red"/> 
    <UniformGrid Rows="2"> 
     <Ellipse Grid.Row="0" Fill="White" Height="10" Width="10" HorizontalAlignment="Right" VerticalAlignment="Center" /> 
     <Ellipse Grid.Row="1" Fill="White" Height="10" Width="10" HorizontalAlignment="Right" VerticalAlignment="Center" /> 
    </UniformGrid> 
</Grid> 

哪裏yourNs爲您提供的命名空間,你想補充<yourNs:RectangleWithCircles />到畫布:

enter image description here

這是使用來完成。

因爲您創建了RectangleWithCircles類,所以您可以通過添加公共方法來顯示或隱藏圓圈,從而輕鬆地對其進行自定義以滿足您的需求。