4

次のコードは十字を描画します。

using (SolidBrush brush = new SolidBrush(Color.FromArgb(192, 99, 104, 113)))
{
    using(GraphicsPath path = new GraphicsPath())
    {
        path.AddRectangle(new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height));
        path.AddRectangle(new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width, 40));
        path.FillMode = FillMode.Winding;
        e.Graphics.DrawPath(Pens.DimGray, path);
    }
}

ここに画像の説明を入力

私はそれを次のように描きたいと思います:

ここに画像の説明を入力

Flatten();andを使用してみましCloseAllFigures();たが、これらは機能しません。

ユニオンのようなエフェクトを探しています:

ここに画像の説明を入力

これは GraphicsPath で可能ですか?

4

4 に答える 4

1

Regions を使用することは可能ですが、他に解決策がない場合は、GDI で API FrameRgn を使用して領域のフレームを描画する必要があります。

        Graphics g = e.Graphics;
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(192, 99, 104, 113)))
        {
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddRectangle(new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height));
                path.AddRectangle(new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width, 40));
                path.FillMode = FillMode.Winding;
                using (Region region = new Region(path))
                {
                    IntPtr reg = region.GetHrgn(g);
                    IntPtr hdc = g.GetHdc();
                    IntPtr brushPtr = Win32.GetStockObject(Win32.WHITE_BRUSH);
                    IntPtr oldbrushPtr = Win32.SelectObject(hdc, brushPtr);
                    Win32.FrameRgn(hdc, reg, brushPtr, 1, 1);
                    Win32.DeleteObject(brushPtr);
                    Win32.SelectObject(hdc, oldbrushPtr);
                    region.ReleaseHrgn(reg);
                    g.ReleaseHdc();
                }
            }
        }
于 2016-04-11T01:39:31.723 に答える
0

これには Regions を使用する必要があります。これは、既存のコードから適用された例です。領域で Exclude、Intersect、および Xor を呼び出すこともできることに注意してください。:)

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        GraphicsPath Shape1 = new GraphicsPath();
        Shape1.AddRectangle(new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height));

        GraphicsPath Shape2 = new GraphicsPath();
        Shape2.AddRectangle(new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width, 40));

        Region UnitedRegion = new Region();
        UnitedRegion.MakeEmpty();
        UnitedRegion.Union(Shape1);
        UnitedRegion.Union(Shape2);

        e.Graphics.FillRegion(Brushes.Black, UnitedRegion);
    }
于 2015-06-15T06:42:00.770 に答える