Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing Text on a picture box 2

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi All:

I'm trying to draw text on a picture box. The image is a button. I'd tell you the color, but I'm slightly color blind (mostly). It is a shade of blue, a bit darker than cyan.

I got this code straight from the help. The way it is coded right now works, but I had to divide the locations in half to see it. When I take out the dividend, I see the outline of the rectangle around the button (which I don't want), but you can't see the text.

TP is a Tab Page.

Code:
Private Sub TP3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TP3.Paint
        Dim Rect As New Rectangle[b][COLOR=blue](pbTests.Location.X / 2, pbTests.Location.Y / 2, pbTests.Width / 2, pbTests.Height / 2)[/color][/b]
        Dim myText As String = "Test"
        Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
        Dim fontFamily As New FontFamily("Arial")
        Dim font As New Font( _
           fontFamily, _
           8, _
           FontStyle.Regular, _
           GraphicsUnit.Point)

        e.Graphics.DrawString(myText, font, solidBrush, _
           RectangleF.op_Implicit(Rect))

        Dim pen As Pen = Pens.Black
        e.Graphics.DrawRectangle(pen, Rect)

    End Sub

I guess 3 things here:
A. I don't want to see the rectangle.
B. I need the text to be top in the Z-Order.
C. I want the text centered in the picture box.

If someone could steer me in the right direction, it would be greatly appreciated.


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Try something like this:

Code:
	Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

		Dim t As String = "This is some text"
		Dim f As Font = New Font("Arial", 12, FontStyle.Regular)
		Dim g As Graphics = e.Graphics
		Dim StringSize As SizeF = g.MeasureString(t, f)
		Dim StringTop As Single = CSng((PictureBox1.Height / 2) - (StringSize.Height / 2))
		Dim StringLeft As Single = CSng((PictureBox1.Width / 2) - (StringSize.Width / 2))
		g.DrawString(t, f, Brushes.Aquamarine, StringLeft, StringTop)

	End Sub

	Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize

		PictureBox1.Invalidate()

	End Sub


Hope this helps.

[vampire][bat]
 
I think a (disabled) flat Sytle button will serve the purpose very easily. Button size can be a the size you required.

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
E & A:

I'll try this when I get home. I kind of forgot that I posted this.

Zameer, you are correct, this does work, but I'm trying to accomplish it similar to what E & A posted. If all else fails, I'll do that. My real problem with this method is being color blind, because I cannot match the pic color on the button, and of course, when it is in run mode, then I can see the button.

Thanks to both of you. I'll post either tonight or over the weekend and let you know how it went.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
E & A:

I know it's been awhile, but I just had a chance to try this.

Works as advertised! I'd give you another star if I could.

Thanks so much,


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top