Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - Общий форум

Страница: 1 |

 

  Вопрос: Как выделить прорисовать выделение на форме Добавлено: 18.02.14 12:27  

Автор вопроса:  Cramper
Такая проблема. На форме программно нарисовано что-то, например геометрические фигуры. Нужно очертить, выделить мышкой на форме какую-то прямоугольную область, не искажая рисунка.
VB.Net 2008. Заранее спасибо за помощь.

Ответить

  Ответы Всего ответов: 1  

Номер ответа: 1
Автор ответа:
 Дмитрий Юпатов



Вопросов: 4
Ответов: 457
 Web-сайт: cargomaster.at.ua/
 Профиль | | #1
Добавлено: 16.06.14 09:49
Создаем класс:
  1. Public Class RubberBand
  2.     ' ----- The three types of rubber bands.
  3.     Public Enum RubberBandStyle
  4.         DashedLine
  5.         ThickLine
  6.         SolidBox
  7.         SolidBoxWithDashedLine
  8.     End Enum
  9.  
  10.     ' ----- The current drawing state.
  11.     Public Enum RubberBandState
  12.         Inactive
  13.         FirstTime
  14.         Active
  15.     End Enum
  16.  
  17.     ' ----- Class-level variables.
  18.     Private BasePoint As Point
  19.     Private ExtentPoint As Point
  20.     Private CurrentState As RubberBandState
  21.     Private BaseControl As Control
  22.     Public Style As RubberBandStyle
  23.     Public BackColor As Color
  24.     Public Sub New(ByVal useControl As Control, _
  25.           Optional ByVal useStyle As RubberBandStyle = _
  26.           RubberBandStyle.DashedLine)
  27.         ' ----- Constructor with one or two parameters.
  28.         BaseControl = useControl
  29.         Style = useStyle
  30.         BackColor = Color.Black
  31.     End Sub
  32.  
  33.     Public Sub New(ByVal useControl As Control, ByVal useStyle As RubberBandStyle, ByVal useColor As Color)
  34.         ' ----- Constructor with three parameters.
  35.         BaseControl = useControl
  36.         Style = useStyle
  37.         BackColor = useColor
  38.     End Sub
  39.  
  40.     Public ReadOnly Property Rectangle() As Rectangle
  41.         Get
  42.             ' ----- Return the bounds of the  rubber-band area.
  43.             Dim result As Rectangle
  44.  
  45.             ' ----- Ensure the coordinates go left to
  46.             ' right, top to bottom.
  47.             result.X = IIf(BasePoint.X < ExtentPoint.X, _
  48.                BasePoint.X, ExtentPoint.X)
  49.             result.Y = IIf(BasePoint.Y < ExtentPoint.Y, _
  50.                BasePoint.Y, ExtentPoint.Y)
  51.             result.Width = Math.Abs(ExtentPoint.X - BasePoint.X)
  52.             result.Height = Math.Abs(ExtentPoint.Y - BasePoint.Y)
  53.             Return result
  54.         End Get
  55.     End Property
  56.  
  57.     Public Sub Start(ByVal x As Integer, ByVal y As Integer)
  58.         ' ----- Start drawing the rubber band. The user must
  59.         '       call Stretch() to actually draw the first
  60.         '       band image.
  61.         BasePoint.X = x
  62.         BasePoint.Y = y
  63.         ExtentPoint.X = x
  64.         ExtentPoint.Y = y
  65.         Normalize(BasePoint)
  66.         CurrentState = RubberBandState.FirstTime
  67.     End Sub
  68.  
  69.     Public Sub Stretch(ByVal x As Integer, ByVal y As Integer)
  70.         ' ----- Change the size of the rubber band.
  71.         Dim newPoint As Point
  72.  
  73.         ' ----- Prepare the new stretch point.
  74.         newPoint.X = x
  75.         newPoint.Y = y
  76.         Normalize(newPoint)
  77.  
  78.         Select Case CurrentState
  79.             Case RubberBandState.Inactive
  80.                 ' ----- Rubber band not in use.
  81.                 Return
  82.             Case RubberBandState.FirstTime
  83.                 ' ----- Draw the initial rubber band.
  84.                 ExtentPoint = newPoint
  85.                 DrawTheRectangle()
  86.                 CurrentState = RubberBandState.Active
  87.             Case RubberBandState.Active
  88.                 ' ----- Undraw the previous band, then
  89.                 '       draw the new one.
  90.                 DrawTheRectangle()
  91.                 ExtentPoint = newPoint
  92.                 DrawTheRectangle()
  93.         End Select
  94.     End Sub
  95.  
  96.     Public Sub Finish()
  97.         ' ----- Stop drawing the rubber band.
  98.         DrawTheRectangle()
  99.         CurrentState = 0
  100.     End Sub
  101.  
  102.     Private Sub Normalize(ByRef whichPoint As Point)
  103.         ' ----- Don't let the rubber band go outside the view.
  104.         If (whichPoint.X < 0) Then whichPoint.X = 0
  105.         If (whichPoint.X >= BaseControl.ClientSize.Width) Then whichPoint.X = BaseControl.ClientSize.Width - 1
  106.  
  107.         If (whichPoint.Y < 0) Then whichPoint.Y = 0
  108.         If (whichPoint.Y >= BaseControl.ClientSize.Height) Then whichPoint.Y = BaseControl.ClientSize.Height - 1
  109.     End Sub
  110.  
  111.     Private Sub DrawTheRectangle()
  112.         ' ----- Draw the rectangle on the control or
  113.         '       form surface.
  114.         Dim drawArea As Rectangle
  115.         Dim screenStart, screenEnd As Point
  116.  
  117.         ' ----- Get the square that is the  rubber-band area.
  118.         screenStart = BaseControl.PointToScreen(BasePoint)
  119.         screenEnd = BaseControl.PointToScreen(ExtentPoint)
  120.         drawArea.X = screenStart.X
  121.         drawArea.Y = screenStart.Y
  122.         drawArea.Width = (screenEnd.X - screenStart.X)
  123.         drawArea.Height = (screenEnd.Y - screenStart.Y)
  124.  
  125.         ' ----- Draw using the user-selected style.
  126.         Select Case Style
  127.             Case RubberBandStyle.DashedLine
  128.                 ControlPaint.DrawReversibleFrame( _
  129.                    drawArea, BackColor, FrameStyle.Dashed)
  130.             Case RubberBandStyle.ThickLine
  131.                 ControlPaint.DrawReversibleFrame( _
  132.                    drawArea, BackColor, FrameStyle.Thick)
  133.             Case RubberBandStyle.SolidBox
  134.                 ControlPaint.FillReversibleRectangle( _
  135.                    drawArea, BackColor)
  136.             Case RubberBandStyle.SolidBoxWithDashedLine
  137.                 ControlPaint.FillReversibleRectangle( _
  138.                    drawArea, BackColor)
  139.                 ControlPaint.DrawReversibleFrame( _
  140.                    drawArea, Color.Black, FrameStyle.Dashed)
  141.         End Select
  142.     End Sub
  143. End Class

В коде формы
  1. Friend SelectionArea As RubberBand

И, например в процедуре загрузки формы
  1. SelectionArea = New RubberBand(Me.Poligon, RubberBand.RubberBandStyle.DashedLine, Color.FromArgb(255, 0, 255))

где poligon - контрол, на котором рисуем выделение (если форма - me)
RubberBand.RubberBandStyle.DashedLine - слить очерчивания (описан в классе rubberband)
Последний параметр - Цвет области выделения
И работа с ним при нажатии на кнопку мыши в пределах контрола, на котором рисуем область (mouse_down)
  1. SelectionArea.Start(e.X, e.Y)

при очерчивании прямоугольника (при нажатой кнопке мыши (mousemove)
  1. If e.Button = Windows.Forms.MouseButtons.Left Then
  2.             SelectionArea.Stretch(e.X, e.Y)
  3.         End If

Ну и при отпускании кнопки, в конце очерчивания области (mouseup)
  1. SelectionArea.Finish()

В итоге объект SelectionArea будет иметь в своей структуре координаты прямоугольной области выделения.
Ну а дальше с этой информацией уже обрабатывайте то, что выделяли.

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам