How To:VC++ 2008 Express创建WPF工程的方法
via:http://blog.chinaunix.net/u/31687/showart_478923.html(my old blog)
VC++ 2008 Express创建WPF工程的方法
很显然, 如果不是MicroSoft对Visual C++的定位有变化, 就是MicroSoft在弱化C++在Visual Studio中的地位.
可以看到, Visual C++ 2008 Express Edition (简称VC2008 EE)中没有WPF Project的工程模板, MicroSoft也没有对Visual C++ 2008 Express提供编译XAML 和 WPF Designer的支持, 但是不代表我们不能使用C++ 开发WPF工程, 因为VC同样可以访问那些必要的组件和方法.
感谢MS MVP Void Nish 在这里提供的资料, 我们来建立一个WPF工程 for VC2008EE:
- 打开VC2008 EE, 新建一个C++项目, 项目类型选择: CLR Empty Project, 输入项目名称,比如 “WPFProject”
- 然后打开工程选项, Common Properties->References. 添加如下引用:
PresentationCore
PresentationFramework
System
System.Xml
UIAutomationProvider
UIAutomationTypes
WindowsBase - 给这个项目新建一个C++源文件, 如main.cpp, 输入如下内容
namespace WPFProject{
using namespace System;
using namespace System::IO;
using namespace System::Windows;
using namespace System::Windows::Media::Imaging;
using namespace System::Windows::Media::Animation;
using namespace System::Windows::Markup;
using namespace System::Windows::Interop;
using namespace System::Windows::Controls;
using namespace System::Xml;ref struct EventHelper
{
//Quit Application
static void OnButtonQuit_Click(Object^ sender, RoutedEventArgs^ e)
{
Window^ mainWnd = Windows::Application::Current->MainWindow;
mainWnd->Close();
}//Load .jpg file to image
//and begin a Stroyboard
static void OnButtonAction1_Click(Object^ sender, RoutedEventArgs^ e)
{
Window^ mainWnd = Windows::Application::Current->MainWindow;
Image^ img = (Image^)mainWnd->FindName(“imageTest”);
Uri^ uri = (Uri^)gcnew Uri(IO::Directory::GetCurrentDirectory()+ “\\disp.jpg”);
BitmapImage ^bmp = gcnew BitmapImage(uri);
img->Source = bmp;Storyboard^ myStory = (Storyboard^)Windows::Application::Current->Resources["myStory"];
myStory->Begin(mainWnd, true);}
//resume storyboard
static void OnButtonAction2_Click(Object^ sender, RoutedEventArgs^ e)
{
Window^ mainWnd = Windows::Application::Current->MainWindow;
Storyboard^ myStory = (Storyboard^)Windows::Application::Current->Resources["myStory"];
myStory->Resume(mainWnd);
}//pause storyboard
static void OnButtonAction3_Click(Object^ sender, RoutedEventArgs^ e)
{
Window^ mainWnd = Windows::Application::Current->MainWindow;
Storyboard^ myStory = (Storyboard^)Windows::Application::Current->Resources["myStory"];
myStory->Pause(mainWnd);
}
};int LoadWPFWindow() {
//load Application Resource
FileStream^ resStream = File::OpenRead(“App1.xaml”);
if(resStream == nullptr)
return 0;
Application ^app = (Application^)XamlReader::Load(resStream);
resStream->Close();//load Window
FileStream^ pStream = File::OpenRead(“Window1.xaml”);
if(pStream == nullptr)
return 0;
Window^ wpfWindow = (Window^)XamlReader::Load(pStream);
pStream->Close();//FindName will find the element with the specified identifier
//And raise event handler
Button^ btn = (Button^)wpfWindow->FindName(“buttonQuit”);
btn->Click += gcnew RoutedEventHandler(&EventHelper::OnButtonQuit_Click);
btn = (Button^)wpfWindow->FindName(“buttonAction1″);
btn->Click += gcnew RoutedEventHandler(&EventHelper::OnButtonAction1_Click);
btn = (Button^)wpfWindow->FindName(“buttonAction2″);
btn->Click += gcnew RoutedEventHandler(&EventHelper::OnButtonAction2_Click);
btn = (Button^)wpfWindow->FindName(“buttonAction3″);
btn->Click += gcnew RoutedEventHandler(&EventHelper::OnButtonAction3_Click);return app->Run(wpfWindow);
}[STAThreadAttribute]
int WPFMain() {LoadWPFWindow();
return 0;
}
} - 新建两个xaml文件, 如Window1.xaml和App1.xaml,分别输入以下内容:
Window1.xaml, 定义了静态的窗口元素:<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Test"
Height="400" Width="400"
>
<Grid >
<DockPanel Margin="0,0,0,0" Name="dockPanelMain" Background="Ivory" VerticalAlignment="Top" LastChildFill="True">
<TextBlock Height="21" Name="textBlockTitle" DockPanel.Dock="Top" >
WPF Project Test
</TextBlock>
<StackPanel Background="LightGoldenrodYellow"
DockPanel.Dock ="Left" Width="120" Margin="5"
Height="200" >
<Expander IsExpanded ="True" Header="Action Menu">
<Border Margin ="5" Padding ="10" Background ="Ivory" BorderBrush ="DimGray" BorderThickness ="1">
<StackPanel>
<Button x:Name="buttonAction1">Action 1(Load)</Button>
<Button x:Name="buttonAction2">Action 2(Resume)</Button>
<Button x:Name="buttonAction3">Action 3(Pause)</Button>
<Line Margin="3" Fill="Ivory"></Line>
<Button x:Name="buttonQuit">Quit</Button>
</StackPanel>
</Border>
</Expander>
</StackPanel>
<Canvas DockPanel.Dock="Right" ><Image Canvas.Left=“30″ Canvas.Top=“50″ Height=“150″ x:Name=“imageTest” Stretch=“Fill” Width=“150″ >
<Image.RenderTransform>
<ScaleTransform ScaleX=“1″ ScaleY=“0″ CenterY=“75″ CenterX=“75″ x:Name=“transf”/>
</Image.RenderTransform></Image>
</Canvas>
</DockPanel>
</Grid>
</Window>App1.xaml, 我这里是包含了一些资源, 样式表和故事板:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<Style TargetType ="{x:Type Button}">
<Setter Property ="Background" Value ="Yellow"/>
</Style><Storyboard RepeatBehavior=“Forever” AutoReverse=“True” x:Name=“myStory1″ x:Key=“myStory”>
<DoubleAnimation
Storyboard.TargetName=“transf”
Storyboard.TargetProperty=“ScaleY”
From=“0″ To=“1″ Duration=“0:0:1″ />
</Storyboard></Application.Resources>
</Application> - 还是工程选项, 在linker->advance->Entery Point设置成我们的启动函数
WPFMain; Linker->System->Sub System设置成Windows (/SUBSYSTEM:WINDOWS).
别忘了入口函数WPFMain()前的[STAThreadAttribute]标记, 否则会在解析XAML流是抛出一个XamlParseException异常.
(btw, 我不晓得怎么在.Net下俘获这样的异常, try {}catch (XamlParseException ^e) {} 是俘获不到的. 晕一个.)
然后就OK了, 运行.

zhen niu a
wo dou kan bu dong