Graphics.SetClip 用法介绍

1.Graphics 对象做为GDI+ 图形绘制接口,其对绘制区域剪辑,通过 SetClip() 方法实现。 

比较常用 方法如: SetClip(Rectangle, CombineMode)

Rectangle :选择区域范围

CombineMode: Rectangle 所代表区域与Graphics 已选择范围之间作用关系,如 交集,并集,或者替换等,详细介绍:

 public enum CombineMode
    {
        //
        // 摘要:
        //     另一种将替换为一个剪辑区域。
        Replace = 0,
        //
        // 摘要:
        //     通过采用它们的交集组合两个剪辑区域。
        Intersect = 1,
        //
        // 摘要:
        //     通过采用这两者的 union 组合两个剪辑区域。
        Union = 2,
        //
        // 摘要:
        //     两个剪辑区域是组合采取相应的区域括起,一项或在其他区域中,但不是能同时。
        Xor = 3,
        //
        // 摘要:
        //     指定正在从现有的区域中删除的新区域的结果替换为现有区域。 换言之,从现有区域中排除的新区域。
        Exclude = 4,
        //
        // 摘要:
        //     指定正在从新的区域中删除现有区域的结果替换为现有区域。 换言之,从新区域中排除现有的区域。
        Complement = 5
    }

2.测试CombineMode 不同类型 效果:

private void SetClipRectangleFCombine(PaintEventArgs e)
        {
            Graphics newGraphics = e.Graphics;

            if (state == 1)
            {
                //左上角矩形
                newGraphics.SetClip(new Rectangle(0, 0, 100, 100));
            }
            else if (state == 2)
            {
                //右下角矩形
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100));
            }
            else if (state == 3)
            {
                //通过采用这两者的 union 组合两个剪辑区域
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100), CombineMode.Replace);
                newGraphics.SetClip(new Rectangle(0, 80, 100, 100), CombineMode.Union);
            }
            else if (state == 4)
            {
                //两个剪辑区域是组合采取相应的区域括起,一项或在其他区域中,但不是能同时
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100), CombineMode.Replace);
                newGraphics.SetClip(new Rectangle(0, 80, 100, 100), CombineMode.Xor);
            }
            else if (state == 5)
            {
                //通过采用它们的交集组合两个剪辑区域
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100), CombineMode.Replace);
                newGraphics.SetClip(new Rectangle(0, 80, 100, 100), CombineMode.Intersect);
            }
            else if (state == 6)
            {
                //指定正在从新的区域中删除现有区域的结果替换为现有区域。 换言之,从新区域中排除现有的区域
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100), CombineMode.Replace);
                newGraphics.SetClip(new Rectangle(0, 80, 100, 100), CombineMode.Complement);
            }
            else if (state == 7)
            {
                //指定正在从现有的区域中删除的新区域的结果替换为现有区域。 换言之,从现有区域中排除的新区域
                newGraphics.SetClip(new Rectangle(50, 50, 100, 100), CombineMode.Replace);
                newGraphics.SetClip(new Rectangle(0, 80, 100, 100), CombineMode.Exclude);
            }

            e.Graphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, 500, 500);

             //恢复Clip 为无限区域,重新绘制
             //e.Graphics.ResetClip();
             //e.Graphics.FillRectangle(new SolidBrush(Color.Black), 75, 75 , 200, 200);

        }

3.运行结果:

state:1

state 2:

state 3:

state 4:

state 5:

state 6:

 state 7:

原文地址:https://www.cnblogs.com/howtrace/p/11491525.html