How do you move a form with a custom background?

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
For the fun of it I made a background for a form that looks like this:
background.jpg


however, now I have the problem that I cant seem to get it to move... I would like it to be that you can click on the background of the form and just drag and drop it, but I cant seem to find any information on how to do it, I am hoping that is because it is really simple and I am just totally missing it.

Any ideas?
 
hmm... I looked in the form properties and did not see any "dragable" property.

I saw "AllowDrop" but I dont think that is what you were talking about.

When the program runs, all you see is the picture I posted (minus the bright green areas, those are transparent).
 
well, it turns out in my google searches I was searching for the wrong thing, I should have been searching for, how to move a form with no border, but I was just searching for how to move a form with a custom background :rolleyes:

So, here is the code I found: http://www.daniweb.com/techtalkforums/thread31022.html

and it seems to work, is there a better way to do this?

*edit* sorry for the double posts, I was just excited I had found a solution...
 
Yes, there is a better way to do this, and you need'nt resort to any API. You can just create the same message that is sent when you try to move a form with a border and pass it to the forms DefWndProc Method. Override the forms OnMouseDown Event


VB.NET:
MyBase.OnMouseDown(e)
YourForm.Capture = False
 
 Const WM_NCLBUTTONDOWN As Integer = &HA1S
        Const HTCAPTION As Integer = 2
        Dim msg As Message = _
            Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                New IntPtr(HTCAPTION), IntPtr.Zero)
        Me.DefWndProc(msg)

P.s I noticed that in your image that the corners are still visible, if you would like to not see these corners check out the forms protected region property. If you pass in a shape thats the only part of the form that will get painted. Don't use the TransparencyKey property it's a pain in the bum.
 
Last edited:
Believe it or not the two are doing pretty much the same thing. In the prior code from the site above he simply lies to the msg system when the form recieves a message indicating the user is clicking on the client region. In your example, after the form recieves the msg it raises the event then you capture the event and again lie to the msg system saying the same thing. Not really a difference in the two that I can make out. But I may have missed the point.


BTW Overriding WndProc is not a call to an API. So the prior version wasn't resorting to an API. You can override the WndProc Sub to recieve and manipulate nearly all messages sent to the form. When it's loading, painting, ect..... It's the heart of the events that are given to you to start with. WndProc will get every msg before the event actually arrives from the form in most situations so if you want to do some serious surgery on the form then you could start here before resorting to API's.
 
If you check out the two links that, that link leads to one of them uses win32 API, thats where i was coming from. In terms of which one is better... My version is because why override the wndProc if you dont have to. The .Net Framework Provides native methods for creating messages to insert into an applications message loop as my example demonstrates.
 
The two methods are doing the same thing but through different communication channels. The use of SendMessage here sends a message to the operating system, which as intended sends it back to the application to move. vis781 bypasses the OS messaging relay system and sends the same message with the applications own low level messaging system.

Fascinating idea that, to remote control myself without an remote :)
 
Just as a footnote. That routine works for most. if not all other controls as well. So you can move a button around the form too!!. I haven't tried it with all .net controls but it wouldn't take long to try them. I set up a little routine a while ago to test this. I just put in a boolean property called DragMe and tested the values in the overridden MouseDown Event handler and it worked. You can set the boolean value to true, drag it around the form, then set it to false and it operates like a normal button again. I don't know how useful it will be but it's a bit of fun....;)
 
Back
Top