By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Moving Transparent Control

P: n/a
Hi,
I wish to code a control that writes a text over a picturebox and that can
be dragged with mouse.

Here is what i've actually coded to obtain a similar effect, but as you can
see i've serious problems with trasparence

Create new project and add a button in the form and then insert a background
image to the form itself.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewTextLayer As New LayerText
With NewTextLayer
.Text = "Click Here!"
.ForeColor = Color.Blue
.Font = New Font("Arial Black", 20)
.Angle = 10

.Size = New System.Drawing.Size(200, 100)
.Location = New System.Drawing.Point(100, 30)
End With
Me.Controls.Add(NewTextLayer)
End Sub
This is a class:
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class LayerText : Inherits Control
Private _backBuffer As Bitmap
Private _angle As Single
Private dragPoint As Point

Public Property Angle() As Single
Get
Return _angle
End Get
Set(ByVal Value As Single)
If _angle <> Value Then
_angle = Value
Me.Invalidate()
End If
End Set
End Property
Public Sub New()
SetStyle(ControlStyles.DoubleBuffer Or _
ControlStyles.AllPaintingInWmPaint Or
ControlStyles.UserPaint Or _
ControlStyles.Opaque, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
If BackgroundImage Is Nothing Then
If Not (Parent.BackgroundImage Is Nothing) Then
e.Graphics.DrawImage(Parent.BackgroundImage, _
New Rectangle(0, 0, Width, Height), _
Me.Left, Me.Top, Width, Height, GraphicsUnit.Pixel)
Else
e.Graphics.FillRectangle(New SolidBrush(Parent.BackColor), _

New Rectangle(0, 0, Width, Height))
End If
End If

If _backBuffer Is Nothing Then
_backBuffer = New
Bitmap(Me.ClientSize.Width,Me.ClientSize.Height)
End If

Dim nGraphics As Graphics = Nothing
nGraphics = Graphics.FromImage(_backBuffer)

With nGraphics
'.Clear(Color.Transparent)

.RotateTransform(Angle)
.SmoothingMode = SmoothingMode.HighQuality

.DrawString(Text, Font, New SolidBrush(ForeColor), New
PointF(0,0))
.Dispose()
End With

e.Graphics.DrawImageUnscaled(_backBuffer, 0, 0)
End Sub

Private Sub TextLayer_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) andles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
dragPoint.X = e.X
dragPoint.Y = e.Y
End If
End Sub
Private Sub TextLayer_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = MouseButtons.Left Then
Location = New Point(Left + e.X - dragPoint.X, Top + e.Y -
dragPoint.Y)
Me.Invalidate()
End If
End Sub

End Class
Please any tips to enhance this code is welcomed
Thacks in andvance

Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.