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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Draw picture on user control 3

Status
Not open for further replies.

graabein

Programmer
Oct 9, 2002
186
NO
Hi,

I have a user control with a PictureBox that I want to draw a picture on from the outside. The picture displays the user control status so it varies from each instance of the user control.

I do not want to create one ImageList per user control, I'd rather just have one ImageList on the parent and draw on each child's (instance of my user control) PictureBox.

How do I best do this? Is there any way to expose a user control's PictureBox?

Something like:

parentIconList.ListImages(iconIndex).Draw child(i).PictureBox.hDC, 10, 10, imlTransparent


[elephant2]
graabein
 
Maybe just using a Property Get that fetches a reference to the PictureBox?
 
Sounds like a great idea but I've never returned a reference. Can someone type it up for me? I'll also try to google it. Thanks for the tip!
 
I assume there will be a Property Set in your User Control where you set the 'State' of the user control. Use the same routine; (if you have Enum-ed the States) use Select Case to check which state the usercontrol presently is in, and set the control's picture property.

The idea is, you could do all this while you set the State property of the user control.

 
Yes but then I'd have to have the images in an ImageList inside each user control. I want to have one ImageList on the parent that has lots of user controls in it, and set each user control with a picture from the outside. Am I missing something here?
 
In that case, you will need a property (properties if you need the user to assign multiple state icons) for your usercontrol. If you have Get and Set defined for those properties, the user will be able to do,

StateUserCtl1.ActiveStateImage = Picture1.picture
StateUserCtl1.InactiveStateImage = Picture2.picture

Inside your usercontrol's code, you will still need to store these images in the propertybag so that it can be read and reflected when the State property changes to something else during runtime.

Hope that 'very fast' response helps you.

 
<I do not want to create one ImageList per user control

Why not? Also, why can't you just have two image controls in your user control?
 
Thanks vbSun, all I need now is the syntax of the Get and Set. I would like to expose the hDC property of the PictueBox inside the user control:

Public Property Let ActiveStateImage <insert code here>

So I can set the image from the parent like mentioned earlier:

parentIconList.ListImages(iconIndex).Draw child(i).PictureBox.hDC, 10, 10, imlTransparent


BobRodes that would mean loading an extra ImageList control for each child I load during runtime and I feel that's not good use of system resources but I could be wrong?

There's a maximum number of say 30 children at a single time in the application's lifetime. Maybe an ImageList with 10 icons per child instance is not that big of a deal?
 
Ok, I begin to see what you are up to. It looks like you're just creating a form with a bunch of custom usercontrols on it. You could put an imagelist on each control, or you could keep the images with your form and pass them to the usercontrol as needed. The former solution uses more memory, the latter more as-you-go processing. I lean a bit towards the latter as you are.

However, I wouldn't expose the picture box handle. It's simpler to put a method on your control, pass it a picture as an arguement, and paint the picture in the box. To illustrate simply what I'm talking about, start a new project. Add two forms. On form1, put a picture box, on form2 an image control and a command button. Find a picture for the image control and add it before you run the program. Then, add the following code to form1:
Code:
Public Sub CallItWhatYouLike(WhateverYouLike As IPictureDisp)
Picture1.PaintPicture WhateverYouLike, 0, 0
End Sub

Private Sub Form_Load()
Form2.Show
End Sub
and add the following code to form2:
Code:
Option Explicit

Private Sub Command1_Click()
Form1.CallItWhatYouLike Image1.Picture
End Sub
You'll find that when you click the command button, the image appears in form1's picturebox. Now, you should be able to create the same idea for your usercontrol--the control is analogous to form1 in my example, and your form that holds the control is analogous to form2.

By the way, consider using a resource file to hold your images. will give you some information. I can't test resource files at work, but you can probably get rid of your imagelist control altogether; you should be able to load the picture directly from the resource file as you call the method. If not, you still will find it easier to deploy your application if you use a resource file, since it will be a single file that contains all of your pictures. You can populate the imagelist control at startup from it and go from there.

HTH

Bob
 
At it's simplest all you need to add to your user control (assuming the picturebox on the control is called Picture1):
Code:
[blue]Public Property Get Picturebox() As Picturebox
    Set Picturebox = Picture1
End Property

Public Property Let Picturebox(ByVal New_Picturebox As Picturebox)
    Set Picture1 = New_Picturebox
End Property[/blue]

which means you should now be able to use the code you have been suggesting you'd like to use ...
 
With all respect to Bob's advice of setting the picture in a method..

If you are looking for properties that you can assign from the front end, you have got the Syntax for Let and Get from strongm.

It will be good to play around with the ActiveX Control interface wizard, (Available in add-ins) and study the code generated by it.

 
Thanks for all your help. I got it going now.

Next step is implementing drag and drop to move the child controls around inside an area on the parent.

[peace]
 
<At it's simplest

Yes, that's simpler than mine, and then I assume you can just call the paintpicture method of the PictureBox property. That given, isn't it even simpler to remove the property let and make it a read only property, since you won't ever be passing a picturebox control reference to the control for it to use?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top