views:

1014

answers:

2

I'm trying to create an HBox in flex that has only the top corners rounded. HBox (and all the other flex containers) have a cornerRadius style, but it applies to all corners. I'm not seeing a way to specify the style of individual corners.

Is there any built-in way to do this, or will I need to write my own custom drawing code to draw the background with rounded corners?

+3  A: 

It depends what border skin you're using. If you're using the built in skins (HaloBorder), there's a style that you can specify "roundedBottomCorners" (which is a boolean), which allows you to control whether the bottom corners are rounded or not. If you're using your own border skin, you can add in whatever styles you want to control the corners.

quoo
never even looked at skins. I'm just using whatever the default settings are. I'll try the roundedBottomCorners thing and see if that works.
Herms
Is there any way to do the opposite as well? rounded bottom corners but not top?
Herms
roundedBottomCorners doesn't apply to HBox.
Christian Nunciato
This answer actually worked?
Christian Nunciato
@Christian - it might not be documented, but it's working for me.
Herms
What version of the framework are you using? I'm just curious, because it doesn't compile for me (3.4 using FlexBuilder 3, though neither with Flex 3.2).
Christian Nunciato
FlexBuilder 3 with SDK 3.2. I'm setting the value using the css stylesheet, not by setting it in MXML. You could probably set it using setStyle() as well. I'm guessing you're setting it directly in the MXML, but the compiler doesn't know the component uses that style, so it chokes on it.
Herms
Sorry, work's keeping me super busy but - I'm using 3.4 - I found it by looking through the HaloBorder class - so while it may not be documented for HBox - it works. The HaloBorder is the default border skin for HBoxes (and most flex components), so it'd apply. There's nothing in there that allows you to control anything other than the cornerRadius though. For anything else, you'd have to write a custom skin, Christian's example is a good simple starting point.
quoo
+2  A: 

Alas, the only solution I know is to draw the background yourself, using Programmatic Skinning -- specifically, orderriding RectangularBorder::

package
{
    import mx.skins.RectangularBorder;

    public class HBoxSkin extends RectangularBorder
    {
     public function HBoxSkin()
     {
      super();
     }

     override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
     {
      super.updateDisplayList(unscaledWidth, unscaledHeight);

      var cr:Number = getStyle("cornerRadius");
      var bc:Number = getStyle("backgroundColor");

      graphics.clear();
      graphics.beginFill(bc, 1);

      // Draw the rectangle manually, rounding only the top two corners
      graphics.drawRoundRectComplex(0, 0, unscaledWidth, unscaledHeight, cr, cr, 0, 0);
      graphics.endFill();
     }
    }
}

... and then applying the change using the borderSkin property of your HBox:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <!-- Apply the skin using the borderSkin property of your HBox -->
    <mx:HBox borderSkin="HBoxSkin" backgroundColor="#FFFFFF" cornerRadius="10" height="100" width="100" top="10" left="10" />

</mx:Application>

The Flex Documentation gives an example of how to implement programmatic skinning. It's beyond beginner level, unfortunately (this stuff gets a lot easier in Flex 4), but if you follow the code in my example and in the documentation's, you should be able to get something together. Good luck!

Christian Nunciato
This is more complicated than necessary for the original question, but it's still very helpful to me. I'm going to need an HBox later that can have arbitrary corners rounded, and it looks like I can easily adapt this to make it work.
Herms