Jaime Rodriguez
On Windows Store apps, Windows Phone, HTML and XAML

  • Jaime Rodriguez

    Using VisualTreeHelper.GetDrawing to get around the Z-Order limitation when hosting Windows Forms controls

    • 2 Comments

    Several times I have had needed to put WPF content in front of  Windows Forms control hosted in a WPF app...

    Unfortunately, this is a limitation in the interop story, WindowsFormsHost elements are always drawn on top, and don't get affected by z-order ...

    The first time I did this, I was working with an imaging company, so it was easy as their activex rendered onto a bitmap already.. so we used that as a workaround: create a bitmap, and then 'replace' the control with the bitmap at specific times/triggers..

    The next time, I tried to use the same workaround .. but this time, I wrote some 'ugly' code to make a simple winforms control render into a bitmap ... [trust me it was ugly, took me a day+, used interop, ec.] ...   Now, I see this post from LLobo telling me there was an API for this ...  way to make me feel dumber than usual :( ...

    So, I am putting it here so no one else misses it like I did,  also as a reference as now I have to email Robert -sorry man - to tell him to replace our old code :) The API is VisualTreeHelper.GetDrawing..   Wrote the simplest  test, here is kinda what it looks like [will do for a quick airspace demo :) ]

     

     

    Explanation of the screen above:
    The Left hand side is a WindowsFormsHost  control hosting a MonthCalendar Windows Forms controls. . The RHS is a Rectangle witha DrawingBrush of the WindowsFormsHost in the LHS...   Across both UIElements, there is supposed to be a red rectangle overlayed [imagine this could be any other element or a flying animation, etc.] ...  Notice that on the Left, the Red rectangle is behind the WindowsFormsHost. . on the right, you get the right visual experience. .
    You could easily use this strategy to swap the WindowsFormsHost in and out ...  don't you think??

    The code is trivial,  I packaged it inside a Dispatcher callback - I don't think it is absolutely needed, but felt safer, trying to make sure windowsformshost has been drawn] ....

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
    Matrix m =
    PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
    double dx = m.M11;
    double dy = m.M22;

    System.Windows.Forms.MonthCalendar mc = new System.Windows.Forms.MonthCalendar();
    mc.Width = (int)(wfHost.Width * dx);
    mc.Height = (int)(wfHost.Height * dy);
    wfHost.Child = mc;

    this.Dispatcher.BeginInvoke (System.Windows.Threading.DispatcherPriority.Background ,
    new DispatcherOperationCallback(delegate
    {
    DrawingGroup dg = VisualTreeHelper.GetDrawing( wfHost );
    DrawingBrush db = new DrawingBrush ( dg );
    db.TileMode = TileMode.None;
    db.Stretch = Stretch.None;
    rect.Fill= db ;

    return null;
    } ), null );

    }

     

    Full sample is here.

    I tested with several custom windows forms controls seems to work ... That said, I could not get Lester's demo to work with the IE frame ...
    I believe this same code will work for Win32 interop (HwndHost) but did not test it, if you try it and it does not work ping me..

  • Jaime Rodriguez

    Desktop matters conference report ..

    • 1 Comments

    Tim Sneath ( my work boss)  emailed me asking how it went , and since I am typing if for my team I thought: "what the heck ... just publish it..."

    I had the pleasure of attending and presenting at Desktop Matters last Friday .. It is the "hard-corest" Java conference I have ever attended (disclaimer, I have only attended 2 java conferences: JavaOne and OSCON) ...  The conference focuses on client java technologies,  a lot of people from Sun's Java2D and Swing team presented..  Also, some influentials building interesting java stuff..

    Here are my thoughts:

    On the conference:
    it was awesome... A small group of Java influentials talking with the Java leaders (Hans Muller, Chet HaaseRichard Bair, etc.) .. there was nothing held back either direction.. The Sun team shared challenges and road-map... the community provided good feedback.. I had a blast and learned a lot ...   Read some of their reports here..
    The event reminded me of what at Microsoft we call Software Design Reviews ( SDRs ) ... Some of you reading this post might have attended one of these ...  You know that it is hard to buy that kind of access, well on the Java client community you might have the answer in this conference..  
    A nice touch different from the SDR was Ben Galbraith the organizer who kept it light .. making equal fun of every one there 

    On Java SE and Swing: 
    I was surprised and (as a technologist) happy to see progress on java and Swing ; their 'filthy rich' demos looked pretty good ; That said, I still think they are very far behind Microsoft (both Windows forms but specially WPF) in tooling, customization, richness, etc..  You can definitely do good stuff on Java, if you are willing to write a bit of code or hire experienced devs..  [yes, I of course acknowledge they do it for the cross-platform promise.. if you must go x-platform and can't go AJAX, I think that is one of the best choices out there]

    On my presentation/message:
    When I arrived, I had a deck with 40 slides covering the ins of WPF ( templates, styles, resources, etc.)  by the second day I had changed it  based on a private conversation with Hans Muller - I pitched him on XAML and he seemed overwhelmed with properties, triggers, styles, etc.. but he really got the "oh, makes better tools" idea..

    So, in the end, I presented a simple (much slower-paced ):  

    1. Why is MS doing WPF..?    It is ALL about User Experience; I elaborated that UX is business value to the everyone.
      On day 1, the Java folks listed why write client software and the list was usability, richness, performance, leverage local resources, etc..   most of these are factors of user experience.. so it resonated..
      UX is also business value for the Dev/ISV/SI selling the solution... It provides differentiation opportunity, branding, pride of ownership, etc. 
       
    2. How is MS we improving UX with WPF?  
      1. Integration of all our UI Stacks ( Text, 2D, 3D, Video, etc.)
      2. Declaratively;  we invested in a declarative model to optimize toolability and fully incorporate the designer role into application development .. this is the silver bullet in the long-run; along with rich services to embrace the declarative modell...
    3. What are we doing on x-platform?
      (IMHO)  WPF/E is a huge step for Microsoft. Not because of what we are delivering in Version1, but because it is a commitment to innovate in client software that runs x-platform.    MS is best positioned [we have the framework, WPF, XAML, etc. to act quickly in that space, since we have a lot of the lessons learned - and the code ;)

    I did two demos:

    1. The Healthcare demo I was short on time and wanted to keep it focused -- check for a post on that demo later today or tomorrow there is some good news there--..  I think this summarizes well WPF integration since it shows most UI stacks..  
    2. I spend 15 to 20 mins in Expression blend building stuff from scratch ;
      1. created the data templates for the healthcare demo ...  and
      2. created a 3D button with animation ( triggers on mouse over)  ...  the latter was mostly to make explain how we compare to Chet's animation engine for his book...

    The feedback
    I was very surprised on the 'kudos' ... Quite a few people came to me afterwards and shared kind thoughts...   I got a lot of "I get your experience message";  and "I love what WPF can do", "XAML seems very neat", etc..  

    My best two take aways...   

    1. An interesting cross-platform combo in the interim? :
      While sitting there listening to their challenges, goals, etc. and after receiving the feedback on my presentation I was thinking...  what if "XAML was a standard" and the Java community embraced it to create UI? ....  They could keep their performant cross platform engine .. but use our tooling to create rich apps" .... does it sound crazy??   I think it is a good interim while MS gets a strong x-platform client out (if we ever do, I can't confirm nor deny, only hope )...   The type of apps these folks are writing requires rich development frameworks, rich programming languages, and powerful drawing engines...   I can't think of a good solution for that today if you include a good tooling/debug story.
    2. Some lower hanging, more realistic takeaway for Microsoft:
      I think we need to re-visit simple styles and create several "cool" versions of simple styles...   We have the designer tools.. but there will be teams out there that do not have designers.. We need to pony up for a few good design templates that developers can use to create "cool looking apps" with out a lot of designer support...  If MS provides  styled primitives then developers could put screens build on these simple styled controls...   For example,  I recently downloaded the "Roxio" client, nice gradients, stylish controls, yet nothing extreme (other than a few cool vector graphics that could have been images) ..   I think with "cool simple styles" this could be accomplish with out much design work...  

     

    That is it ...  I hope the conference repeats next year ....  If it does,  I would recommend it if you are a Java client developer ...
    I also hope I don't get fired for a post w/ the word java so much ... My first test to see how far I can take it ....

  • Jaime Rodriguez

    Performance on the WPF AllScripts Healthcare demo ...

    • 1 Comments

     

    Over the last month, several people have asked me why CPU is so high on the healthcare demo ...     
    I usually  

    1. Ask if they are getting hardware acceleration? point them to Perforator or WPFPerf to check,  and/or explain them the driver checks in case they have old drivers ....   I also
    2. Explain to them that it is a demo; opting for slightly higher CPU if it can shave milliseconds of screen time is often not a bad decision for a demo.  Overall, it was not originally architected as a sample ....and retrofitting that to be a good perf sample can likely compromise readability for those trying to use it to learn...


    Last week, Christoph emailed me asking about the perf the same day that I was installing the demo to show it , so I "peeked" ...   
    In summary:
    I cut the CPU consumption on my laptop ( 2.1 GHz  ) significantly due to a possible tiny bug..  Taskmgr tells me  CPU went down between 20 and 40% .. specially for the first screen... 
    In my laptop, the first screen is running at around 3% CPU, and the second screen is running at around 25 % ...  [Both of these used to be much higher]

    Here are the findings

    1. There was this in the code
      Chart.cs, Line 35 ...      private const double timerFrameRate = 20.0; // 1000 / 200 = 5 fps ....   Well last I checked,  1000 frames/20ms  == 50 FPS ...
      I am not sure if this is a bug or some one purposedly changed it later to 20 from 200.. ... but considering that the chart is updated once a second, 50 fps is way too much ...  

      The line above is a big deal, cause you are running at least 5 charts ALL the time -- regardless of whether the charts are showing on the screen or not.. it is a demo after all.. and also because they actually change the screen (see #2 below)..
       
    2. It is doing some funny stuff...  
      1. Redraws each chart in "full"every second,   Controlled by UpdateInterval Dep Property, which defaults to 1000 ms
      2. but then they refresh every 50 fps ( see #1) and it actually moves the canvas by 5 pixels every frame.. so technically data changes once a second but demo is still creating a dirty region and redrawing 50 fps...    
        Check in my code for # ONESECONDREFRESH  ... This is how the app would behave if you were just refreshing the data once a second and letting it be.. this is how this app would likely behave in real life..
    3. The demo uses Shapes & Paths to draw... Not sure why ..  I tested it with StreamGeometries and works fine too ..  Look at my code for #preprocessor NOTOPTIMIZED ...  and check the differences.. sadly, that failed to yield a significant change on my side, but it might if doing software rendering..   I am guessing given this is once per second, it is not a huge deal ..
    4. The demo itself does a little more drawing to make things pretty than a lot of people would do in a real scenario (e.g. they have code to draw shadows, real code might not do this)
    5.  The first and second screen pretty much stay in memory and running... this is one of the reasons the second screen is 25% CPU ...  It does not stop updating the stuff on the first screen...    This one I think is because it is a demo/prototype...
    6. I also had time to fix this FAQ ...  People have asked me about why the 3D flips some times don't work ...
      The demo checks if I am running in PowerSaver mode .. and when I do, it does not do the cool 3D flips.  ...   Check AppWindow.xaml.cs, OnPatientRosterSelectionChanged ... 

     

    OK, those are a few of my findings  while on a 2 hour flight to San Jose... I am zipping the code and putting it here.


    When I have time I will check with Karsten to see if that is worth re-publishing ..  I think if we are going to re-publish we should add a couple of extra changes like:

    • using the 3DTools library from CodePlex to do the 3D rotation of the listbox...  The latter change looks like more than a few hours worth of work because it is at the core of the layout for the whole app ... that is why I did not do it ...

    Hope that helps some ... If I ever have time, I will look deeper into the second screen...  I think that is still has a lot of room ...   if any one peeks and finds stuff let us know..  

  • Jaime Rodriguez

    Getting system DPI in WPF app..

    • 0 Comments

    it only took less than an hour for some one to ask why my last post has this code:

    Matrix m =
    PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
    double dx = m.M11;
    double dy = m.M22;

    The multiple choice answer is:
    a) habit, not paying attention
    b) filling space, not paying attention
    c) not paying attention

    The  code gets the horizontal (M11), vertical (M22) DPI on my system..   The actual value are divided by 96 ...  [going back to WPF's device independent logical units in wpf being 1/96 of an inch]... so for example, my system at 144 DPI so I get 1.5  ... 

    Technically, WindowsFormsHost does NOT need that code; it is supposed to do that translation for you, if you give it a windows forms control that is not sized.

    Sorry ..

Page 1 of 1 (4 items)