1、WPF第一个程序

来源:互联网 发布:linux怎么配置网卡 编辑:程序博客网 时间:2024/05/17 01:54

参考自:http://www.cnblogs.com/evangelion_yu/archive/2009/05/21/WPFintro.html

1、开发工具:vs2008

2、新建工程:

    new->file->project->WPF Application 取名HelloWorld。

3、工程中插入图片:

    点击工程HelloWorld右键->Add->Existing Item,选择图片。

4、新建User Control:

    点击工程HelloWorld右键->Add->New Item,选择WPF->User Control;将UserControl1.xaml和UserControl1.xaml.cs加入工程。

5、自定义User Control:

        UserControl1.xaml文件如下:

<UserControl

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

       x:Class="HelloWorld.UserControl1"

       x:Name="UserControl"

Width="30" Height="200">

 

    <Grid x:Name="LayoutRoot">

        <Rectangle Margin="0,9,0,8.5">

            <Rectangle.Fill>

                <LinearGradientBrush EndPoint="-1.741,0.747" StartPoint="3,0.16">

                    <GradientStop Color="#FFDDDB15" Offset="0"/>

                    <GradientStop Color="#FFFF0000" Offset="1"/>

                </LinearGradientBrush>

            </Rectangle.Fill>

        </Rectangle>

        <Ellipse VerticalAlignment="Top" Height="15">

            <Ellipse.Fill>

                <LinearGradientBrush EndPoint="-1.741,0.747" StartPoint="3,0.16">

                    <GradientStop Color="#FFDDDB15" Offset="0"/>

                    <GradientStop Color="#FFFF0000" Offset="1"/>

                </LinearGradientBrush>

            </Ellipse.Fill>

        </Ellipse>

        <Ellipse VerticalAlignment="Bottom" Height="15">

            <Ellipse.Fill>

                <LinearGradientBrush EndPoint="-1.741,0.747" StartPoint="3,0.16">

                    <GradientStop Color="#FFDDDB15" Offset="0"/>

                    <GradientStop Color="#FFFF0000" Offset="1"/>

                </LinearGradientBrush>

            </Ellipse.Fill>

        </Ellipse>

    </Grid>

</UserControl>

 

6、Window1.xaml文件如下:

<Window x:Class="HelloWorld.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="400" Width="400" xmlns:HelloWorld="clr-namespace:HelloWorld" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" WindowState="Normal" ResizeMode="NoResize">

    <Window.Resources>

        <Storyboard x:Key="Storyboard1">
            <!--卷轴动画-->

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="userControl1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">

                <SplineDoubleKeyFrame KeyTime="00:00:04" Value="-58" KeySpline="0,0,0,1"/>

            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="userControl2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">

                <SplineDoubleKeyFrame KeyTime="00:00:04" Value="180.5" KeySpline="0,1,1,1"/>

            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(FrameworkElement.Width)">

                <SplineDoubleKeyFrame KeyTime="00:00:04" Value="240" KeySpline="0,1,1,1"/>

            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">

                <SplineDoubleKeyFrame KeyTime="00:00:04" Value="-58.5" KeySpline="0,1,1,1"/>

            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

                <SplineDoubleKeyFrame KeyTime="00:00:04" Value="0.5" KeySpline="0,1,1,1"/>

            </DoubleAnimationUsingKeyFrames>

        </Storyboard>

    </Window.Resources>

    <Window.Triggers>

    <EventTrigger RoutedEvent="FrameworkElement.Loaded">

            <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>

        </EventTrigger>

    </Window.Triggers>

    <Window.Background>

        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

            <GradientStop Color="#FF575757" Offset="0"/>

            <GradientStop Color="#FFFFFFFF" Offset="1"/>

            <GradientStop Color="#FF936226" Offset="0.674"/>

            <GradientStop Color="#FF512B2B" Offset="0.353"/>

        </LinearGradientBrush>

    </Window.Background>

    <Grid>
        <!--Grid以及相关的属性 -->

        <Grid.RowDefinitions>

            <RowDefinition Height="0.845*"/>

            <RowDefinition Height="0.155*"/>

        </Grid.RowDefinitions>
        

        <HelloWorld:UserControl1 Margin="101,57.79,0,48.1" d:LayoutOverrides="VerticalAlignment" x:Name="userControl1" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Width="30">
    <!--左边卷轴边缘-->

            <HelloWorld:UserControl1.RenderTransform>

                <TransformGroup>

                    <ScaleTransform ScaleX="1" ScaleY="1"/>

                    <SkewTransform AngleX="0" AngleY="0"/>

                    <RotateTransform Angle="0"/>

                    <TranslateTransform X="0" Y="0"/>

                </TransformGroup>

            </HelloWorld:UserControl1.RenderTransform>
        </HelloWorld:UserControl1>

        <HelloWorld:UserControl1 Margin="132,58.29,0,49.29" HorizontalAlignment="Left" Width="30" x:Name="userControl2" RenderTransformOrigin="0.5,0.5">
           <!--右边卷轴边缘-->

            <HelloWorld:UserControl1.RenderTransform>

                <TransformGroup>

                    <ScaleTransform ScaleX="1" ScaleY="1"/>

                    <SkewTransform AngleX="0" AngleY="0"/>

                    <RotateTransform Angle="0"/>

                    <TranslateTransform X="0" Y="0"/>

                </TransformGroup>

            </HelloWorld:UserControl1.RenderTransform>

        </HelloWorld:UserControl1>


        <Button Style="{DynamicResource redbutton}" Content="Click here" Grid.Row="1" Click="Button_Click"/>
        <Image Margin="131.798,69.595,0,59.985" Source="DSC06927.jpg" Stretch="Fill" Width="0.89" x:Name="image" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left">

        <!--图片-->

            <Image.RenderTransform>

                <TransformGroup>

                    <ScaleTransform ScaleX="1" ScaleY="1"/>

                    <SkewTransform AngleX="0" AngleY="0"/>

                    <RotateTransform Angle="0"/>

                    <TranslateTransform X="0" Y="0"/>

                </TransformGroup>

            </Image.RenderTransform>

        </Image>

    </Grid>

</Window>

 

7、Window1.xaml.cs文件内容如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace HelloWorld
{

    /// <summary>

    /// Window1.xaml 的交互逻辑

    /// </summary>

    public partial class Window1 : Window
    {

        public Window1()
        {

            InitializeComponent();

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            MessageBox.Show("大家好~欢迎观看本文~不足之处还请见谅");

        }

    }

}

 s

8、App.xaml文件如下:

<Application x:Class="HelloWorld.App"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="Window1.xaml">

    <Application.Resources>

        <Style x:Key="redbutton" TargetType="Button" >

            <Setter Property="Foreground" Value="Red"/>

            <Setter Property="FontSize" Value="14"/>

        </Style>

    </Application.Resources>

</Application>

原创粉丝点击