在矩形上绘制圆 [英] Drawing circles on a Rectangle

查看:136
本文介绍了在矩形上绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio中的WPF中的画布上绘制形状。在矩形的右内侧添加两个小圆圈的最佳方法是什么?我希望它们在用户看来像是矩形上的小孔。我是否应该获得矩形右侧的坐标并通过计算圆心的相应要求坐标(我希望它们对称地位于矩形中间的上方和下方)来绘制圆? Canvas GetRight是获取矩形右侧坐标的适当方法吗?我如何在代码上应用它:

I am drawing shapes on a canvas in wpf in visual studio. What is the (best) way to "add" two small circles on the inner right side of a Rectangle? I want them to seem to the user as "small holes" on the Rectangle. Should I get the coordinates of the right side of the Rectangle and by computing the respective demanded coordinates of the circles’ centers (I want them to be above and below of the middle of the rectangle symmetrically) to draw the circles? Is Canvas GetRight the appropriate method to get the coordinates of the right side of the Rectangle? How I apply it on the code:

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事件创建的。

The shapes are created by a MouseEnter event.

推荐答案

这是您要使用的东西类型吗?

Is this the type of thing you are going for?

使用以下方法完成:

<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 />

Where yourNs is the namespace you provide, you would just add <yourNs:RectangleWithCircles /> to the canvas.

因为您创建了 RectangleWithCircles 类,例如,您可以通过添加显示或隐藏圆圈的公共方法轻松地对其进行自定义,以满足自己的需求。

Because you have created the RectangleWithCircles class, you can easily customize it to fit your needs by adding a public method to show or hide the circles, for example.

这篇关于在矩形上绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆