グラフィックス 領域の利用 GDI+ での領域 領域は 出力デバイスのディスプレイ範囲の一部です 単純な領域 ( 単一の四角形 ) と複雑な領域 ( 複数の多角形と閉じた曲線の組み合わせ ) があります 四角形から構築された領域とパスから構築された領域を次の図に示します 領域の使用 領域は クリッピングとヒットテストに使用されることがよくあります クリッピングでは ディスプレイ範囲の特定の領域 ( 通常は更新が必要な部分 ) だけに描画を制限します ヒットテストでは マウスボタンが押されたときにカーソルが画面上の特定の領域内にあるかどうかを判断します 四角形またはパスから領域を構築できます 既存の領域を組み合わせて複雑な領域を作成することもできます Region クラスでは 領域を組み合わせるためのメソッドとして Intersect Union Xor Exclude および Complement が用意されています 2 つの領域の積集合は 両方の領域に属しているすべての点の集合です 和集合は いずれか一方または両方の領域に属しているすべての点の集合です 1 つの領域の補集合は その領域に属していないすべての点の集合です 上記の図にある 2 つの領域の積集合と和集合を次の図に示します Xor メソッドは 2 つの領域に対して適用され いずれか一方の領域にだけ属しているすべての点を含む領域を生成します Exclude メソッドは 2 つの領域に対して適用され 1 番目の領域には属しているが 2 番目の領域には属していないすべての点を含む領域を生成します このトピックの最初に示した 2 つの領域に対して Xor メソッドと Exclude メソッドを適用して生成された領域を次の図に示します -1-
領域を塗りつぶすには Graphics オブジェクト Brush オブジェクト および Region オブジェクトが必要です Graphics オブジェクトには FillRegion メソッドが用意されており Brush オブジェクトには色やパターンなど 塗りつぶしの属性が格納されます 純色で領域を塗りつぶす例を次に示します mygraphics.fillregion(mysolidbrush, myregion) mygraphics.fillregion(mysolidbrush, myregion); GDI+ での描画サーフェイスの制限 クリッピングでは 特定の四角形または領域だけに描画を制限します 文字列 "Hello" をハート形の領域に合わせてクリップした例を次の図に示します 領域のクリッピング 領域はパスから構築でき パスは文字列のアウトラインを含むことができるため テキストのアウトラインをクリッピングに使用できます 複数の同心楕円をテキスト文字列の内部に合わせてクリップした例を次の図に示します クリッピングを使用して描画するには Graphics オブジェクトを作成し このオブジェクトの Clip プロパティを設定してから 同じ Graphics オブジェクトの描画メソッドを呼び出します mygraphics.clip = myregion mygraphics.drawline(mypen, 0, 0, 200, 200) mygraphics.clip = myregion; mygraphics.drawline(mypen, 0, 0, 200, 200); クリッピング領域 Graphics クラスのプロパティの 1 つとしてクリップ領域があります 指定の Graphics オブジェク -2-
トによって行われるすべての描画は その Graphics オブジェクトのクリップ領域に限定されます クリップ領域を設定するには SetClip メソッドを呼び出します 使用例 1 つの多角形だけから成るパスを作成する例を次に示します このコードは 次に そのパスに基づいて領域を作成します 作成された領域は Graphics オブジェクトの SetClip メソッドに渡され 2 つの文字列が描画されます クリップされた文字列を次の図に示します ' Create a path that consists of a single polygon. Dim polypoints As Point() = { _ New Point(10, 10), _ New Point(150, 10), _ New Point(100, 75), _ New Point(100, 150)} Dim path As New GraphicsPath() path.addpolygon(polypoints) ' Construct a region based on the path. Dim [region] As New [Region](path) ' Draw the outline of the region. Dim pen As Pen = Pens.Black e.graphics.drawpath(pen, path) ' Set the clipping region of the Graphics object. e.graphics.setclip([region], CombineMode.Replace) ' Draw some clipped strings. Dim fontfamily As New FontFamily("Arial") Dim font As New Font( _ fontfamily, _ 36, _ FontStyle.Bold, _ GraphicsUnit.Pixel) Dim solidbrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0)) -3-
e.graphics.drawstring( _ "A Clipping Region", _ font, _ solidbrush, _ New PointF(15, 25)) e.graphics.drawstring( _ "A Clipping Region", _ font, _ solidbrush, _ New PointF(15, 68)) // Create a path that consists of a single polygon. Point[] polypoints = { new Point(10, 10), new Point(150, 10), new Point(100, 75), new Point(100, 150)}; GraphicsPath path = new GraphicsPath(); path.addpolygon(polypoints); // Construct a region based on the path. Region region = new Region(path); // Draw the outline of the region. Pen pen = Pens.Black; e.graphics.drawpath(pen, path); // Set the clipping region of the Graphics object. e.graphics.setclip(region, CombineMode.Replace); // Draw some clipped strings. FontFamily fontfamily = new FontFamily("Arial"); Font font = new Font( fontfamily, 36, FontStyle.Bold, GraphicsUnit.Pixel); SolidBrush solidbrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0)); e.graphics.drawstring( "A Clipping Region", font, solidbrush, new PointF(15, 25)); e.graphics.drawstring( "A Clipping Region", font, -4-
solidbrush, new PointF(15, 68)); 前述の例は Windows フォームと一緒に使用することが想定されていて PaintEventHandler のパラメータである PaintEventArgs e が必要です 領域でヒットテスト ヒットテストの目的は アイコンやボタンなどの特定のオブジェクト上にカーソルが配置されているかどうかを確認することです 使用例 2 つの四角形の領域の交差させることにより 十字型の領域を作成する例を次に示します 変数 point が 最新のクリック位置を保持しているとします このコードは point が十字型の領域内にあるかどうかを確認します point が保持している位置が領域内にある場合は 領域が不透明な赤いブラシで塗りつぶされます 領域内にはない場合 領域は半透明の赤いブラシで塗りつぶされます Dim point As New Point(60, 10) ' Assume that the variable "point" contains the location of the ' most recent mouse click. ' To simulate a hit, assign (60, 10) to point. ' To simulate a miss, assign (0, 0) to point. Dim solidbrush As New SolidBrush(Color.Black) Dim region1 As New [Region](New Rectangle(50, 0, 50, 150)) Dim region2 As New [Region](New Rectangle(0, 50, 150, 50)) ' Create a plus-shaped region by forming the union of region1 and region2. ' The union replaces region1. region1.union(region2) If region1.isvisible(point, e.graphics) Then ' The point is in the region. Use an opaque brush. solidbrush.color = Color.FromArgb(255, 255, 0, 0) Else ' The point is not in the region. Use a semitransparent brush. solidbrush.color = Color.FromArgb(64, 255, 0, 0) End If e.graphics.fillregion(solidbrush, region1) Point point = new Point(60, 10); // Assume that the variable "point" contains the location of the -5-
// most recent mouse click. // To simulate a hit, assign (60, 10) to point. // To simulate a miss, assign (0, 0) to point. SolidBrush solidbrush = new SolidBrush(Color.Black); Region region1 = new Region(new Rectangle(50, 0, 50, 150)); Region region2 = new Region(new Rectangle(0, 50, 150, 50)); // Create a plus-shaped region by forming the union of region1 and // region2. // The union replaces region1. region1.union(region2); if (region1.isvisible(point, e.graphics)) { // The point is in the region. Use an opaque brush. solidbrush.color = Color.FromArgb(255, 255, 0, 0); } else { // The point is not in the region. Use a semitransparent brush. solidbrush.color = Color.FromArgb(64, 255, 0, 0); } e.graphics.fillregion(solidbrush, region1); 前述の例は Windows フォームと一緒に使用することが想定されていて PaintEventHandler のパラメータである PaintEventArgs e が必要です -6-