Nothing is everything.

Friday, January 8, 2010

Get Shortcut Path

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/99249eff-5e8f-4ce8-8fd6-625401c987ea/

private string GetShortcutPath(string shortCutPath)
{
Shell32.Shell shl = new Shell32.ShellClass();
Shell32.Folder dir = shl.NameSpace(Path.GetDirectoryName(shortCutPath));
string linkName = Path.GetFileName(shortCutPath);
Shell32.ShellLinkObject linkObj = (Shell32.ShellLinkObject)dir.Items().Item(linkName).GetLink;
return linkObj.Path;
}

We need to add reference of path ‘dll c:\windows\system32\shell32.dll’ to run the code above.

Tuesday, January 5, 2010

WPF Data Binding - xaml

<Grid.Resources>
<
collection:ArrayList x:Key="photos"/>
</
Grid.Resources>
<!--Bind to element mainPate-->
<TextBlock Text="{Binding ElementName=mainPage, Path=Title}"/>
<!--Bind to self-->
<TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=Background}"/>
<!--Bind to its TemplatedParent property-->
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
<!--Bind to an ancestor-->
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type TypeName=TreeView}}}"/>
<!--Bind to the previous data.-->
<TextBlock Text="{Binding RelativeSource={RelativeSource PreviousData}}"/>
<!--Bind to a resourc, one-way binding unless photos's type implements INotifyPropertyChanged-->
<!--WPF has an inner collection class which implements INotifyPropertyChanged-->
<TextBlock Text="{Binding Source={StaticResource photos}, Path=Count}"/>

<!--binding source can be of any type, target can only be dependency objects-->

WPF Data Binding - Code

TextBlock textBlock = new TextBlock();
//Set the binding.
textBlock.SetBinding(TextBlock.TextProperty,new Binding{ Source = this, Path = new PropertyPath("Width")});
//Remove the binding, only one-way is removed.
textBlock.Text = "";
//Remove the binding.
BindingOperations.ClearBinding(textBlock, TextBlock.TextProperty);
//Remove all the bindings.
BindingOperations.ClearAllBindings(textBlock);

Windows Media Player Control - Mute

axWindowsMediaPlayer1.settings.mute = true;

Msi Properties

The link below shows the detail list:
http://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx#operating_system_properties

Deply ClickOnce Application Manually and Add Prerequisites

http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/1de12214-680d-4adf-96f4-bae72b481ed6

Sunday, January 3, 2010

Filter and Pagination

The thread below shows a good idea to filter a BindingSource at first and then get one page from it:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/8f27b1b8-f63c-4f8e-80c8-51fdd6e44219

Saturday, January 2, 2010

Accessing Resources in Other Assemblies

<SolidColorBrush
x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyClass},
ResourceId=MyClassBrush}">Yellow</SolidColorBrush>


<Button Background="{DynamicResource {x:Static otherAssembly:MyClass.MyClassBrushKey}}"/>


WPF unleashed, page 256

Handle Resources programmatically

//Add resources.
window.Resources.Add("backgroundBrush", new SolidColorBrush(Colors.Blue));
window.Resources.Add("borderBrush", new SolidColorBrush(Colors.Red));

//Access resources.
Button button = new Button();
button.Background = (Brush)button.FindResource("backgroundBrush");
button.BorderBrush = (Brush)button.FindResource("borderBrush");


 



WPF unleashed, page 254.

Avoid Sharing a Resource

x:Shared=”False”

Merging Resources

<Window.Resources>
<
ResourceDictionary>
<
ResourceDictionary.MergedDictionaries>
<
ResourceDictionary Source=”±file1.xaml”±/>
<
ResourceDictionary Source=”±file2.xaml”±/>
</
ResourceDictionary.MergedDictionaries>
</
ResourceDictionary>
</
Window.Resources>


WPF unleashed, page 253.

Accessing Resources Embedded in Another Assembly

AssemblyReference;Component/ResourceName

WPF unleashed, page243

A complete table about resources accessing methods, page 242.

How do I create a full-trust XAML Browser Application?

If you want to take advantage of functionality that requires a higher level of trust yet still want
to be integrated into a browser,you can indeed configure an XBAP to require full trust. The
two actions to enable this are a bit convoluted,however:
1. In the ClickOnce application manifest (app.manifest),add Unrestricted=”true” to
the PermissionSet XML element. For example:
<PermissionSet class=”System.Security.PermissionSet” version=”1”
ID=”Custom” SameSite=”site” Unrestricted=”true”/>
2. In the project file (with the .csproj or .vbproj extension),change
<TargetZone>Internet</TargetZone>
to
<TargetZone>Custom</TargetZone>
You can also make this change inside Visual Studio’s project properties UI on the
Security tab.

WPF unleashed, page 231

How to enable our own library to run in the WPF browser application.

You use the same mechanism that applies to all .NET components. If you mark an assembly
with the AllowPartiallyTrustedCallers attribute and install it into the Global Assembly
Cache (which can only be done if the user trusts your code and decides to run it),any of the
assembly’s public APIs can be called by any XBAP

WPF unleashed, page 230

Nonrectangular Window, Irregular Window

<Window x:Class="MyWpfSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True" WindowStyle="None" Background="Transparent"
Title="Window1" Height="300" Width="300"
MouseLeftButtonDown="Window_MouseLeftButtonDown">
<
Grid>
<
Ellipse Fill="Red" Opacity="0.5" Margin="20">
<
Ellipse.BitmapEffect>
<
DropShadowBitmapEffect/>
</
Ellipse.BitmapEffect>
</
Ellipse>
<
Button Margin="100" Click="Button_Click">Close</Button>
</
Grid>
</
Window>



public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}

Referenced from book WPF unleashed, page225.

WPF Hook Message

IntPtr hwnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));


private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_PAINT)
{
//Do some extra actions.
}
return IntPtr.Zero;
}

C# Check OS Version

System.Environment.OSVersion

http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx

Edit Page: PageFunction

http://msdn.microsoft.com/en-us/library/ms615540.aspx

Below is a simple sample.

PageFunction:

<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="NavigateSample.PageFunction1"
x:TypeArguments="sys:String"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="PageFunction1">
<
Grid>

</
Grid>
</
PageFunction>



Show page and get result:




//Instantiate the edit page.
PageFunction1 editPage = new PageFunction1();
//Show the page.
this.NavigationService.Navigate(editPage);

//Get returned result.
string result;
editPage.Return += new ReturnEventHandler<string>((object obj, ReturnEventArgs<string> arg)=>
{
result = arg.Result;
});

Have one Frame Navigate to Another Page

<TextBlock Height="23" HorizontalAlignment="Left" Margin="66,66,0,0" Name="textBlock1" VerticalAlignment="Top" Width="278" >
Click <Hyperlink NavigateUri="Page2.xaml">Page2</Hyperlink>
<
Hyperlink NavigateUri="Page3.xaml" TargetName="frame">Have frame navigate to another page.</Hyperlink>
</
TextBlock>
<
Frame Name="frame" Source="Page2.xaml" Margin="66,118,137,52" />

A Simple Way to Persist and Retrieve Application Settings : IsolatedStorage

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx

WPF Single Instance

http://pietschsoft.com/post/2009/01/Single-Instance-WPF-Application-in-NET-3.aspx
http://social.msdn.microsoft.com/forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60/
http://msdn.microsoft.com/en-us/library/ms771662.aspx

Notes about Application

1. The event fired when the current user logs off or shuts down the computer: SessionEnding
http://msdn.microsoft.com/en-us/library/system.windows.application.sessionending.aspx

2. Some properties about window: Windows, MainWindow
http://msdn.microsoft.com/en-us/library/system.windows.application.windows.aspx
http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx

3. Customerize how to exit an application: ShutdownMode
http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx

4. Start window: StartupUri
http://msdn.microsoft.com/en-us/library/system.windows.application.startupuri.aspx

5. A dictionary for the windows to share information: Properties
http://msdn.microsoft.com/en-us/library/system.windows.application.properties.aspx

How to Get Command Arguments

string[] args = System.Environment.GetCommandLineArgs();

Followers