none
Drawing in OnRender() BeginnerQuestion

    Question

  •  

    Hi,

    I'm new to WPF and just can't figure out some things (which did work on windows forms)

    I want to draw some FormattetText in the OnRender() -Mehod of my window, but I just see no Results?

    How does this come and how can I se my text???

     

    Code Block

    protected override void OnRender(DrawingContext drawingContext)

    {

    base.OnRender(drawingContext);

    string testString = "Hallo";

    FormattedText formattedText = new FormattedText(

    testString,

    CultureInfo.CurrentCulture,

    FlowDirection.LeftToRight,

    new Typeface("Verdana"),

    32,

    Brushes.Black);

    formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);

    // Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.

    formattedText.MaxTextWidth = 300;

    formattedText.MaxTextHeight = 240;

     

    // Draw the formatted text string to the DrawingContext of the control.

    drawingContext.DrawText(formattedText, new Point(50,50));

    }

     

     

    This is just a copy from a MSDN Article but it does not work on my project, why?

    Oh and I would like to be able to resize my text if the window resizes, is there some common and easy way to make this work?

    Tuesday, January 08, 2008 3:00 PM

Answers

  • I don't know why, but overriding OnRender does not work when the class is Window or derives from Window.  That's the only reason I can think of that this method wouldn't work.

     

    Most commonly, only classes the derive from FrameworkElement override OnRender.  Most applications use pre-existing classes that derive from FrameworkElement rather than making their own.

     

    Commonly, a class the overrides OnRender also overrides MeasureOverride to indicate the size the element wants to be.  The OnRender method can then use the RenderSize property to find out what actual size it's been given.  OnRender can then scale graphics to that size if it wants to.

     

     

    Tuesday, January 08, 2008 6:18 PM

All replies

  • I don't know why, but overriding OnRender does not work when the class is Window or derives from Window.  That's the only reason I can think of that this method wouldn't work.

     

    Most commonly, only classes the derive from FrameworkElement override OnRender.  Most applications use pre-existing classes that derive from FrameworkElement rather than making their own.

     

    Commonly, a class the overrides OnRender also overrides MeasureOverride to indicate the size the element wants to be.  The OnRender method can then use the RenderSize property to find out what actual size it's been given.  OnRender can then scale graphics to that size if it wants to.

     

     

    Tuesday, January 08, 2008 6:18 PM
  •  

    Thanks, you helped me very much. I would have spent hours to figure out why it does not work.
    Wednesday, January 09, 2008 3:53 PM
  • Changing the window background to transparent makes OnRender calls successful.
    Don't really know the reason behind this..

    Possibly a layer being drawn (like white) when no background is specified?

    Can someone throw some light on this behavior?

    -
    Sharath
    Wednesday, January 27, 2010 1:07 PM
  • The reason is that, internally, WPF does not create a drawing surface for a control unless it thinks the control will have a visible presence on the screen.  This is why setting the Background property solves the problem - it conveys the message to the framework that the control will have some visual impact, and should therefore have a drawing surface.  You will also find that no mouse events are received unless the Background property is set.  The control is literally "not there".
    Wednesday, May 09, 2012 3:05 PM
  • Override the VisualChildrenCount property and return 0 (or the number of child visuals if you have any).

    Wednesday, July 31, 2013 8:44 AM
  • This is identical to a quirk I saw in MFC: Drawing to a Frame Window doesn't work because the Frame Window uses an extra, invisible window (called the MDI Client) for display.

    There's no good reason for these inconsistencies; they just waste developers' time until they learn them. 

    Thursday, July 31, 2014 9:51 PM