Colored Buttons
July, 2008


An include file for Win32lib applications

Author: Judith Evans
Release Version: 0.1.17 build date 07-04-2008

Purpose:
To provide programmers using Win32lib the ability to create buttons with color, caption and graphics.

Credits:
ColoredButtons was developed using Euphoria, Win32lib and IDE developmental tools
Andy Drummond supplied many tips for drawing buttons as well as ideas to enhance colored buttons and a trick to prevent w32HClick of real M$ button being handled twice.
Rajesh Deshpande suggested placing Graphic on colored button, obtaining Hint text from the actual createEx button, changing caption color when pressed, new Caption Postion 3 which means center caption and graphic as a unit, suggested use of repaintFG when button is not completely repainted if another window partially covers button, suggested improvements and helped me in use of icon and bitmap from Bernie Ryan's win32eru package.
Bernie Ryan for reminding me about and_bits to get BS_MULTILINE and Flat style
Roland Stowasser informed me that getWindowInfo did not work on Win95 and suggested I use getControlStyles.
Stephen ? informed me about usage of ClearWindow.
Gregory Haberek suggested adding text rotation
Thomas Parslow for the irregular.ew lib for drawing Elliptic and RoundedRectangle buttons.
Chris Cuvier gave me assistence removing a stray pixel near non-rectangular buttons.

License:
You may use this file in any application for free but should acknowledge myself as original author. You may modify to include additional routines but I do ask that you send me a copy so that I might upgrade for other people's use.

To Do:
capability to rotate graphic as option when caption is rotated; may be restricted to 90 degree rotations
capability to rotate rectangular button as option when caption is rotated

Method:
Colored Buttons are emulated buttons built with Child Window and Pixmap controls and an array holding original button Id and a generated Id for the colored button with all information needed to color and manage the button.

Restrictions:
Colored buttons may be used in place of createEx buttons where permitted by Windows[tm] except placing a coloredbutton on another button (colored or not) or on ReBarBand.

Using classDefaults on Window control will produce unpredictable appearance of colored buttons.

Tips and Tricks:
If you have a message_box or another window in your program that covers colored buttons when the window is moved or dismissed the colored button may not be entirely repainted. When this happens code repaintFG(Window Id) after your message_box statement or after moving another window.

If you see button sitting inside itself, you might need to add the statement clearWindow(id) prior to any forceCBPaint(id) or remove any classDefault statements for Window control.

Code forceCBPaint(id) after coding setVisible, setEnable, setCheck

If you make your button elliptical and rotate the caption the button will appear to rotate as well unless you have a graphic.

Problems:
Drawing arc around corners of RoundedRectangle and the arc around Elliptical buttons is not smooth and there may be gaps.

Required include file
At the top of your program, or at least before any of the following routines, include coloredButtons.ew which includes Irregular.ew

Getting Started
To set up a colored button you would first define all the controls, including the buttons which would later be colored, via createEx statements in the normal way. After all your controls are defined, for each button you desire color, add the following statement which builds an entry in the ColoredButtonInformation array.

translateToColoredButton(integer Id, object graphic_filename, object disabled_graphic_filename)

  • Id is the button's Win32lib createEx name.
  • graphic_filename when a sequence is the file name of your graphic with complete or partial path. When not a sequence the handle of a graphic from Win32eru has been supplied. To use dib's from other graphics use addCBXpmmerGraphic. If there is not a graphic use {}.
  • disabled_graphic_filename when a sequence is the file name of your disabled graphic which will show when you have set Id disabled. When not a sequence the handle of a graphic from Win32eru has been supplied. To use dib's from other grahics use addCBXpmmerGraphic. If you do not use this feature enter {}.

    Note:
    Style and ExStyle params from your createEx'd button will be ignored except for BS_MULTILINE and #8000 (FlatButton). If you have other Style or ExStyle please contact me and I'll investigate if it can be added.
    If your button class is PictureButton or TogglePicture, colored buttons can not determine BS_MULTILINE or #8000 from Style and you must use multilineCBCaption and/or changeCBClass
    You may have colored buttons on more than one window.
    You may place each of the translateToColoredButton statements after the button control it is to replace instead of after all the createEx statements but be sure all button controls to be colored are translated prior to your program routines.
    If you have a Hint for your actual buttons, colored button will acquire the hint.
    If you have assigned a font for your actual buttons, colored button will acquire the font.
    If you change your actual button's Font via setFont or setCreateFont, the colored button will initially use that font.
    There is a way to use your own routines which can access the ColoredButtonInformation array and force a w32HPaint event if necessary.

    After all your controls are createEx'd and translateToColoredButton added, use the following statement to open and paint the colored buttons.

    initiateColoredButtons()

    In the routines that follow you may assign multiple Id by enclosing the Id in brackets separated by commas like so:{id,id,id}. If you enter {} for Id, all colored buttons will be affected. Most routines also have a corresponding routine for all ids in case you do not like using {} to denote all ids. In all cases where Id is referenced it is the Id, or Name, of the createEx button.

    addCBGraphic(integer id, integer which, object graphic)
    You can change the graphic and/or the disable graphic during execution of your program.

  • which is 1 for graphic and 2 for disable graphic
  • graphic is either a filename or handle from Win32eru

    addCBXpmmerGraphic(integer id, integer which, object graphic)
    You can change the graphic and/or the disable graphic during execution of your program.

  • which is 1 for graphic and 2 for disable graphic
  • graphic is the dib of the graphic

    assignParentBackColor(object Id, integer color)
    If you move, resize or setVisible w32False your colored button and there is left at the original position and size a box of the wrong color, coloredbuttons.ew did not locate the parent color and you must tell coloredbuttons.ew about the color.

  • color is the parent color.

    Note:
    If you have a colored button on a Group control which does not have color and the Group is on a Window which does have color be sure to add color to your Group control or use this routine. Eventually coloredbuttons.ew will provide for this situation.
    If you have several controls within a parent, each one will use this statement.
    If all your colored buttons are on the same parent and the parent has color, you may use assignAllParentBackColor(integer color)

    Following are routines to manage and manipulate colored buttons.

    changeBorderThickness(object Id, integer thickness)

  • border thickness is the width of the border, in pixels, of a button. Default is 1 which is the default value for Windows[tm]. There is a reflection inside the border automatically generated for the button and you should take that into account before deciding border thickness value. For example if you want the border around your caption to be 10 pixels thick you would enter 9 for the border thickness. The border around buttons is only colored by the normal highlight and shadow colors used by Microsoft[tm] buttons.

    changeAllBorderThickness(integer thicknessX)
    This routine is the same as above, except works for all colored buttons.

    changeCBColor(object Id, integer buttonState, integer color)

  • buttonState is 1 for button up and 2 for button down.
  • color is the numeric value of the color desired.
    Up color default is getSysColor(COLOR_BTNFACE).
    Down color default is also COLOR_BTNFACE except for TogglePicture and ToggleButton.
    TogglePicture and ToggleButton are preset with a default down color. You may change to another color if you wish. The default color may or may not be accurate with different Window[tm] Themes.

    changeAllCBColor(integer buttonState, integer color)
    This routine is the same as above, except works for all colored buttons.

    changeCBTextColor(object Id, integer buttonState, integer color)

  • color is the numeric value of the text color. The default is black.
  • buttonState is 1 for button up and 2 for button down. The default is black.

    changeAllCBTextColor(integer color)
    This routine is the same as above, except works for all colored buttons.

    centerVerticallyCBText(object Id, integer how)

  • how is true or false to vertically center caption. The default is true.

    centerAllVerticallyCBText(object Id, integer how)
    This routine is the same as above, except works for all colored buttons.

    moveCBCaption(object Id, integer placement)

  • placement may be 0 = center, 1 = left, 2 = right or 3 = center caption and graphic as a unit where the graphic may be on left or right as determined by moveCBGraphic.

    Note:
    Right placement allows room for caption to move when button is pressed.
    When placement is 3 and there is not room for the entire graphic to show when positioned at left, it is forced to show. Same rule applies to right side. This means if your caption is long it might have a portion overlayed by the graphic.

    moveAllCBCaption(integer placement)
    This routine is the same as above, except works for all colored buttons.

    changeCBCaption(object Id, sequence newCaption)

  • newCaption is your replacement text for the button caption. newCaption may contain \n to create new lines. If you do not use \n, a parsing routine may attempt to logically break newCaption into several lines at spaces to fit the button face. If newCaption has too many consecutive letters without a space, the result will not be pleasing. If you have used \n or caption does not fit on one line, you should specify multiline.

    changeAllCBCaption(sequence newCaption)
    This routine is the same as above, except works for all colored buttons.

    multilineCBCaption(object Id, integer how)

  • how is true or false to (1) break caption at \n or not and (2) parse long captions or not

    multilineAllCBCaption(integer how)
    This routine is the same as above, except works for all colored buttons.

    showCBCaption(object Id, integer showStatus

  • showStatus is true or false to show or hide the caption

    showAllCBCaption(integer whereIs)
    This routine is the same as above, except works for all colored buttons.

    moveCBGraphic(object Id, integer position)

  • position is center = 0, left = 1 and right = 2

    Note:
    Right placement allows room for graphic to move when button is pressed.

    moveAllCBGraphic(integer position)
    This routine is the same as above, except works for all colored buttons.

    setCBHint(object Id, sequence hint)

  • hint is text which shows when cursor hovers over the button.

    Note:
    Hint will be obtained from the actual CreateEx button. You may use this routine when you want to change the hint while your program is running.

    setAllCBHint(sequence hint)
    This routine is the same as above, except works for all colored buttons.

    setCBFont(object Id, sequence facename, integer pts, integer attribute)

  • facename is the Font Name
  • pts is the height of the font
  • attribute is either Normal, Bold, Italic, Underline or Strikeout or any combinations of these values, for example, Bold+Italic.

    Note:
    Font will be obtained from the actual CreateEx button. You may use this routine when you want to change the font while your program is running.

    setAllCBFont(sequence facename, integer pts, integer attribute)
    This routine is the same as above, except works for all colored buttons.

    setCBDefaultFont(object Id)

  • This routine sets Font back to Win32lib default font, as if you had never set font for the button

    setAllCBDefaultCBFont()
    This routine is the same as above, except works for all colored buttons.

    changeCBClass(object Id, sequence newclass)

  • newClass is the class in quotes you wish to change the button

    changeAllCBClass(sequence newclass)
    This routine is the same as above, except works for all colored buttons.

    setNonGrayDisable(object Id, integer state)
    state is w32True to disable and w32False to enable
    The disabled state does not have a gray-ed out caption.

    setCBFocus(object Id)
    The routine will issue a setFocus for Id.

    changeCBButtonType(object Id, sequence style, integer corner size)

  • style is "Ellipse" if you desire an elliptical button. "Ellipse" will ignore corner size so enter 0.
    When button's cx and cy are the same value then the button is rounded.
  • style is "RoundedRectangle" if you want a rectangular button with corner size rounded corners.
    corner size of 0 or less than 10 will default to 10. 30 is the recommended corner size.

    changeAllCBButtonType(sequence style, integer corner size)
    This routine is the same as above, except works for all colored buttons.

    setCBFontAngle(object Id, integer angle)

  • angle is the rotation amount when the caption is to be rotated.
  • This feature does not play well with multiple line captions.

    setAllCBFontAngle(integer angle)
    Same as above except works for all colored buttons.

    forceCBPaint(object Id)
    This routine is used only in the event you need to code something that affects the colored button such that a w32HPaint event needs to be invoked.
    Examples would be using setVisible, setEnable, setCheck

    ColoredButtons.ew can access w32HEvent and w32HLostFocus handlers for the createEx'd button as well as setRect, isVisible and isEnabled statements. use setCBFocus( button id) instead of setFocus. Internally w32HPaint and w32HMouse handlers are used.

    Using your own routines to manipulate colored buttons
    This is the way you would set a button visible/invisible.

    procedure setButtonVisible(integer id, integer state)
         setVisible(id, state)
         forceCBPaint(id)
    end procedure