Skinning with two picturebox's

CdRsKuLL

Active member
Joined
Sep 26, 2006
Messages
40
Programming Experience
Beginner
Im converting over to vb.net and i'm trying to skin my application. on the VB6 one I just used bitblt which worked great.. I'm trying the same on this new app but I'm not getting anywhere..

First I have 2 picturebox's names 1 and 2..
PictureBox1 is the destination and visible
PictureBox2 is the source and not visible

I have labels on the PictureBox1, which when clicked I want to show the same position on the PictureBox2 on PictureBox1 (button down image)

I have created an event handler so I'm capturing the mouse events I just need to work out how to show the image and then reset the image back to normal (mouse up event)

any help appriciated

thanks

Steve
 
You need to Override the OnMouseDown and OnMouseUp events and add the code there to handle the changing of the images. Though if i may say so, why are you not just creating a new button from scratch rather than using two picture boxes?
 
I have managed to capture the mousedown / mouse up events as these are set my dynamic label arrays.. (position buttons on load from a ini file) The reason I'm using 2 pictures is one has buttons up.. and the other has buttons down. this only means you have 2 pictures rather then 50 - 60 which is what I would need as I'm also having a keyboard on screen as well. I've tried this in the sub...

VB.NET:
    Private Sub MyControlArray_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim hDCsource As Int32
        Dim hDCdest As Int32
        hDCsource = GetWindowDC(PictureBox2.Handle)
        hDCdest = GetWindowDC(PictureBox1.Handle)
        BitBlt(hDCdest, DirectCast(sender, Control).Left, DirectCast(sender, Control).Top, DirectCast(sender, Control).Width, _
DirectCast(sender, Control).Height, hDCsource, DirectCast(sender, Control).Left, DirectCast(sender, Control).Top, 13369376)
     End Sub

which doesnt do anything..

CdR
 
I'd forget about BitBlt it is fast in an unmanaged world but in .net DrawImage of the graphics class is the way to go. Win32 API's are fast but the framework has many optimizations that can make .net code run faster that Win32 API Calls. Aside from the speed advantage it's much much easier to use. So instead use..

VB.NET:
Dim G as graphics = Graphics.FromImage(PictureBox whatever.image)
 
G.drawimage(..)
 
G.Dispose
 
Hi thanks for the quick reply. You wouldnt happen to have a example would you. I have tried several different ways all morning, all failing sadly.

thanks

Steve
 
Hi, just having a quick look, it appears the .FromImage would collect the whole of the picturebox2.image which is not what I'm after. I only need the image of where the buttondown is for that key.. ie. 300,300,60,60 from picture2, then place this onto picture1 at the same place.

CdR
 
The drawimage methods is really well overloaded, check them out. There is one that can take a source rectangle i.e the portion of the image to want to draw and a destination rectangle i.e the place you want to draw it.
 
hmm this is doing my head in.. added the following code and been looking at it and looking at other example.. and I cant understand why its not working.. can anyone help me here please..

VB.NET:
    Private Sub MyControlArray_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim G As Graphics = Graphics.FromImage(PictureBox1.Image)
        Dim dst_rect As New Rectangle(DirectCast(sender, Control).Left, DirectCast(sender, Control).Top, DirectCast(sender, Control).Width, DirectCast(sender, Control).Height)
        Dim src_rect As New Rectangle(DirectCast(sender, Control).Left, DirectCast(sender, Control).Top, DirectCast(sender, Control).Width, DirectCast(sender, Control).Height)
        G.DrawImage(PictureBox2.Image, dst_rect, src_rect, GraphicsUnit.Pixel)

        G.Dispose()

    End Sub

Steve
 
I've used debug.print to check that the values are correct ie.. they hole the correct placements for the label clicked.. eg.. 50,50,50,50 which is right.. so no idea where I'm going wrong here.

Steve
 
So what exactly is happening? This could be problem with the graphics.fromimage method. You are creating a graphics object from picturebox1.image and then attempting to draw onto a different graphics object i.e picturebox2. Essentially picturebox2 is a separate Hdc. I'd need to have a go at this to figure out why it isn't working. I'd suggest drawing your image to a bitmap then placing it into picturebox2
 
it seems from playing more that its not getting the correct image thats in picturebox2 , this maybe because its not visible. How would I load this image into memory so I can the either .Drawimage or bitblt from its location.

thanks

Steve
 
Back
Top