Witam,
Musze zrealizowac zaznaczanie pictureboxow tworzonych dyanmicznie na panelu.
Teraz mam pytanie jak sie do tego zabrac, wg mnie trzeba rysowac prostokat na oibszarze panela i wszystkie kontrolkki ktore w momencie zwolnienie przycisku myszy leza pod kwadratem zaznaczyc np borderstyle.
Nie znacie jakiegos artykulu czy moze jakies porady jak do tego podejsc?
0
0
Ja do tego przed chwilą podszedłem. :) Niestety w vb.net, bo nie mam C# w domciu. Ale jak ci bardzo potrzebne to sobie łatwo przetłumaczysz. Mark/unmark działa poprzez kliknięcie na PictureBox. Można zaznaczać wiele kontrolek poprzez rysowanie prostokąta. Wciśnięcie guzika powie Ci które są aktualnie zaznaczone, a które nie (po to żeby pokazać jak to zrobić). A oto kod:
Public Class Form1
Private Structure CMouseSelect
Public startx As Int32
Public starty As Int32
Public endx As Int32
Public endy As Int32
End Structure
Private MouseSelect As CMouseSelect
Private DrawSelection As Boolean = False
Private gfx As Graphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim picture As PictureBox
For i As Int32 = 0 To 8
picture = New PictureBox()
picture.Name = "picture" & i.ToString
picture.Width = 10
picture.Height = 10
picture.Top = 10
picture.Left = 10 + 20 * i
picture.BackColor = Color.AliceBlue
picture.Tag = New String("not selected")
AddHandler picture.Click, AddressOf HandlerForDinamicPictureBox
Me.Controls.Add(picture)
Next
gfx = Graphics.FromHwnd(Me.Handle)
End Sub
Private Sub HandlerForDinamicPictureBox(ByVal sender As Object, ByVal e As System.EventArgs)
If CType(CType(sender, PictureBox).Tag, String) = "selected" Then
CType(sender, PictureBox).Tag = "not selected"
CType(sender, PictureBox).BorderStyle = BorderStyle.None
Else
CType(sender, PictureBox).Tag = "selected"
CType(sender, PictureBox).BorderStyle = BorderStyle.FixedSingle
End If
End Sub
Private Sub CheckSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckSelected.Click
Dim message As String = ""
For Each element As Object In Me.Controls
If element.GetType.ToString = "System.Windows.Forms.PictureBox" Then
message &= CType(element, PictureBox).Name & " -> " & CType(element, PictureBox).Tag.ToString & ControlChars.CrLf
End If
Next
MsgBox(message)
End Sub
Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
MouseSelect.startx = e.X
MouseSelect.starty = e.Y
End If
DrawSelection = True
End Sub
Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
MouseSelect.endx = e.X
MouseSelect.endy = e.Y
End If
Dim picture As PictureBox
For Each element As Object In Me.Controls
If element.GetType.ToString = "System.Windows.Forms.PictureBox" Then
picture = CType(element, PictureBox)
If picture.Left > MouseSelect.startx _
AndAlso picture.Top > MouseSelect.starty _
AndAlso picture.Left + picture.Width < MouseSelect.endx _
AndAlso picture.Top + picture.Height < MouseSelect.endy Then
picture.Tag = "selected"
picture.BorderStyle = BorderStyle.FixedSingle
End If
End If
Next
DrawSelection = False
Me.Refresh()
End Sub
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If DrawSelection Then
Me.Refresh()
gfx.DrawRectangle(Pens.Black, MouseSelect.startx, MouseSelect.starty, e.X - MouseSelect.startx, e.Y - MouseSelect.starty)
End If
End Sub
End Class
I to tyle. :)