VB.Net/2D/Clip

Материал из VB Эксперт
Перейти к: навигация, поиск

Clip Combine Mode Intersect

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class MainClass
  Shared Sub Main()
     Dim form1 As Form1 = new Form1
     Application.Run(form1)
  End Sub
  
End Class
Public Class Form1
  Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
    "This call is required by the Windows Form Designer.
    InitializeComponent()
    "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.IContainer
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(352, 273)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Form1"
    Me.ResumeLayout(False)
  End Sub
#End Region
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim G As Graphics = Me.CreateGraphics
    Dim R As New Rectangle(40.0F, 20.0F, 100.0F, 100.0F)
    Dim R2 As New Rectangle(60.0F, 20.0F, 120.0F, 120.0F)
    Dim P1 As New GraphicsPath()
    Dim P2 As New GraphicsPath()
    G.Clear(Color.Gainsboro)
    P1.AddEllipse(R)
    G.DrawPath(Pens.Black, P1)
    P2.AddEllipse(R2)
    G.DrawPath(Pens.Black, P2)
    G.SetClip(P1)
    G.SetClip(P2, CombineMode.Intersect)
    " Draw some clipped strings.
    Dim F As New Font("Arial", 16)
    G.DrawString("www.vbex.ru", F, Brushes.DeepPink, 15, 25)
  End Sub
End Class


Exclude Clip Path Region

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class MainClass
  Shared Sub Main()
     Dim form1 As Form1 = new Form1
     Application.Run(form1)
  End Sub
  
End Class
Public Class Form1
  Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
    "This call is required by the Windows Form Designer.
    InitializeComponent()
    "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.IContainer
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(352, 273)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Form1"
    Me.ResumeLayout(False)
  End Sub
#End Region
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim G As Graphics = Me.CreateGraphics
    Dim R As New Rectangle(40.0F, 20.0F, 100.0F, 100.0F)
    Dim path As New GraphicsPath()
    G.Clear(Color.Gainsboro)
    path.AddEllipse(R)
    G.DrawPath(Pens.Black, path)
    G.ExcludeClip(New Region(path))
    " Draw some clipped strings.
    Dim F As New Font("Arial", 16)
    G.DrawString("www.vbex.ru", F, Brushes.DeepPink, 15, 25)
  End Sub
End Class


Graphics Path Clip Region

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class MainClass
  Shared Sub Main()
     Dim form1 As Form1 = new Form1
     Application.Run(form1)
  End Sub
  
End Class
Public Class Form1
  Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
    "This call is required by the Windows Form Designer.
    InitializeComponent()
    "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.IContainer
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(352, 273)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Form1"
    Me.ResumeLayout(False)
  End Sub
#End Region
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim G As Graphics = Me.CreateGraphics
    Dim R As New Rectangle(20.0F, 20.0F, 100.0F, 100.0F)
    Dim path As New GraphicsPath()
    G.Clear(Color.Gainsboro)
    path.AddEllipse(R)
    G.DrawPath(Pens.Black, path)
    G.SetClip(path)
    Dim F As New Font("Arial", 16)
    G.DrawString("www.vbex.ru", F, Brushes.DeepPink, 15, 25)
  End Sub
End Class


Rectangle clipped to Ellipse

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New ClippingRegionForm()
        Application.Run(myform)
    End Sub
End Class

Public Class ClippingRegionForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
        Me.Text = "ClippingRegionForm"
    End Sub
#End Region
    Private Sub ClippingRegionForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim path As GraphicsPath = New GraphicsPath()
        path.AddEllipse(Me.ClientRectangle)
        Dim region As Region = New Region(path)
        g.DrawPath(Pens.Red, path)
        g.Clip = region
        Dim rect As Rectangle = Me.ClientRectangle
        rect.Offset(20, 20)
        rect.Width = rect.Width - 40
        rect.Height = rect.Height - 40
        g.FillRectangle(Brushes.Black, rect)
    End Sub
End Class


Rectangle Clip Region

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class MainClass
  Shared Sub Main()
     Dim form1 As Form1 = new Form1
     Application.Run(form1)
  End Sub
  
End Class
Public Class Form1
  Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
  Public Sub New()
    MyBase.New()
    "This call is required by the Windows Form Designer.
    InitializeComponent()
    "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.IContainer
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(352, 273)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Form1"
    Me.ResumeLayout(False)
  End Sub
#End Region
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim G As Graphics = Me.CreateGraphics
    Dim R As New Rectangle(50.0F, 50.0F, 100.0F, 100.0F)
    G.Clear(Color.Gainsboro)
    G.DrawRectangle(Pens.Black, R)
    G.Clip = New Region(R)
    G.DrawLine(Pens.Blue, 0, 75, Me.Width, 75)
    G.FillEllipse(Brushes.LawnGreen, New Rectangle(75, 75, 150, 150))
  End Sub
End Class


SetClip Demo

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim txt As String = "www.vbex.ru"
        Dim graphics_path As New GraphicsPath
        graphics_path.AddString(txt, _
            New FontFamily("Times New Roman"), _
            FontStyle.Bold, 150, _
            New Point(0, 0), _
            New StringFormat)
        e.Graphics.SetClip(graphics_path)
        e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle)
        Dim rnd As New Random
        Dim x1, y1, x2, y2 As Integer
        For i As Integer = 1 To 800
            x1 = rnd.Next(0, Me.ClientSize.Width - 1)
            y1 = rnd.Next(0, Me.ClientSize.Height - 1)
            x2 = rnd.Next(0, Me.ClientSize.Width - 1)
            y2 = rnd.Next(0, Me.ClientSize.Height - 1)
            e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2)
        Next i
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(200, 200)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class