在多个控件之上执行 Paint
Perform Paint in top of multiple controls
我有一个小问题。
我怎样才能在 controls
之类的任何 textbox
等
之上绘制这种图像
这是我的代码:
Private Sub GroupBox6_Paint(sender As Object, e As PaintEventArgs) Handles GroupBox6.Paint
If txtStatus.Text = "Cancelled" Then
Try
Dim newImage As Image = Image.FromFile(FOLDER_PATH & "\completed.png")
Dim x As Single = ((GroupBox6.Width / 2) - (463 / 4))
Dim y As Single = 10
Dim width As Single = 463 / 2
Dim height As Single = 242 / 2
e.Graphics.DrawImage(newImage, x, y, width, height)
Catch ex As Exception
End Try
End If
End Sub
这是我的输出:
所以我的目标是在 groupbox
内的 textbox, label
顶部绘制图像 Completed
有什么想法吗?
您需要两个 bitmaps
和一个 picturebox
才能工作。第一张是 png
图片,第二张是 picturebox
图片:
Private pngImage, picBoxImage As Image
在表单加载事件中初始化两个图像:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
pngImage = Image.FromFile(FOLDER_PATH & "\completed.png") //load it once
picBoxImage = CType(pngImage.Clone, Image)
PictureBox1.Size = New Size(CInt(463 / 2), CInt(242 / 2))
PictureBox1.Parent = GroupBox6
PictureBox1.Image = picBoxImage
PictureBox1.Visible = False //you dont want it at the beggining
End Sub
sub显示图片框:
Private Sub ShowCompletedMessage()
Dim screenLocation As Point
Dim gr As Graphics
//you can pass these values as parameters in the sub if you want to make the code more generic
Dim x As Integer = CInt(((GroupBox6.Width / 2) - (463 / 4)))
Dim y As Integer = 10
Dim width As Integer = CInt(463 / 2)
Dim height As Integer = CInt(242 / 2)
//Ensure that picturebox is not visible. If it is you don't need to take a screenshot
If PictureBox1.Visible = True Then
Return
End If
gr = Graphics.FromImage(picBoxImage)
//you need to transform the coordinates to screen ones
screenLocation = GroupBox6.PointToScreen(New Point(x, y))
//draw the portion of the screen to your bitmap
gr.CopyFromScreen(screenLocation.X, screenLocation.Y, 0, 0, New Size(width, height), CopyPixelOperation.SourceCopy)
//draw the png image on top
gr.DrawImage(pngImage, 0, 0, width, height)
PictureBox1.Location = New Point(x, y)
PictureBox1.BringToFront()
PictureBox1.Visible = True
gr.Dispose()
gr = Nothing
Return
End Sub
每次你想显示消息调用上面的子。您决定何时何地。如果你不再需要它,你需要隐藏picturebox
PictureBox1.Visible = False
我有一个小问题。
我怎样才能在 controls
之类的任何 textbox
等
这是我的代码:
Private Sub GroupBox6_Paint(sender As Object, e As PaintEventArgs) Handles GroupBox6.Paint
If txtStatus.Text = "Cancelled" Then
Try
Dim newImage As Image = Image.FromFile(FOLDER_PATH & "\completed.png")
Dim x As Single = ((GroupBox6.Width / 2) - (463 / 4))
Dim y As Single = 10
Dim width As Single = 463 / 2
Dim height As Single = 242 / 2
e.Graphics.DrawImage(newImage, x, y, width, height)
Catch ex As Exception
End Try
End If
End Sub
这是我的输出:
所以我的目标是在 groupbox
内的 textbox, label
顶部绘制图像 Completed
有什么想法吗?
您需要两个 bitmaps
和一个 picturebox
才能工作。第一张是 png
图片,第二张是 picturebox
图片:
Private pngImage, picBoxImage As Image
在表单加载事件中初始化两个图像:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
pngImage = Image.FromFile(FOLDER_PATH & "\completed.png") //load it once
picBoxImage = CType(pngImage.Clone, Image)
PictureBox1.Size = New Size(CInt(463 / 2), CInt(242 / 2))
PictureBox1.Parent = GroupBox6
PictureBox1.Image = picBoxImage
PictureBox1.Visible = False //you dont want it at the beggining
End Sub
sub显示图片框:
Private Sub ShowCompletedMessage()
Dim screenLocation As Point
Dim gr As Graphics
//you can pass these values as parameters in the sub if you want to make the code more generic
Dim x As Integer = CInt(((GroupBox6.Width / 2) - (463 / 4)))
Dim y As Integer = 10
Dim width As Integer = CInt(463 / 2)
Dim height As Integer = CInt(242 / 2)
//Ensure that picturebox is not visible. If it is you don't need to take a screenshot
If PictureBox1.Visible = True Then
Return
End If
gr = Graphics.FromImage(picBoxImage)
//you need to transform the coordinates to screen ones
screenLocation = GroupBox6.PointToScreen(New Point(x, y))
//draw the portion of the screen to your bitmap
gr.CopyFromScreen(screenLocation.X, screenLocation.Y, 0, 0, New Size(width, height), CopyPixelOperation.SourceCopy)
//draw the png image on top
gr.DrawImage(pngImage, 0, 0, width, height)
PictureBox1.Location = New Point(x, y)
PictureBox1.BringToFront()
PictureBox1.Visible = True
gr.Dispose()
gr = Nothing
Return
End Sub
每次你想显示消息调用上面的子。您决定何时何地。如果你不再需要它,你需要隐藏picturebox
PictureBox1.Visible = False