简体   繁体   English

VB.NET中带字幕的Picturebox

[英]Picturebox with Caption in VB.NET

How can I have a picturebox (control) with a caption? 如何为图片框(控件)添加字幕? I already can add Text over this picturebox, but I want it under the picturebox. 我已经可以在此图片框上添加文本,但是我希望它在图片框下。 But if the location of the text is beyond Picturebox's size, it won't be visible. 但是,如果文本的位置超出了Picturebox的大小,则它将不可见。 It would be great if the text had a border & background color too. 如果文本也具有边框和背景色,那就太好了。

Please help. 请帮忙。

Here's the code: 这是代码:

Public Class neoPic
   Inherits PictureBox
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       MyBase.OnPaint(e)
       e.Graphics.DrawString("Caption ", New Font("Cambria", 10), Brushes.Black, New PointF(0, 60))
   End Sub
End Class

I ended up creating a UserControl. 我最终创建了一个UserControl。 I put the working code here in case it saves another developer's time. 我将工作代码放在这里,以防它节省另一个开发人员的时间。 I added a UserControl to my project (named PicTitled) and added a Picturebox (named PTPicturebox) and a Label (named PTLabel) stacked on each other. 我在项目(名为PicTitled)中添加了一个UserControl,并添加了一个图片框(名为PTPicturebox)和一个标签(名为PTLabel)。 Then I added Text & Image property for the PicTitled and an event handler for mouse click. 然后,我为PicTitled添加了Text&Image属性,并为鼠标单击添加了事件处理程序。

Public Class PicTitled
    Public Shadows Event MouseClick As MouseEventHandler


    Overrides Property Text As String
        Get
            Return PTLabel.Text
        End Get
        Set(ByVal Value As String)
            PTLabel.Text = Value
        End Set
    End Property

    Property Image As Image
        Get
            Return PTPicturebox.Image
        End Get
        Set(ByVal Value As Image)
            PTPicturebox.Image = Value
        End Set
    End Property

    Private Sub PicTitled_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick, PTPicturebox.MouseClick, PTLabel.MouseClick
        RaiseEvent MouseClick(Me, e)
    End Sub


End Class

And in the main form, I added a code like: 在主表单中,我添加了如下代码:

    Private Sub CreateObj()
        Dim pbPicture As New PicTitled
        pbPicture.Name = "Object"
        pbPicture.Location = New System.Drawing.Point(40, 40)
        pbPicture.Text = "Object"
        pbPicture.Image = My.Resources.IMG
        pbPicture.Size = New System.Drawing.Size(50, 50)
        AddHandler pbPicture.MouseClick, AddressOf PictureBox_MouseClick

        Panel1.Controls.Add(pbPicture)
    End Sub

    Private Sub PictureBox_MouseClick(sender As Object, e As MouseEventArgs)
        'Do stuff when mouse click happens...
    End Sub     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM