I want to place my Crop Control on center of image of picture box. I have tried following code
Dim oCropControl As new CropControl
Dim oControlLocation As Point
oControlLocation = New Point(peImageViewer.Width / 2, peImageViewer.Height / 2)
oCropControl.Location = New Point(oControlLocation.X, oControlLocation.Y)
But this is not working well.. :( Crop Control shown in bottom.
Thanks in advance!!
Assuming they are both parented to the same control you could do it like this:
Dim rect1 As Rectangle = Me.myPictureBox.Bounds
Dim rect2 As Rectangle = Me.myCropControl.Bounds
rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))
Me.myCropControl.Bounds = rect2
Me.myCropControl.BringToFront()
Example
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.Size = New Size(400, 400)
Me.StartPosition = FormStartPosition.CenterScreen
Me.myButton = New Button() With {.Location = New Point(3, 3), .Text = "ALIGN!"}
Me.myCropControl = New Label() With {.Bounds = New Rectangle(245, 263, 60, 60), .BackColor = Color.Blue, .ForeColor = Color.White, .Text = "CROP", .TextAlign = ContentAlignment.MiddleCenter}
Me.myPictureBox = New PictureBox() With {.Bounds = New Rectangle(23, 56, 246, 143), .BackColor = Color.Red}
Me.Controls.AddRange({Me.myButton, Me.myCropControl, Me.myPictureBox})
End Sub
Private Sub Align(sender As Object, e As EventArgs) Handles myButton.Click
Dim rect1 As Rectangle = Me.myPictureBox.Bounds
Dim rect2 As Rectangle = Me.myCropControl.Bounds
rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))
Me.myCropControl.Bounds = rect2
Me.myCropControl.BringToFront()
End Sub
Private WithEvents myButton As Button
Private myCropControl As Label
Private myPictureBox As PictureBox
End Class
Related
I am trying to move a Label object from a Panel object, in this case I have a parent panel that has Panel and Label objects as child, these are dynamically created. The objective is that when executing from the ToolStripMenuItem, when the Panel object is moved, the Label object is also moved.
I made the following code, but I think I could not move the Label object. From what I understand, what I am doing is generating the variable that is called the same as the Label that I need, but what I need is to refer to the existing object, not the new one. (this is correct?)
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
Dim posX = clickedPanel.Location.X + 120
Dim posY = clickedPanel.Location.Y
Dim namelabel As New Label With {
.Name = "Label" & clickedPanel.Name.Last
}
namelabel.Location = New Point((posX), posY)
End Sub
Could you please guide me?
Note: I Forgot something, in this case, If I move the Panel1, Label1 will move too, if I move Panel2, Label2 will move too, etc, etc.
This is the code, where the label is created inside the panel dynamically, and move together.
Private Sub NavButton15_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton15.ElementClick
Dim pos As Int32 = 50
Dim poslabel As Int16 = 26
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Dim counter as Int16 = 1
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\Example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & counter
}
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Panel " & counter,
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & counter
}
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
End Sub
This is the ToolStripMenuItem where move the panel whit the label.
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub
I was trying to build a custom CheckBox, but I see a black rectangle around it. Why does this happen? And why the second CheckBox has the first overlapped?
It looks like there's a black rectangle in a default position of the control. When I add one more CheckBox, the second looks like 2 CheckBox-es in the same position.
Public Class mycheckbox
Inherits CheckBox
Protected Overrides Sub onpaint(pevent As PaintEventArgs)
pevent.Graphics.FillRectangle(New SolidBrush(BackColor), Location.X, Location.Y, Width, Height)
Dim brsh As New SolidBrush(Color.YellowGreen)
Dim boxside As Integer = CInt(pevent.Graphics.MeasureString(Text, Font, Width).Height)
pevent.Graphics.FillRectangle(brsh, Location.X, Location.Y, Width, Height)
If Checked And Enabled Then
pevent.Graphics.DrawImage(My.Resources.X, Location.X + 1, Location.Y + 1, 18, 18)
pevent.Graphics.DrawRectangle(Pens.Black, New Rectangle(Location.X, Location.Y, 19, 19))
pevent.Graphics.DrawString(Text, Font, Brushes.Black, boxside + 15, 11)
End If
brsh.Dispose()
End Sub
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mcb1 As New mycheckbox
Dim mcb2 As New mycheckbox
mcb1.Name = "cb1"
mcb1.Text = "Hello!!!"
mcb1.Location = New Point(10, 10)
mcb1.Size = New Size(300, 30)
mcb2.Name = "cb2"
mcb2.Text = "Hi!!!"
mcb2.Location = New Point(10, 50)
mcb2.Size = New Size(300, 30)
Me.Controls.Add(mcb1)
Me.Controls.Add(mcb2)
End Sub
I am trying to make a game where I bounce a ball off of the ground which is drawn as a closed curve (The green part), I just don't know how I would go about calculating the collision between the ball and curve.
I've drawn both the ball and curve using graphics in a picturebox, I imagine I have to do it mathematically as I can't find any builtin functionality in Visual basic that lets me do it.
My code:
Public Class Form1
Dim BallSpeedY, BallSpeedX As Double
Dim BallLoc As Point
Dim Start As Boolean = False
Dim gameTime As Decimal = 60.1
Dim gameTimeFont As New Font("Arial", 24, FontStyle.Bold)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
'Size of the picturebox that is being drawn on
PictureBox1.Width = Me.Width
PictureBox1.Height = Me.Height - 24
DrawGame(False, False, True)
End Sub
Private Sub DrawGame(refreshMap As Boolean, drawBall As Boolean, drawTime As Boolean)
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
Static startcornerpt As PointF
Static firstpt As PointF
Static pt2 As PointF
Static pt3 As PointF
Static pt4 As PointF
Static pt5 As PointF
Static lastpt As PointF
Static Endcornerpt As PointF
' Clears the window
g.Clear(Color.White)
If refreshMap Then
Randomize() ' The rnd seed would always be the same if this is not done
' Creates 5 points chosen at random positions on the window within certain parameters
startcornerpt = New PointF(0, Me.Height)
pt2 = New PointF(Me.Width * 0.2, Me.Height * ((39 * Rnd() + 30) / 100))
firstpt = New PointF(0, pt2.Y * 0.5 * 2)
pt3 = New PointF(Me.Width * 0.4, Me.Height * ((39 * Rnd() + 30) / 100))
pt4 = New PointF(Me.Width * 0.6, Me.Height * ((39 * Rnd() + 30) / 100))
pt5 = New PointF(Me.Width * 0.8, Me.Height * ((39 * Rnd() + 30) / 100))
lastpt = New PointF(Me.Width, pt5.Y * 0.5 * 2)
Endcornerpt = New PointF(Me.Width, Me.Height)
End If
' Draws the map with the 5 points
Dim curvepoints As PointF() = {startcornerpt, firstpt, pt2, pt3, pt4, pt5, lastpt, Endcornerpt}
g.FillClosedCurve(Brushes.PaleGreen, curvepoints)
If drawBall Then
' Draws the ball
g.DrawEllipse(Pens.Blue, BallLoc.X, BallLoc.Y, 20, 20)
g.FillEllipse(Brushes.Blue, BallLoc.X, BallLoc.Y, 20, 20)
' Accelerates speed
BallSpeedY = BallSpeedY - 1
' Update position
BallLoc = New Point(BallLoc.X + BallSpeedX, BallLoc.Y - BallSpeedY)
End If
If drawTime Then
gameTime = gameTime - 0.1
gameTime.ToString()
g.DrawString(gameTime, gameTimeFont, Brushes.Black, Me.Width / 2 - 120, Me.Height * 0.025) ' Draws the gametime on screen,
' Width is set to be around the middle of the screen while height is just under the strip menu.
End If
PictureBox1.Refresh()
End Sub
Private Sub StartToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartToolStripMenuItem.Click
If Start = False Then
gameTime = 60.1
'Resets ball for testing
BallLoc.X = 800
BallLoc.Y = 300
BallSpeedY = 0
'Starts the timers
Gravity.Start()
Movement.Start()
Start = True
ElseIf Start = True Then
' Resets the ball
Dim ballloc As New Point(800, 300)
Gravity.Stop()
Movement.Stop()
Start = False
End If
End Sub
Private Sub NewMapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewMapToolStripMenuItem.Click
' Creates a new map
DrawGame(True, False, False)
Start = False
End Sub
Private Sub Movement_Tick(sender As Object, e As EventArgs) Handles Movement.Tick
' Starts movement of the ball
DrawGame(False, True, True)
End Sub
End Class
EDIT: Collision now works with these additions:
Dim wider = CType(ground.Clone(), GraphicsPath)
Using widenizer As Pen = New Pen(Color.Black, ballDiameter)
wider.Widen(widenizer)
End Using
And:
If ground.IsVisible(BallLoc) OrElse wider.IsVisible(BallLoc) Then
BallSpeedY = BallSpeedY + 50 ' rebound on collision
End If
The code below illustrates the comment I've added about using Widen() so you can test using the center of the ball.
It has been ages since I've done any VB.NET, but it does show the idea:
Imports System.Drawing.Drawing2D
Partial Public Class Form1
Inherits Form
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim diameter As Integer = 10
Dim path As GraphicsPath = New GraphicsPath()
path.AddCurve(New Point() {New Point(0, 100), New Point(200, 200), New Point(400, 100)})
path.AddLines(New Point() {New Point(400, 400), New Point(0, 400)})
path.CloseAllFigures()
e.Graphics.FillPath(Brushes.Green, path)
Dim wider = CType(path.Clone(), GraphicsPath)
Using widenizer As Pen = New Pen(Color.Black, diameter)
wider.Widen(widenizer)
End Using
For x As Integer = 0 To 400 Step diameter
For y As Integer = 0 To 400 Step diameter
If path.IsVisible(x, y) OrElse wider.IsVisible(x, y) Then
e.Graphics.DrawEllipse(Pens.Red, CType(x - diameter / 2, Single), CType(y - diameter / 2, Single), diameter, diameter)
Else
e.Graphics.DrawEllipse(Pens.Blue, CType(x - diameter / 2, Single), CType(y - diameter / 2, Single), diameter, diameter)
End If
Next y
Next x
End Sub
End Class
It fills the area with circles, where the red ones are "touching" the ground:
How do I erase a graphic as it is moving along the screen? As my code is now it draws the shape on screen multiple times and doesn't ever erase them so it ends up looking like the attached picture.
picture
I'm trying to make a game where you shoot a ball from a cannon but I'm having trouble getting the ball to move smoothly and without the ball being on screen multiple places at the same time.
All of the code used:
Public Class Form1
Dim BMP As New Bitmap(6000, 6000)
Dim ball As Graphics = Graphics.FromImage(BMP)
Dim Map As Graphics = Graphics.FromImage(BMP)
Dim BallSpeedY, BallSpeedX As Integer
Dim BallLoc As Point
Dim Start As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
'Size of the picturebox that is being drawn on
PictureBox1.Width = Me.Width
PictureBox1.Height = Me.Height - 24
End Sub
Private Sub StartToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartToolStripMenuItem.Click
If Start = False Then
'Resets ball for testing
BallLoc.X = 800
BallLoc.Y = 300
BallSpeedY = 0
'Starts the timers
Gametime.Start()
Gravity.Start()
MovementY.Start()
MovementX.Start()
Start = True
ElseIf Start = True Then
Dim ballloc As New Point(800, 300)
Start = False
End If
End Sub
Private Sub NewMapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewMapToolStripMenuItem.Click
Map.Clear(Color.White)
Randomize()
' Draws map
Dim firstpt As New Point(0, Me.Height * 0.7)
Dim pt2 As New Point(Me.Width * 0.2, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
Dim pt3 As New Point(Me.Width * 0.4, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
Dim pt4 As New Point(Me.Width * 0.6, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
Dim pt5 As New Point(Me.Width * 0.8, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
Dim lastpt As New Point(Me.Width, Me.Height * 0.7)
Dim curvepoints As Point() = {firstpt, pt2, pt3, pt4, pt5, lastpt}
Map.DrawCurve(Pens.Red, curvepoints)
PictureBox1.Image = BMP
End Sub
Private Sub Gametime_Tick(sender As Object, e As EventArgs) Handles Gametime.Tick
End Sub
Private Sub MovementY_Tick(sender As Object, e As EventArgs) Handles MovementY.Tick
' Draws the ball
ball.DrawEllipse(Pens.Blue, BallLoc.X, BallLoc.Y, 20, 20)
ball.FillEllipse(Brushes.Blue, BallLoc.X, BallLoc.Y, 20, 20)
' Accelerated speed
BallSpeedY = BallSpeedY - 1
' Updates position
BallLoc = New Point(BallLoc.X + BallSpeedX, BallLoc.Y - BallSpeedY)
' Redraws image
PictureBox1.Image = BMP
End Sub
End Class
You're drawing on a bitmap and setting that to your PictureBox image. Try this...
In your tick event, draw everything directly on the PictureBox Graphics object and refresh the PictureBox. The points are saved as Static variables so as the ball is moving, the same map is generated behind it and whenever you choose new map, it will generate new points and redraw the map.
Basically the concept is to calculate different object movements and then draw everything in one place making sure to draw it in layers: background, next foreground object, etc.
Private Sub NewMapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewMapToolStripMenuItem.Click
DrawGame(True, True)
'Or if you just want a new map and not the ball use: DrawGame(True, False)
End Sub
Private Sub MovementY_Tick(sender As Object, e As EventArgs) Handles MovementY.Tick
DrawGame(False, True)
End Sub
Private Sub DrawGame(refreshMap As Boolean, drawBall As Boolean)
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
Static firstpt As Point
Static pt2 As Point
Static pt3 As Point
Static pt4 As Point
Static pt5 As Point
Static lastpt As Point
g.Clear(Color.White)
If refreshMap Then
Randomize()
' Generate new points
firstpt = New Point(0, Me.Height * 0.7)
pt2 = New Point(Me.Width * 0.2, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
pt3 = New Point(Me.Width * 0.4, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
pt4 = New Point(Me.Width * 0.6, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
pt5 = New Point(Me.Width * 0.8, Me.Height * (((70 - 30 + 1) * Rnd() + 30) / 100))
lastpt = New Point(Me.Width, Me.Height * 0.7)
End If
Dim curvepoints As Point() = {firstpt, pt2, pt3, pt4, pt5, lastpt}
g.DrawCurve(Pens.Red, curvepoints)
If drawBall Then
' Draws the ball
g.DrawEllipse(Pens.Blue, BallLoc.X, BallLoc.Y, 20, 20)
g.FillEllipse(Brushes.Blue, BallLoc.X, BallLoc.Y, 20, 20)
' Accelerated speed
BallSpeedY = BallSpeedY - 1
' Updates position
BallLoc = New Point(BallLoc.X + BallSpeedX, BallLoc.Y - BallSpeedY)
End If
PictureBox1.Refresh()
End Sub
And then you don't need this:
Dim BMP As New Bitmap(6000, 6000)
Dim ball As Graphics = Graphics.FromImage(BMP)
Dim Map As Graphics = Graphics.FromImage(BMP)
I am trying to place a custom user control over the top of a PictureBox control but I cannot seem for the life of me how to set the transparency of the user control so it doesn't chop out the PictureBox image.
My User Control consists of a RectangleShape with text in the middle to create a 'Badge' icon on top of an image (see pictures below). The PictureBox and User Control both sit inside a Panel control and I have set the PictureBox.SendToBack() property and UserControl.BringToFront() property.
What I am left with is this:
My Code looks like this:
Option Explicit On
Option Strict On
Imports Microsoft.VisualBasic.PowerPacks
Public Class BadgeIcon
Inherits UserControl
Private _value As Integer
Private canvas As New ShapeContainer
Private Badge_Icon As New RectangleShape
Private rect As New Rectangle
Private m_BorderColor As Color = Color.White
Private m_FillColor As Color = Color.Red
Private m_BorderThickness As Integer = 2
Private m_BadgeFont As New Font("Segoe UI", 7, FontStyle.Bold)
Private m_BadgeText As String
Private m_TextColor As New SolidBrush(Color.White)
Private m_TextSize As Size
Private m_TextPadding As Integer = 5
Public Property Value() As Integer
Get
Return _value
End Get
Set(value As Integer)
_value = value
m_BadgeText = CStr(_value)
m_TextSize = TextRenderer.MeasureText(m_BadgeText, m_BadgeFont)
rect.Width = m_TextSize.Width + m_TextPadding
rect.Height = m_TextSize.Height + m_TextPadding
Me.Refresh()
End Set
End Property
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = &H20
Return cp
End Get
End Property
Sub New()
' This call is required by the designer.
InitializeComponent()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
SetStyle(ControlStyles.Opaque, False)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.UserPaint, True)
Me.BackColor = Color.FromArgb(0, 0, 0, 0)
UpdateStyles()
' Add any initialization after the InitializeComponent() call.
canvas.Parent = Me
Badge_Icon.Parent = canvas
canvas.BackColor = Color.FromArgb(0, 0, 0, 0)
'Create Badge Icon
With Badge_Icon
.BackColor = Color.FromArgb(0, 0, 0, 0)
.BorderColor = m_BorderColor
.BorderWidth = m_BorderThickness
.BorderStyle = Drawing2D.DashStyle.Solid
.CornerRadius = 11
.FillColor = m_FillColor
.FillStyle = FillStyle.Solid
.SelectionColor = Color.Transparent
End With
AddHandler Badge_Icon.Paint, AddressOf BadgeIcon_Paint
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
DrawBadgeIcon(e)
End Sub
Public Sub DrawBadgeIcon(e As PaintEventArgs)
Try
'Alter the size of the icon to fix the text
With Badge_Icon
.Location = New Point(rect.Left + 1, rect.Top + 1)
.Size = New Size(rect.Width, rect.Height - 1)
End With
Catch ex As Exception
ErrorTrap(ex, "cls_NotificationBadgeIcon: DrawBadgeIcon()")
End Try
End Sub
Private Sub BadgeIcon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim textRect As New Rectangle(2, 2, m_TextSize.Width + m_TextPadding - 1, m_TextSize.Height + m_TextPadding - 2)
'Draw the Text
Dim flags As New StringFormat
flags.Alignment = StringAlignment.Center
flags.LineAlignment = StringAlignment.Center
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.DrawString(m_BadgeText, m_BadgeFont, m_TextColor, textRect, flags)
End Sub
End Class
Then to add everything to my main form I call the following:
Dim pic As New PictureBox
pic.Image = My.Resources.Notifications
pic.SizeMode = PictureBoxSizeMode.StretchImage
pic.Location = New Point(21, 221)
pic.Size = New Size(42, 29)
pnlLeftMenuBar.Controls.Add(pic)
pic.SendToBack()
Dim Counter_Notify As New BadgeIcon
Counter_Notify.Location = New Point(50, 240)
pnlLeftMenuBar.Controls.Add(Counter_Notify)
Counter_Notify.BringToFront()
And simply use Counter_Notify.Value = 1 to update the counter value.
How can I remove the square rectangle chopping out the background image? Or should I be setting this up an entirely different way? I'm a little new to User Controls.
Any help appreciated. Thanks
Using the paint event you can draw right on the picturebox itself.
Private Sub pb__Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles pb.Paint
Dim bgRect As New Rectangle({x,y,width,height})
Dim textRect As New Rectangle(bgRect.X - {?}, bgRect.Y = {?}, width, height)
e.Graphics.FillEllipse(New SolidBrush(Color.Red), bgRect)
e.Graphics.DrawEllipse(New Pen(Color.White, 10), bgRect)
Using sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
e.Graphics.DrawString("1", {your font}, {your brush}, textRect, sf)
End Using
End Sub