首页 > 程序开发 > 软件开发 > 其他 > 正文
WPF实例图解
2017-02-17       个评论      
收藏    我要投稿

1 StackPanel

请输入昵称:

确定

\

2 选项卡

\

3 grid

\

4 groupbox

\

5 WPF版进程管理器

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;

using System.Diagnostics;

using System.Collections.Generic;

using System.Collections.ObjectModel;

namespace EnumProcess

{

///

/// MainWindow.xaml 的交互逻辑

///

public partial class MainWindow : Window

{

ObservableCollection processInfoList = new ObservableCollection();

internal ObservableCollection ProcessInfoList

{

get { return processInfoList; }

set { processInfoList = value; }

}

public MainWindow()

{

InitializeComponent();

RefreshList();

listView1.ItemsSource = processInfoList;

}

private void RefreshList()

{

Process[] processes;

processes = Process.GetProcesses();

foreach (Process instance in processes)

{

string str1,str2,str3;

str1=instance.ProcessName;

str2=instance.Id.ToString();

str3 = instance.WorkingSet64.ToString();

processInfoList.Add(new processInfo(str1,str2,str3));

}

}

}

class processInfo

{

private string _pname;

private string _pid;

private string _pworkset;

public string pname

{

get { return _pname; }

set { _pname = value; }

}

public string pid

{

get { return _pid; }

set { _pid = value; }

}

public string pworkset

{

get { return _pworkset; }

set { _pworkset = value; }

}

public processInfo(string name, string id, string workset)

{

_pname = name;

_pid = id;

_pworkset = workset;

}

}

}
\

6 dockpanel

button1button2button3button4button5

\

7 wpf 2d 图形

\

8 基本3D

\

9 wpf 特效按钮

\
\
\
\

10 wpf依赖属性的说明

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 btnspace

{

///

/// Interaction logic for MainWindow.xaml

///

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

private void btnSpace_Click(object sender, RoutedEventArgs e)

{

this.btnSpace.Space = 3;

}

private void winSpace_Click(object sender, RoutedEventArgs e)

{

this.Space = 3;

}

static MainWindow()

{

FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();

metadata.Inherits = true;

SpaceProperty = SpaceButton.SpaceProperty.AddOwner(typeof(MainWindow));

SpaceProperty.OverrideMetadata(typeof(Window),metadata);

}

public static readonly DependencyProperty SpaceProperty;

public int Space

{

set

{

SetValue(SpaceProperty, value);

}

get

{

return (int)GetValue(SpaceProperty);

}

}

}

}

SpaceButton.cs

using System;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

namespace btnspace

{

public class SpaceButton : Button

{

// 传统.Net做法 私有字段搭配一个公开属性

string txt;

public string Text

{

set

{

txt = value;

Content = SpaceOutText(txt);

}

get

{

return txt;

}

}

// 依赖属性

public static readonly DependencyProperty SpaceProperty;

//.Net属性包装器

public int Space

{

set

{

SetValue(SpaceProperty, value);

}

get

{

return (int)GetValue(SpaceProperty);

}

}

// 静态的构造函数

static SpaceButton()

{

// 定义元数据

FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();

metadata.DefaultValue = 0;

metadata.Inherits = true;

metadata.PropertyChangedCallback += OnSpacePropertyChanged;

// 注册依赖属性

SpaceProperty = DependencyProperty.Register("Space", typeof(int), typeof(SpaceButton), metadata, ValidateSpaceValue);

}

// 值验证的回调函数

static bool ValidateSpaceValue(object obj)

{

int i = (int)obj;

return i >= 0;

}

// 属性值改变的回调函数

static void OnSpacePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)

{

SpaceButton btn = obj as SpaceButton;

string txt = btn.Content as string;

if (txt == null) return;

btn.Content = btn.SpaceOutText(txt);

}

// 该方法给字符串间距加上空格

string SpaceOutText(string str)

{

if (str == null)

return null;

StringBuilder build = new StringBuilder();

// 往里面加上Space个空格

foreach (char ch in str)

build.Append(ch + new string(' ', Space));

return build.ToString();

}

}

}
\

11 控件模板

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 button03

{

///

/// Interaction logic for MainWindow.xaml

///

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

private void Window_Loaded(object sender, RoutedEventArgs e)

{

Button obj = btn.Template.FindName("btn1", btn) as Button;

}

}

}
\

12 动画演示

XzWindow.xaml;

SimpleAniWindow.xaml;

SimpleAniWindow.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.Shapes;

using System.Windows.Media.Animation;

namespace AnimationDemo

{

///

/// SampleAniWindow.xaml 的交互逻辑

///

public partial class SampleAniWindow : Window

{

public SampleAniWindow()

{

InitializeComponent();

}

private void Grid_Loaded(object sender, RoutedEventArgs e)

{

}

private void button1_Click(object sender, RoutedEventArgs e)

{

DoubleAnimation widthAnimation =

new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);

//指定高度变化的起点,终点与持续时间,并在动画结束时保持大小

DoubleAnimation heightAnimation =

new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);

//开始动画

//变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化

button1.BeginAnimation(Button.WidthProperty, widthAnimation);

button1.BeginAnimation(Button.HeightProperty, heightAnimation);

}

private void button2_Click(object sender, RoutedEventArgs e)

{

DoubleAnimation widthAnimation =

new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);

//指定高度变化的起点,终点与持续时间,并在动画结束时恢复原值

DoubleAnimation heightAnimation =

new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);

//开始动画

//变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化

button2.BeginAnimation(Button.WidthProperty, widthAnimation);

button2.BeginAnimation(Button.HeightProperty, heightAnimation);

}

}

}
\
\

项目在此下载;

http://pan.baidu.com/s/1o7OEMc6

AnimationDemo.rar;

13 绘制例子

\

14 路径的使用

PathAnimationWindow.xaml;

项目在此下载;

http://pan.baidu.com/s/1o7OEMc6

PathDemo.rar;

\
\
\
\
\

15 3D立方体和光照

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;

using System.Windows.Media.Media3D;

namespace wpf3d2

{

///

/// Interaction logic for MainWindow.xaml

///

public partial class MainWindow : Window

{

Viewport3D vp = new Viewport3D();

public MainWindow()

{

InitializeComponent();

//this.Content = vp;

//

//grid1.Children.Add(vp);

SetupCamera();

GetModelVisual3D();

}

private void SetupCamera()

{

PerspectiveCamera camera = new PerspectiveCamera();

camera.Position = new Point3D(-40, 40, 40);

camera.UpDirection = new Vector3D(0, 0, 1);

camera.LookDirection = new Vector3D(40, -40, -40);

vp.Camera = camera;

}

private void GetModelVisual3D()

{

Model3DGroup mg = new Model3DGroup();

DirectionalLight dl = new DirectionalLight();

dl.Color = Colors.White; dl.Direction = new Vector3D(-1, -1, -3);

GeometryModel3D gm = new GeometryModel3D();

MeshGeometry3D meshg = new MeshGeometry3D();

Point3DCollection pc = new Point3DCollection();

pc.Add(new Point3D(0, 0, 0)); pc.Add(new Point3D(10, 0, 0)); pc.Add(new Point3D(10, 10, 0));

pc.Add(new Point3D(0, 10, 0)); pc.Add(new Point3D(0, 0, 10)); pc.Add(new Point3D(10, 0, 10));

pc.Add(new Point3D(10, 10, 10)); pc.Add(new Point3D(0, 10, 10));

meshg.Positions = pc;

Int32Collection ic = new Int32Collection();

ic.Add(0); ic.Add(1); ic.Add(3); ic.Add(1); ic.Add(2); ic.Add(3); ic.Add(0); ic.Add(4); ic.Add(3);

ic.Add(4); ic.Add(7); ic.Add(3); ic.Add(4); ic.Add(6); ic.Add(7); ic.Add(4); ic.Add(5); ic.Add(6);

ic.Add(0); ic.Add(4); ic.Add(1); ic.Add(1); ic.Add(4); ic.Add(5); ic.Add(1); ic.Add(2); ic.Add(6);

ic.Add(6); ic.Add(5); ic.Add(1); ic.Add(2); ic.Add(3); ic.Add(7); ic.Add(7); ic.Add(6); ic.Add(2);

meshg.TriangleIndices = ic;

DiffuseMaterial dm = new DiffuseMaterial();

dm.Brush = Brushes.Red;

gm.Geometry = meshg;

gm.Material = dm;

mg.Children.Add(dl);

mg.Children.Add(gm);

ModelVisual3D mv = new ModelVisual3D();

mv.Content = mg;

vp.Children.Add(mv);

}

private void Window_Loaded(object sender, RoutedEventArgs e)

{

grid1.Children.Add(vp);

}

}

}
\
点击复制链接 与好友分享!回本站首页
上一篇:在Windows和debian上配置Armadillo(线性代数库)并使用OpenBlas
下一篇:Shell学习笔记
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站