Welcome to MSDN Blogs Sign in | Join | Help
very good 3d read

hah... supposed to be my last post but hey i have 1 more thing to post. This is a good book for 3d - plus it's very focus and not thick at all. Very well written.

I have to buy this full-price since I can't order book from the company anymore. Actually I ordered it from my admin 2 months ago and nothing came (テクスチャの位). Maybe they know I'm already leaving :-). That could save me $25 and now I'm $25 poorer.

Last blog post here...

This should be my last blog post here at MSDN blog. 9/14/2007 is my last day at Microsoft and if you know me or would like to contact me or would like to chat about UI/UX technologies - email me at kenny.lim@frogdesign.com. I'm still based in Seattle, WA.

Thank you.

Given a point, how to get the element at that coordinate?

UIElement.InputHitTest is a hidden function that could do just that,

public IInputElement InputHitTest (
Point point
)

 Passing in the coordinate, and this function will look at the element subtree and get the element (IInputElement because it's a common interface between ContentElement and UIElement) that is at that location.

GetPointAtFractionLength

This is such a cool API, this is one sample usage (ie. Drawing a clock). I could see things like manually tracing a PathGeometry which could contain complex Bezier segments.

double len = 0d;
Point pt, ptTan;
EllipseGeometry geo = new EllipseGeometry(new Point(50, 50), 50, 50);
PathGeometry pathGeo = geo.GetFlattenedPathGeometry();

for (int c = 0; c < 12; ++c)
{
    len += (double)(1 / 12d);
    pathGeo.GetPointAtFractionLength(len, out pt, out ptTan);
    Rectangle rect = new Rectangle();
    rect.Height = rect.Width = 2;
    rect.Fill = Brushes.Red;
    this.mainCanvas.Children.Add(rect);

    Canvas.SetTop(rect, pt.Y);
    Canvas.SetLeft(rect, pt.X);
}

 

PresentationSource

System.Windows.PresentationSource

PresentationSource is such a useful class after digging deeper into it. I’ve use it from time to time and looking at its members, I can see more use of it in the future.

The purpose of PresentationSource is to handle interop scenarios. To begin using this, anytime you want to call into User32.dll, you need a HWND.

Then you need this to get hwnd:
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle; 

You can do a ton of work from a hwnd itself like calling SetWindowLong, SetParent, SetWindowRegion and hundreds more etc..

Next cool function of PresentationSource is to get HwndSource so that you can call AddHook which allows you to subclass by providing your own WndProc.

HwndSource hwndSource = PresentationSource.FromVisual(window) as HwndSource;

            hwndSource.AddHook(…);

Then you can use PresentationSource to determined, if an element is rendered. If so it must be connected to PresentationSource.

PresentationSource.FromVisual(uielement); will return a PresentationSource object if connected, null otherwise.

PresentationSource.FromVisual(uielement).RootVisual is an alternate way to get to your Root UI like a Window without walking up the visual tree.

You can use PresentationSource.AddSourceChangedHandler to determined when the element are disconnected from it’s Parent.

Finally, you can also get CompositionTarget which is another cool class by presentationSource.CompositionTarget. By using CompositionTarget, you can do stuffs like this but don’t over-do it cause it’s expensive in performance. CompositionTarget.Rendering is an alternate way to get high fidelity (animation class) rendering. Here a summary of CompositionTarget.Rendering.

The CompositionTarget.Rendering event is fired when the animation system finishes applying animation related changes.  The updated display information is then handed off to the rendering engine, which evaluates how many times per-second it can redisplay based on scene complexity and other factors.  Depending on that, it may decide to skip a particular frame produced by the animation system.  Putting it another way, the draw rate can easily be different from the rate of the CompositionTarget.Rendering events. 

"People Near Me" in Vista.

Browsing around the my Windows Vista at home, I saw this "people near me" icon. Reading more about it, it's a new capability of Peer to peer networking built into Windows Vista. It allows you to connect to literally people near you in the airport, in a meeting room, starbucks etc.

More information about this feature can be found at:
http://www.microsoft.com/technet/network/p2p/pnm.mspx

In addition, it comes with a set of APIs - p2p.lib, p2p.dll and header file in p2p.h.

Get notification from WPF dependency system

This is valuable information on hooking into DP system of WPF.

The answer is DependencyPropertyDescriptor. This is similar to PropertyDescriptor from WinForm.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.dependencypropertydescriptor_members.aspx

For example, if you want to get notification whenever a property change but you don't own the code and you don't derive from the class and it does not expose SomethingChanged event. How can you get notification.

DependencyPropertyDescriptor dpDes = DependencyPropertyDescriptor.FromProperty(Control.BackgroundProperty);
dpDes.AddValueChanged(myElement, myEventHandler);

So whenever someone do a SetValue/ResetValue/ClearValue on myElement.Background, myEventHandler will be raised. When you are done listening to change notification, you can do RemoveValueChanged(..) to unsubscribe.

BenCon was talking about this as well :-)

.NET Book Zero

A free book from Petzold.

What the C or C++ Programmer Needs to Know About C# and the .NET Framework

Available in XPS and PDF. http://www.charlespetzold.com/dotnet/index.html

More WPF 3D playing..

The hardest thing about creating 3D is where to source my MeshGeometry3D data. That's the TriangleIndices, Normals and Positions. These numbers are enormous. Lucky thing there are tools (reading Tim's blog), I found ZAM 3D and other conversion utilities. This list is priceless. Other than this Mesh3D thingy, I found that 3D in XAML is very straight forward, just like normal 2D XAML.

Using the tool, I'm dragging in a Torus model and copy to XAML. Grab those Meshes and I play with brushes, textures, transforms, rotation, cameras etc.. and you got a nice looking 3D XAML :-) Finally, I got it to look like a rotating Jade ring. 

The XAML is posted at Channel9 and it looks like:

WPF 3d going mainstream?

I read Petzold's blog from time to time and I realized recently he has posted a lot of good 3d content.

http://www.charlespetzold.com/blog/2006/12/140114.html
http://www.charlespetzold.com/blog/2006/12/160115.html
http://www.charlespetzold.com/blog/2006/12/171020.html
http://www.charlespetzold.com/blog/2006/12/220812.html
http://www.charlespetzold.com/blog/2007/01/190146.html

and finally it's coming WPF 3d book by Petzold.
http://www.charlespetzold.com/blog/2006/12/270206.html 

5-6 years ago, there's a trend to have app written in irregular window, layered window, pixel opacity or different chrome. Then came a wave of products doing just that. I think what's coming next (in my own opinion) is the application have multiple layers and 1..n layer that display 3d visuals. For example, a TV app with tv controls in another layer in a Viewport3d floating in 3d or even animating a bit :-)

can't wait for his 3d book ...

Hmmm... WPF popup is always TOP_MOST

Recently I have this bug inherited from my good intern that WPF <Popup/> will always be TOP_MOST in terms of z-index. The bug scenario is that when you have another application (eg. notepad.exe) going on top of my WPF application (with Popup launched), Popup will be top most even though notepad appears on top of my app. So to fix this, I'm using some old Win32 tricks :-)

[DllImport("user32", EntryPoint = "SetWindowPos")]
private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags); 

public class MyPopup : Popup
{
  ...
  protected override void OnOpened(EventArgs e)
  {
    IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
    SetWindowPos(hwnd, -2, posX, posY, (int)this.Width, (int)this.Height, 0);
  }
}
 

 

My WPF Hobby project for 2006 "Portfolio Delta"... a little late.

It's been sometime since I last post here. This site even come with a new blog software. Very nice.

Every year during my OOF days, I worked on some hobby project that I will be using. Last year, I was doing a Stock reader for my own consumption. It comes with loads of features. Mostly for my personal use, to track my position at a given time, watch list for funds, stock to look out. Other features includes charting, per stock detail news, RSS viewer for financial/stock news, RSS player for financial/stock podcast, auto-complete for stock quote, streaming quotes, 4 viewing modes and a few more. It's still on-going because I am using this tool from time to time + I have 3 beta users to give me feedback :-)

This is done with the VS template for WPF. Total serious dev time is about 6 days. Developed for Microsoft Windows Vista plaform, it takes about 20MB. Can be deploy via xcopy or clickonce. Haven't experiment with browser inline hosting via IFRAME. Will test that next.

Still a few features to add in like sorting via column header, writing a chart chooser with nice effects etc..

 

1 line XAML challenge and Popup ....

I saw Tim's challenge and thought about some interesting single element XAML. One of my markup which I thought will cover the whole screen does not do that anymore, it just covers about 80% of the screen MAX. I remember going thru the threat modeling and having Markup injection like this a concern. The following markup definitely covers the whole screen 12 months ago - and my application is running full-trust.

<Popup IsOpen="true" StaysOpen="true" Placement="Absolute" HorizontalOffset="0" VerticalOffset="0" 
xmlns= "
http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="2000" Height="2000"/>

This is all good, just signs that WPF is wrapping up and ready to ship :-).

Tiling with VisualBrush - WPF Beta 2

It has been a while since I last blog about WPF (Avalon) related work. Well, here’s some updates on something I tried recently. I have some requirement lately to implement some GridLines for Diagrams. I first try to use OnRender and doing DrawLine inside of OnRender. I found that this is somehow slow when I have many shapes and lines around it.

So I want something similar to DrawLine and try setting Background property to a VisualBrush. XAML as follow:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >  <Grid>
 <Grid.Background>
    <VisualBrush TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute" Viewbox="0,0,20,20"      
ViewboxUnits="Absolute">
          <VisualBrush.Visual>
               
 <Rectangle Stroke="Blue" StrokeThickness="0.1" Width="20" Height="20" />
           </VisualBrush.Visual>
      </VisualBrush>               
</Grid.Background>
</Grid>
</Page>

And this will look like:

I found that this perform better than OnRender, this is solely based on my moving of many lines and shapes around the diagram.  Of course this is not cheap either, a Visual is basically equivalent to an UIElement. So you can essentially do this:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >  <Grid>
 <Grid.Background>
    <VisualBrush TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute" Viewbox="0,0,20,20"      
ViewboxUnits="Absolute">
          <VisualBrush.Visual>
                <Button/>
           </VisualBrush.Visual>
      </VisualBrush>               
</Grid.Background>
</Grid>
</Page>

The above XAML will tile what it seems a bunch of Button. But in reality only 1 Button object is created then casting to Visual and repeat the same bits across the Grid as background. Of course you can also compose deeper XAML tree for the VisualBrush but according the PERF guidance document at www.designerslove.net/docs/ Optimizing%20Performance%20in%20the%20Windows%20Presentation%20Foundation.doc

If you are in Software Rendering Mode, either 16 bits, Hardware Acceleration turned off or using older graphics card (support DX7 and lower) then tiling can be very expensive. If your card supports DX9, you should be fine and running Hardware Acceleration unless you are trying to tile 3d rotating objects as background then it really depends on how much VRAM you have J.

The other thing to note about Visuals in VisualBrush is that there don’t have hittesting, no events, no focus and purely serve as an image to the Background. Finally remember to set TileMode property to Tile to enable tiling.

Calendar Printing Assistant for Outlook 2007

If you have access to Microsoft Office 2007 Beta2, check out this new office program:
http://www.microsoft.com/office/preview/programs/outlook/cpa.mspx

This software has dependency on Windows Presentation Framework Beta 2 (codename: Avalon) and of course Office 2007 Beta2. This software also utilize the rendering/style power of WPF. 

This program allows you to use additional template for your Outlook appointments and data. This allows you to create view like this and more:

More Posts Next page »
Page view tracker