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
Printable View
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
(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.
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.
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
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
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
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.
Lavolpe,
Thanks....you gave me enough to play with.
Lavolpe,
The caption does not wrap around as the regular cmd button...any ideas.
Thanks
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.
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...
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
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
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.
Lavolpe,
Thanks a BUNCH.....I will proceed with my custom ctrl then I will play with your template to compare.