본문 바로가기
카테고리 없음

[vb.net] 주어진 비율을 유지하면서 마우스로 사각형을 그립니다

by IT HUB 2021. 1. 30.
728x90
반응형
   Dim mRect As Rectangle
    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        mRect = New Rectangle(e.X, e.Y, 00)
        Me.Invalidate()
    End Sub
 
    Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mRect = New Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top)
            'Replace 1.5 with the scale you want to use
            Dim hgt As Integer = Convert.ToInt32(mRect.Height / 1.5)
            Dim wdth As Integer = Convert.ToInt32(mRect.Width / 1.5)
            mRect.Size = New Size(wdth * 1.5, hgt * 1.5)
            Me.Invalidate()
        End If
    End Sub
 
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Using pen As New Pen(Color.Red, 3)
            e.Graphics.DrawRectangle(pen, mRect)
        End Using
    End Sub
반응형

댓글