Results 1 to 16 of 16

Thread: [RESOLVED] [VB6] - catch the container scalemode value

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Resolved [RESOLVED] [VB6] - catch the container scalemode value

    has you know, the control can be puted in a container(form\picturebox). i have these code, but only give me the form and not the container
    Code:
    Public Function ContainerScaleMode() As Integer
        ContainerScaleMode = UserControl.ParentControls(0).ScaleMode
    End Function
    can anyone help me?
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6] - catch the container scalemode value

    You might be able to loop thru the controls and compare hWnds
    Code:
    On Error Resume Next
    For I = 0 to UserControl.ParentControls.Count-1
        If UserControl.ParentControls(I).hWnd = UserControl.ContainerHwnd Then
              ' found it
              Exit For
        End If
    Next
    On Error Goto 0
    Edited: I believe code like this has too much room for failure. What if the user changes your control's container during runtime? What if the user changes the container's ScaleMode during runtime? What if your control is in a frame, frames have no ScaleMode property. If your code must rely on the container's scalemode, force the user to supply either the container or the container's scalemode as a property. That way if it fails to work, it is because of incorrect property settings, not your control. And your control does not need to try to overcome the problem with flawed hacks.

    Last but not least. Except for passing event parameters in user scalemode (use ScaleX,ScaleY with vbContainerPosition & vbContainerSize), you can simply use pixels for form-related positions/sizes. Most controls have hWnds, APIs can get & set a control's position and size very easily.

    Try this experiment.
    1. Create a new usercontrol. Set its backcolor to black so it is easy to see
    2. Add this code in its click event:
    Code:
    Debug.Print "2010 = "; CLng(ScaleX(2010, vbPixels, vbContainerPosition)); " in container's scalemode"
    Debug.Print "2010 = "; CLng(ScaleX(2010, vbPixels, vbContainerSize)); " in container's scalemode"
    3. Add a picturebox to your form, then put usercontrol in the picturebox.
    4. Set picturebox scalemode to twips & run project and click on usercontrol
    5. Set picturebox scalemode to pixels & run project and click on usercontrol

    Use vbContainerPosition when you are converting coordinates. Use vbContainerSize for dimensions. I have not seen a situation where using the wrong one returns the wrong values. However, since VB provided two options, best to use the right one for the job.
    Last edited by LaVolpe; Jan 27th, 2010 at 01:04 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    You might be able to loop thru the controls and compare hWnds
    Code:
    On Error Resume Next
    For I = 0 to UserControl.ParentControls.Count-1
        If UserControl.ParentControls(I).hWnd = UserControl.ContainerHwnd Then
              ' found it
              Exit For
        End If
    Next
    On Error Goto 0
    Edited: I believe code like this has too much room for failure. What if the user changes your control's container during runtime? What if the user changes the container's ScaleMode during runtime? What if your control is in a frame, frames have no ScaleMode property. If your code must rely on the container's scalemode, force the user to supply either the container or the container's scalemode as a property. That way if it fails to work, it is because of incorrect property settings, not your control. And your control does not need to try to overcome the problem with flawed hacks.

    Last but not least. Except for passing event parameters in user scalemode (use ScaleX,ScaleY with vbContainerPosition & vbContainerSize), you can simply use pixels for form-related positions/sizes. Most controls have hWnds, APIs can get & set a control's position and size very easily.

    Try this experiment.
    1. Create a new usercontrol. Set its backcolor to black so it is easy to see
    2. Add this code in its click event:
    Code:
    Debug.Print "2010 = "; CLng(ScaleX(2010, vbPixels, vbContainerPosition)); " in container's scalemode"
    Debug.Print "2010 = "; CLng(ScaleX(2010, vbPixels, vbContainerSize)); " in container's scalemode"
    3. Add a picturebox to your form, then put usercontrol in the picturebox.
    4. Set picturebox scalemode to twips & run project and click on usercontrol
    5. Set picturebox scalemode to pixels & run project and click on usercontrol

    Use vbContainerPosition when you are converting coordinates. Use vbContainerSize for dimensions. I have not seen a situation where using the wrong one returns the wrong values. However, since VB provided two options, best to use the right one for the job.
    i found the solution more faster and easy:
    [CODE]Public Function ContainerScaleMode() As Integer
    ContainerScaleMode = Extender.Container.ScaleMode
    End Function[/CODE
    but i need ask 2 things:
    1 - after compiler the Extender object can give errors in other project?
    2 - why the method "Container" isn't showed(when you write "Extender.")?
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6] - catch the container scalemode value

    Quote Originally Posted by joaquim
    i found the solution more faster and easy:
    Code:
    Public Function ContainerScaleMode() As Integer
    ContainerScaleMode = Extender.Container.ScaleMode
    End Function
    but i need ask 2 things:
    1 - after compiler the Extender object can give errors in other project?
    2 - why the method "Container" isn't showed(when you write "Extender.")?
    thanks
    Hmmm
    Yes potential problems. You are assuming that all Extender objects expose a Container property and that all Container properties have ScaleModes; they may not. Again a VB Frame does not have a ScaleMode property. What if your control was inside some custom usercontrol?

    Recommend doing this correctly. Use ScaleX,ScaleY with the vbContainerPosition or vbContainerSize constants. If you do, you will not get those potential errors and you will not need to know the container's scalemode. Your choice of course.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    Hmmm
    Yes potential problems. You are assuming that all Extender objects expose a Container property and that all Container properties have ScaleModes; they may not. Again a VB Frame does not have a ScaleMode property. What if your control was inside some custom usercontrol?

    Recommend doing this correctly. Use ScaleX,ScaleY with the vbContainerPosition or vbContainerSize constants. If you do, you will not get those potential errors and you will not need to know the container's scalemode. Your choice of course.
    if the extender can give me errors, then how can i change these:
    Code:
    lngOldPosX = Extender.Left
            lngOldPosY = Extender.Top
    ?
    then i only need convert pixels to right scalemode in container.
    Code:
    L1 = Extender.Left + LimColLeft
                            T1 = Extender.Top + LimColTop
                            W1 = LimColWidth
                            H1 = LimColHeight
    i only need convert pixels to container scalemode(the limcol values).
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6] - catch the container scalemode value

    Does your usercontrol have an hWnd?
    If so....
    Code:
    Dim uRect As RECT, tPT as POINTAPI
    GetWindowRect UserControl.hWnd, uRect
    tPT.X = uRect.Left: tPT.Y = uRect.Top
    ScreenToClient UserControl.ContainerHwnd, tPT
    ' now tPT has the top/left coords of your usercontrol relative to the container
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    Does your usercontrol have an hWnd?
    If so....
    Code:
    Dim uRect As RECT, tPT as POINTAPI
    GetWindowRect UserControl.hWnd, uRect
    tPT.X = uRect.Left: tPT.Y = uRect.Top
    ScreenToClient UserControl.ContainerHwnd, tPT
    ' now tPT has the top/left coords of your usercontrol relative to the container
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  8. #8

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - catch the container scalemode value

    for convert the position:
    Code:
    Left=CLng(ScaleX(leftvalue, vbPixels, vbContainerPosition))
    top=CLng(ScaleY(topvalue, vbPixels, vbContainerPosition))
    and convert the size:
    Code:
    width= CLng(ScaleX(widthvalue, vbPixels, vbContainerSize))
    height=CLng(Scaley(heightvalue, vbPixels, vbContainerSize))
    tell me if the isn't right.
    thanks for help me
    VB6 2D Sprite control

    To live is difficult, but we do it.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6] - catch the container scalemode value

    I think you have it. But for the size of your usercontrol (for example), your widthValue & widthHeight are usercontrol.Width/Height and scalemode is always Twips.
    Code:
    width= CLng(ScaleX(UserControl.Width, vbTwips, vbContainerSize))
    height=CLng(ScaleY(UserControl.Height, vbTwips, vbContainerSize))
    Last edited by LaVolpe; Jan 27th, 2010 at 07:50 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    I think you have it. But for the size of your usercontrol (for example), your widthValue & widthHeight are usercontrol.Width/Height and scalemode is always Twips.
    Code:
    width= CLng(ScaleX(UserControl.Width, vbTwips, vbContainerSize))
    height=CLng(ScaleY(UserControl.Height, vbTwips, vbContainerSize))
    sorry, but in these case i use, in my UC, everything, is in pixels.
    thanks for everything.
    PS: sorry, but i can't rate you, because i don't have "points" for use it.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] [VB6] - catch the container scalemode value

    You didn't understand that example.
    1. UserControl's Width & Height is always in Twips as is a VB form's Width & Height
    :: the uc's ScaleWidth & ScaleHeight may be in pixels but not the Width & Height
    2. The 2nd parameter in ScaleX & ScaleY is the scalemode you are converting from
    3. The 3rd parameter in ScaleX & ScaleY is the scalemode you are converting to
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [RESOLVED] [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    You didn't understand that example.
    1. UserControl's Width & Height is always in Twips as is a VB form's Width & Height
    :: the uc's ScaleWidth & ScaleHeight may be in pixels but not the Width & Height
    2. The 2nd parameter in ScaleX & ScaleY is the scalemode you are converting from
    3. The 3rd parameter in ScaleX & ScaleY is the scalemode you are converting to
    sorry, but in these case is in pixels:
    Code:
    L1 = Extender.Left + LimColLeft
    T1 = Extender.Top + LimColTop
    W1 = LimColWidth
    H1 = LimColHeight
    the limcol variables are in pixels. thats why that i need convert them to scalemode values.
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  13. #13

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [RESOLVED] [VB6] - catch the container scalemode value

    for finish i forgot to ask 1 thing.
    if i can't use extender.name(for do a test), then how can i see the sprite2d name?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] [VB6] - catch the container scalemode value

    Try: Ambient.DisplayName
    Double check that. I'm not on a pc with VB6 and just going from memory.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [RESOLVED] [VB6] - catch the container scalemode value

    Quote Originally Posted by LaVolpe View Post
    Try: Ambient.DisplayName
    Double check that. I'm not on a pc with VB6 and just going from memory.
    yes.. works... thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  16. #16
    Lively Member
    Join Date
    Sep 2009
    Posts
    95

    Smile Re: [RESOLVED] [VB6] - catch the container scalemode value

    Quote Originally Posted by joaquim View Post
    yes.. works... thanks
    Hello LaVolpe, I have got a similar problem, but I couldn't solve it with your code advices. Please see the attached archive: its a subclassing control for lazy noobs . Open the test form of the test project and change the FitSizeTo property to see the whole magic . - It works perfectly if the parent form is in vbTwips mode, but not if it is switched to vbPixels.

    Do you have an idea to overcome the problem without adding a ParentScaleMode property?


    - Attachment removed, moved to another thread:
    http://www.vbforums.com/showthread.php?t=604704 -
    Last edited by NeedHelp!; Feb 25th, 2010 at 07:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width