Results 1 to 16 of 16

Thread: [RESOLVED] Creating custom Button Ctrl

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Resolved [RESOLVED] Creating custom Button Ctrl

    I am trying to create a custom cmd Button(wrapper) ctrl using my own Bmp. The caption property works fine but the Font does not work at all. Can someone look at the code and let me know what I'm doing wrong?

    See attach zip file.

    Thanks
    Attached Files Attached Files

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Creating custom Button Ctrl

    (didn't look at the code) You need to bind the control's Font properties to the Font properties of the label within your custom control which will represent the caption. You can also look in the CodeBank section of the forum as it has a lot of ready made user controls.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: Creating custom Button Ctrl

    Baya_Yu, I am not using a label. I am actually using the standard Cmb button as a Constituent ctrl(wrapper). The control's font is being bound, it does not get updated on the custom ctrl at all.

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

    Re: Creating custom Button Ctrl

    Looking at your code via Notepad, it appears you do not have them bound as you think.
    Yes you are setting the command button font to the font assigned via the Set Font property.
    But in the Paint event, you are painting with the usercontrol's font, not the button's font.
    Why not the button's font? Because of "TextOut UserControl.hdc ... "; whatever font is selected into the DC will be used for TextOut.

    Suggestion: Don't even use the command button's font at all. Replace it with the UserControl's Font
    Code:
    Public Property Get Font() As Font
        Set Font = UserControl.Font
    End Property
    Public Property Set Font(ByVal New_Font As Font)
        Set UserControl.Font = New_Font 'forces a repaint if AutoRedraw=False
        PropertyChanged "Font"
        UserControl_Paint ' should not be needed unless you have AutoRedraw=True
    End Property
    Last edited by LaVolpe; Apr 1st, 2010 at 03:35 PM. Reason: typo
    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
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: Creating custom Button Ctrl

    Lavolpe,

    Thanks a lot for your pointer.....I've been looking at this code for too long.

    I may call upon you for more questions regarding this custom ctrl creation.

    Thanks again

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

    Re: Creating custom Button Ctrl

    Glad you got it solved. Please mark thread as resolved, using "Thread Tools" dropdown menu near top of your first post

    Quote Originally Posted by PatVB View Post
    ... I may call upon you for more questions regarding this custom ctrl creation...
    ^^ As long as it isn't via PM. I, and most here, don't do personal requests.
    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
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    Lavolpe,

    I need to adjust the caption in the center of the button relative to the selected Font size.
    When I select a big(14,18,24) font size, the caption moves off center of the button...see sample code for any suggestion.

    Private Sub UserControl_Paint()
    UserControl_Resize

    'UserControl.FontSize

    'Set text at center of control
    TextOut UserControl.hdc, ((UserControl.ScaleWidth / 2) - (Len(Command1.Caption) + Screen.TwipsPerPixelX)), ((UserControl.ScaleHeight / 2) - Len(Command1.Caption)), Command1.Caption, Len(Command1.Caption)
    End Sub

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

    Re: [RESOLVED] Creating custom Button Ctrl

    The usercontrol has a TextWidth and TextHeight function/method. Use that to calculate the width/height of the caption, the calculate centering. Calculate centering from caption length isn't reliable. Length is length in number of characters. TextWidth is actual width as it would be drawn.
    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}

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    Lavolpe,

    Thanks....you gave me enough to play with.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    Lavolpe,

    The caption does not wrap around as the regular cmd button...any ideas.

    Thanks
    Last edited by PatVB; Apr 2nd, 2010 at 03:47 PM.

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

    Re: [RESOLVED] Creating custom Button Ctrl

    Yes. Use DrawText vs TextOut.
    DrawText has several flags that can be used, including centering & word-wrapping. That link has a couple examples at bottom of page.
    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
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    I have tried this code, but no luck for now

    DrawText UserControl.hdc, Command1.Caption, Len(Command1.Caption), R, DT_CENTER Or DT_VCENTER Or DT_WORDBREAK Or DT_SINGLELINE...

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

    Re: [RESOLVED] Creating custom Button Ctrl

    DT_SINGLELINE is not compatible with DT_WORDBREAK. One or the other.
    Also DT_VCenter is not compatible without DT_SINGLELINE. Suggest looking at the descriptions of the flags a bit closer.

    To center a multi-line caption in your DC, use the DT_CALCRECT flag with DT_WORDBREAK. The calculation flag sizes your RECT structure. Once sized, you can center it, then draw the text again without the DT_CALCRECT flag
    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}

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    Lavolpe,

    I am ok now with the multi-line, it took a while but its ok.

    Now I have to give the ctrl a few properties like: PictureDown, Style, Caption Hover(make caption change color when mouse over it).

    Since, the only thing I've changed for the custom ctrl is the skin of the cmd button and keeping all properties/events. I am not sure when to refer to the user control properties or the cmd buton to make the custom ctrl... see example below(hope you understand)

    Public Property Get Enabled() As Boolean
    Enabled = UserControl.Enabled 'Command1.Enabled
    End Property
    Public Property Let Enabled(ByVal New_Enabled As Boolean)
    'Command1.Enabled() = New_Enabled
    UserControl.Enabled() = New_Enabled
    PropertyChanged "Enabled"
    End Property

    'Implementing Text Property
    'when reading the Text property and when changing the Text property.
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    'Command1.Enabled = PropBag.ReadProperty("Enabled", True)
    UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
    End Sub

    'Implementing The Writing Method
    'Write the new property value to TextVariable,and update the Text Property
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    'Call PropBag.WriteProperty("Enabled", Command1.Enabled, True)
    Call PropBag.WriteProperty("Enabled", UserControl.Enabled, True)
    End Sub

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

    Re: [RESOLVED] Creating custom Button Ctrl

    I think it is now time for you to step back a second and re-think it a bit.

    Do you really need a command button to complicate things? Afterall, the usercontrol (uc) has most of the events/properties a command button does and you are even drawing the text yourself. Sure there will be some things you will have to add: borders, focus rectangle (if desired), and drawing image(s).

    After re-thinking and you decide maybe you don't need the command button afterall, copy your project and take the button out. In my signature below, is a link for a uc button template. I think you can use that or at least parts of it. Read all the postings in that link.

    If you decide to leave it in, refer to the button for properties you are not storing in your uc or are not available in your uc. Your user gets uc events by you raising them. Whether the event originates from the uc or the button, raise them to the user. If the same event can be received from both uc & button, only raise that event from one, not both.
    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}

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Connecticut
    Posts
    135

    Re: [RESOLVED] Creating custom Button Ctrl

    Lavolpe,

    Thanks a BUNCH.....I will proceed with my custom ctrl then I will play with your template to compare.

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