[ VB 2008 ] DrawRectangle 예제 본문

[PL]/VB & VB.NET

[ VB 2008 ] DrawRectangle 예제

객과 함께. 2010. 1. 8. 20:28

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Graphics

Public Class DrawRectangle

    ' 프로그램명 : 사각형 그리기
    ' 제작일 : 2010. 01. 08
    ' 제작자 : 객과 함께.

    Dim RcDraw As Rectangle
    Dim Rc As New Rectangle


    Private Sub Form3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        RcDraw.X = e.X
        RcDraw.Y = e.Y
    End Sub

    Private Sub Form3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

        If e.X < RcDraw.X Then
            Rc.Width = RcDraw.X - e.X
            RcDraw.X = e.X
        Else
            Rc.Width = e.X - RcDraw.X
        End If

        If e.Y < RcDraw.Y Then
            Rc.Height = RcDraw.Y - e.Y
            RcDraw.Y = e.Y
        Else
            Rc.Height = e.Y - RcDraw.Y
        End If

        If Rc.Width = 0 And Rc.Height = 0 Then Exit Sub
        Form3Paint()
    End Sub

    Private Sub Form3Paint()
        Dim redPen As New Pen(Color.Red, 1)
        Dim G As Graphics = Me.CreateGraphics()

        G.DrawRectangle(redPen, RcDraw.X, RcDraw.Y, Rc.Width, Rc.Height)
    End Sub

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Me.Text = " DrawRectangle 예제 "
    End Sub
End Class