Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
Right-align text drawn by DrawFilter on UltraCalculatorDropDown
posted

I am having some trouble getting right-aligned text to line up with the right edge when drawing it myself using a DrawFilter on an UltraCalculatorDropDown, especially when using TextRenderingMode = TextRenderingMode.GDI.  I can’t just set the UltraCalculatorDropDown .Appearance.TextHAlign = HAlign.Right because I will be adding more custom drawing, but first I need the text to draw against the right edge. 

I am really very close, but I would like to see if anyone can assist me with keeping the text from jumping when entering and exiting edit mode by helping me get it to draw closer to the edge.

Attached is a small VS 2010 project that demonstrates the alignment issue I am having.

GDI Rendering

Question: How do you draw right-aligned text in GDI rendering close to the right side of the element (like where the text is when it is in edit mode)? See ExhibitA.png for an illustration of how the text is not against the edge.

When TextRenderingMode = TextRenderingMode.GDI the text does not jump on an un-customized, right-aligned UltraCalculatorDropDown and that is GREAT.  However, when using GDI rendering I cannot figure out how to draw right-aligned text using a DrawFilter and have it come as close to the right edge of the control as it is when the control is in edit mode, and this causes the text to jump a bit when entering edit mode.

In the example project, uncheck Use GDIPlus and set Negative number pattern to 1.

Here is the simplest DrawFilter I tried:

class JustRightAlignFilterForTesting : IUIElementDrawFilter

{

        public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)

        {

                if (drawParams.Element is EditorWithTextDisplayTextUIElement)

                        return DrawPhase.BeforeDrawForeground;

                return DrawPhase.None;

        }

 

        public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)

        {

                var element = drawParams.Element as EditorWithTextDisplayTextUIElement;

                if (element == null)

                        return false;

 

                drawParams.AppearanceData.TextHAlign = HAlign.Right;

                drawParams.DrawString(element.Rect, element.Text, false, false);

                return true;                   

        }

}

 

 

Background

I am implementing custom behavior so that the control draws similar to how Excel draws numbers in "Accounting" style where there is a dollar sign on the left edge of the cell and the number is right-aligned on the decimal point.  Negative numbers are wrapped in parenthesis and aligned so that the positive numbers leave room for the parenthesis that a negative number has, so that the decimal points line up.

When focus is in the control (in edit mode), this custom formatting should not be used and the UltraCalculatorDropDown should behave as it usually does, except that we would like the text to not jump at all when entering and leaving edit mode which it does when TextRenderingMode = TextRenderingMode.GDIPlus even when not customized.

With all three checkboxes checked and negative number pattern 1, only the four controls with the white background are bahaving like we want them to, all the other ones move the text when entering edit mode.

AlignmentDemo.zip
Parents
  • 53790
    posted

    Hello Ross,

    Thanks for attached sample. I research and debug your sample and I think that mentioned behavior - "if anyone can assist me with keeping the text from jumping when entering and exiting edit mode." is cause from your custom CreationFilter and variable magicNumber = 2. If you are using magicNumber = 4 instead of 2, then the flickering will disappear.

    Please take a look at the attached video file and let me know if you have any questions, or if you think that I misunderstood your scenario.

    Regards

Reply Children
No Data