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:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM