@shellex说: 对对,他们上辈子都是折翼的新奥尔良鸡翅

Pages

Topics

随便看看

路边社评论员

  • Keith:
    还能用不. »
  • deepblue:
    测试一下浏览器和系统 »
  • abettor:
    “就和CPU特权级别一样”——这的哥难道是Linus的表弟?! »
  • 董英男:
    为什么总提示确认是相册首页呢 到底哪个才是相册首页啊 »
  • kendisk:
    作为一个轻度Linuxer,刚分手后,感觉木有鸭梨。 »
  • MS IE:
    THIS SITE REALLY SUCK! »
  • Alex:
    gnome-women... »
  • liangsuilong:
    GNOME 自己也有鼓励女性参与项目的计划啊.. »
  • infinte:
    对不起,你的“解ban”版本算得有点问题,可以下(9)pp4 测试。ACID3可有95分啊……另外同... »
  • Alex:
    »
  • Randee Saadat:
    Glad you solved your problem, but what is your que... »
  • LinuxRock:
    没想到你也有一台和我一样的破机子......还好现在高三没怎么用,受不了它的发热量.. »

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了, 运行.

  1. On May 6, 2008 at 4:56 pm
    niesl: Internet Explorer 7.0 / Windows XP

    zhen niu a

    wo dou kan bu dong

  2. On January 14, 2009 at 10:20 pm
    Ada: Internet Explorer 6.0 / Windows XP

    呃。呃。完全晕。

  3. On January 14, 2009 at 10:40 pm
    shellex: Mozilla Firefox 3.0.5 / Linux

    @Ada,
    这种文章…可以不必勉强嘛

Leave a Reply