2012-05-21 3 views
-1

웹에서이 코드를 발견 했으므로 직사각형을 그리고이 안에 이미지를 유지할 수 있습니다. 그러나이 사각형을 왼쪽에서 오른쪽으로 그리고 위에서 아래로뿐만 아니라 모든 방향으로 그릴 수있는 방법이 있습니까? 도움 주셔서 감사합니다. 여기 코드입니다 :모든 방향으로 직사각형을 그리는 방법은 무엇입니까?

Public Class frmSS 
Private Declare Auto Function BitBlt Lib "gdi32.dll" (_ 
ByVal hdcDest As IntPtr, _ 
ByVal nXDest As Integer, _ 
ByVal nYDest As Integer, _ 
ByVal nWidth As Integer, _ 
ByVal nHeight As Integer, _ 
ByVal hdcSrc As IntPtr, _ 
ByVal nXSrc As Integer, _ 
ByVal nYSrc As Integer, _ 
ByVal dwRop As Int32) As Boolean 

Private Declare Auto Function GetDC Lib "user32.dll" (ByVal hWnd As IntPtr) As IntPtr 
Private Declare Auto Function ReleaseDC Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As IntPtr 

Private Sub frmSS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None 
    Me.Location = New Point(0, 0) 
    Me.ClientSize = Screen.GetBounds(Me).Size 
    Me.BackColor = Color.Gray 
    Me.DoubleBuffered = True 
    Me.Opacity = 0.4# 
    Me.Cursor = Cursors.Cross 
    Me.ShowInTaskbar = False 
End Sub 

Private isDragging As Boolean = False 
Private canDrag As Boolean = True 
Private pt_start As Point = Point.Empty 
Private pt_end As Point = Point.Empty 

Private Sub frmSS_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 
    If Me.canDrag Then 
     Me.isDragging = True 
     Me.pt_start = e.Location 
    End If 
End Sub 

Private Sub frmSS_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 
    If Me.isDragging Then 
     Me.pt_end = e.Location 
     Me.Invalidate() 
    End If 
End Sub 

Private Sub frmSS_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp 
    If Me.isDragging Then 
     Me.isDragging = False 
     Me.canDrag = False 
     Me.Cursor = Cursors.Default 
     Dim r As Rectangle = Me.SelectedRectangle 
     Me.Hide() 
     Application.DoEvents() 'Make sure everything's good and hidden. 
     Me.CaptureThisArea(r) 
     Me.Close() 
    End If 
End Sub 

Private ReadOnly Property SelectedRectangle() As Rectangle 
    Get 
     With pt_start 
      If .X >= pt_end.X OrElse .Y >= pt_end.Y Then Return Rectangle.Empty 
      Return New Rectangle(.X, .Y, pt_end.X - .X, pt_end.Y - .Y) 



     End With 
    End Get 
End Property 

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 
    Dim g As Graphics = e.Graphics 

    Using p As New Pen(Color.Black, 3) 
     p.DashStyle = Drawing2D.DashStyle.Dash 
     If Me.SelectedRectangle <> Rectangle.Empty Then 
      g.FillRectangle(Brushes.Red, Me.SelectedRectangle) 
      g.DrawRectangle(p, Me.SelectedRectangle) 
     End If 
    End Using 

    MyBase.OnPaint(e) 
End Sub 

Private Sub CaptureThisArea(ByVal area As Rectangle) 
    Dim bmp As New Bitmap(area.Width, area.Height, Imaging.PixelFormat.Format24bppRgb) 
    Using g As Graphics = Graphics.FromImage(bmp) 
     Dim srcDC As IntPtr = GetDC(IntPtr.Zero) 
     Dim destDC As IntPtr = g.GetHdc() 

     BitBlt(destDC, 0, 0, area.Width, area.Height, srcDC, area.X, area.Y, 13369376) 'SRCCOPY = 13369376 

     g.ReleaseHdc(destDC) 
     ReleaseDC(IntPtr.Zero, srcDC) 
    End Using 
    Dim s_dl As New SaveFileDialog() 
    s_dl.Filter = "Bitmap Images (*.bmp)|*.bmp" 
    If s_dl.ShowDialog() = DialogResult.Cancel Then Exit Sub 
    bmp.Save(s_dl.FileName) 
    MessageBox.Show("File saved!!!") 
End Sub 

현재 마우스 좌표가 조정에 기반해야하는 경우 최종 클래스는 최초 이는 MouseDown 점과 MouseMove 이벤트 기간 동안 기반으로 사각형을 결정하기 위해 시도해야

+1

귀하의 질문에 대한 명확하지 않습니다. 위, 아래, 왼쪽, 오른쪽 사이에는 어떤 다른 방향이 있습니까? –

+0

사각형을 회전하려고합니까? –

+0

나는 모든 방향으로 마우스를 움직이는 직사각형을 드레와하고 싶다.이 코드를 사용하면 직사각형을 선택하고 마우스를 아래쪽이나 오른쪽으로 움직일 수 있음을 알 수있다. – Valerio

답변

1

은, 참조 최소 각 X 및 Y 값의 최대 값 :

을 주석 pt_end 및 dragRect 변수를 추가

'\\ Private pt_end As Point = Point.Empty 
Private dragRect As Rectangle = Rectangle.Empty 

창 이것에 전자 당신의 MouseMove 이벤트 : 거기에서

Private Sub frmSS_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove 
    If Me.isDragging Then 
    Dim minPoint As New Point(Math.Min(e.Location.X, pt_start.X), _ 
           Math.Min(e.Location.Y, pt_start.Y)) 
    Dim maxPoint As New Point(Math.Max(e.Location.X, pt_start.X), _ 
           Math.Max(e.Location.Y, pt_start.Y)) 
    dragRect = New Rectangle(minPoint, New Size(maxPoint.X - minPoint.X, _ 
               maxPoint.Y - minPoint.Y)) 
    Me.Invalidate() 
    End If 
End Sub 

, 내가 주석 대신 SelectedRectangle의 dragRect를 사용하도록 코드를 변경합니다.

+0

고맙습니다, MouseMove 이벤트를 변경했습니다. 그러나 Selected Rectangle 대신 dragRect를 사용하는 방법을 이해하지 못했습니다. 어떻게 할 수 있습니까? 도와 주셔서 정말 감사합니다! – Valerio

+0

솔루션을 찾았습니다! 다시 감사합니다!!!! – Valerio

+0

@Pablo'SelectedRectangle' 함수를 삭제하고,'SelectedRectangle'을 사용했던 곳의 모든 참조 에러를'dragRect'로 대체하십시오. 'MouseUp'에서와 같이 : Me.CaptureThisArea (dragRect)'와'OnPaint'에서'SelectedRectangle'을'dragRect'로 대체합니다. – LarsTech

관련 문제