Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a picture of a cow. I want to drag her around the window. I want to see her while I drag her. I do not want to drag a whole rectangle of background color with her.

Is there any way to do this anywhere in Visual Basic in Windows Forms application?
Posted
Updated 4-Jul-15 9:06am
v5
Comments
Sergey Alexandrovich Kryukov 3-Jul-15 0:22am    
What have you tried so far?
As you did not even tag your application tag and/or the UI framework/library you want to use, I doesn't look serious.
I only can say that in nearly all of them the problem is pretty trivial, but the solutions are different.
—SA
Walt Hamilton 4-Jul-15 13:24pm    
Sergey,

Sorry, I'm new here, and don't know the secret handshakes yet. I'm just trying to do a simple thing in Visual basic, using VS 2015 and .Net.

I've tried all kinds of suggested ways to make backgrounds transparent, but they are all static (that is, they set the background to the form background), but I need a dynamically transparent background. I've tried them on labels, picture boxes, buttons. I've tried codes that go in the constructor to make transparent backgrounds, I've tried codes to change the parent, to override the drawing and re-drawing, and it seems like of dozens of trivial and non-trivial suggestions.

All I want is to drag the cow and not have a whole rectangle of background following her. I agree with you, it seems like it should be trivial, but if this question has been asked on any forum I can find, I can't even come up with the right keywords to find it.

Any help would be appreciated.
Sergey Alexandrovich Kryukov 4-Jul-15 13:32pm    
It is not so helpful. You did not even answer my question. Is it System.Windows.Forms?
If you are a beginner, start with simpler projects. At least you need to learn what is there around.
—SA
Walt Hamilton 4-Jul-15 14:54pm    
It is a windows forms application, but this system won't let me tag it that way.
Afzaal Ahmad Zeeshan 4-Jul-15 15:07pm    
There, I have made changes to your post, added the tag and Windows Forms in the question text.

Anyways, since this is Windows Forms, why are you trying to use Visual Basic? Why not use VB.NET?

1 solution

This is a customized PictureBox with a "real" transparent Background.

VB
Public Class customPictureBox
    Inherits PictureBox


    Public Sub New()

        Me.SetStyle(ControlStyles.Opaque, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)

    End Sub

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20  ' Turn on WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnPaint(pe As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pe)

        If MyBase.BackgroundImage IsNot Nothing Then
            pe.Graphics.DrawImage(MyBase.BackgroundImage, 0, 0)
        End If

    End Sub

End Class


Unfortunately, because of overiding the OnPaint-method, all wanted painting work now must be done by yourself - this includes the painting of the image. In my example I draw the image in it's original size - but certainly it is possible to do also scaling (resizing) and so on - but now manually ... :(
 
Share this answer
 
v2
Comments
Walt Hamilton 8-Jul-15 23:08pm    
Thanks so much. It will be a day or two before I can get back to this project to test it, but it looks good.

For anybody that is following this thread, a pictureBox with a transparent background and a transparent picture is transparent to the background of the form when moved across it. The problem is that when it is moved across some other control, the transparent part of the picture reverts to the background color of the form, ignoring anything else it passes over.
Thanks again, I'll try it and let you know.
Ralf Meier 9-Jul-15 3:26am    
For Information:
I derived this "customPictureBox"-Control from PictureBox - but that isn't necessary. It could also be derived from Control itself because the needed painting-parts must completly be self-coded. In this case it is much better to create an own control from the lowest practicable platform as possible.

@Walt:
I am awaiting your comment. The control should work as you want if it is on the top of the "z-order". Have fun with it ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900