前言 虽然本人对彩票不感兴趣,仍然有不少人对此情有独钟。他们花大量时间精力去分析彩票的历史记录,企图发现规律,为下一次投注做指导,希望“赢的“”概率增大。不管研究历史记录是否有意义,我用软件实现了对彩票的分析,手工分析彩票几天工作量,现在一秒可以实现。

执行程序,点我下载!

程序界面

处理原理分析

程序实际上是对六合彩分析(彩票种类很多,本文只处理一种)。数据格式如下:

2010001 11 13 22 16 21 18
2010002 22 28 16 5 14 26
2010003 5 14 45 48 16 25

每期6个号,任选3个号,如果6个号中包含该3个号,该期算中奖了。

彩票数值范围为1--49,每期都是6选3. 每次投注3个号,所有的投注可能性有18424次,从概率上讲,每次投注中奖的可能性是18424分之1.

软件就是分析选哪三个号中奖概率最大。是根据历史记录统计分析,找出历史记录上哪些号码出现次数多。

 彩票历史记录界面 

这是ListView控件,显示彩票历史记录。如何显示这样的界面?这种类型的界面非常适用MVVM模式展示。你准备数据和显示模板,剩下的事由ListView来做。

ListView需要绑定的数据定义如下:

  public class ListViewBindingItem
{
public string Index { get; set; } //序号
public string RecordNO { get; set; } //期数
public string[] Digital { get; set; }
public RecordItemInFile RecordItem { get; internal set; } public int ValueIndexOf(string value)
{
for (int i = ; i < Digital.Length; i++)
{
if (Digital[i] == value)
{
return i;
}
}
return -;
} public string StrDigital
{
get
{
return string.Format("{0} {1} {2} {3} {4} {5}",
Digital[], Digital[], Digital[],
Digital[], Digital[], Digital[]);
}
} }

这个类型的定义与界面密切相关,与asp.net 中的ViewMode概念相似。如果界面只是显示字符串,把数据绑定到ListView就大功告成。但是我们想把彩票中的数字显示在圆形背景中,这时候就需要用到数据模板DataTemplate。数据模板的输入是数据和模板,输出就是你想要的界面。模板就是定义数据怎么显示的。如何处理数据如何显示有很多种方法,我只举其中一种。注:每种显示效果可能都会有多种处理方法,这是WPF的灵活性,也是wpf让人困惑的地方。

ListView XAML定义如下

   <ListView  MinHeight="" BorderThickness="1,1,1,1"
VirtualizingPanel.IsVirtualizing="True"
Grid.Row="" x:Name="lvAllDigital" >
<ListView.View>
<GridView >
<GridViewColumn Header="序号" Width="" DisplayMemberBinding="{Binding Path=Index}"></GridViewColumn>
<GridViewColumn Header="期数" Width="" DisplayMemberBinding="{Binding Path=RecordNO}"></GridViewColumn>
<GridViewColumn Header="1列" Width=""
CellTemplate="{StaticResource ColDigital1}"></GridViewColumn>
<GridViewColumn Header="2列" Width="" CellTemplate="{StaticResource ColDigital2}"></GridViewColumn>
<GridViewColumn Header="3列" Width="" CellTemplate="{StaticResource ColDigital3}"></GridViewColumn>
<GridViewColumn Header="4列" Width="" CellTemplate="{StaticResource ColDigital4}"></GridViewColumn>
<GridViewColumn Header="5列" Width="" CellTemplate="{StaticResource ColDigital5}"></GridViewColumn>
<GridViewColumn Header="6列" Width="" CellTemplate="{StaticResource ColDigital6}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>

数字显示的模板定义如下:

<DataTemplate x:Key="ColDigital1" >
<StackPanel Margin="5,2,5,2" HorizontalAlignment="Center" Width="">
<local:CustomControl_digital x:Name="labelDigital1" Width="" Height=""
StrDigital="{Binding Path=Digital[0]}"></local:CustomControl_digital>
</StackPanel>
</DataTemplate>
CustomControl_digital类是一个自定义控件,这个控件就是根据输入的数字,显示圆形背景图像。这个处理也很简单。
public class CustomControl_digital : Control
{
public static readonly DependencyProperty StrDigitalProperty;
public static Color defaultColor = Color.FromRgb(, , ); Color BackColor { get; set; } = defaultColor; static CustomControl_digital()
{
StrDigitalProperty =
DependencyProperty.Register("StrDigital", //属性名称
typeof(string), //属性类型
typeof(CustomControl_digital), //该属性所有者,即将该属性注册到那个类上
new PropertyMetadata("")); //属性默认值
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl_digital), new FrameworkPropertyMetadata(typeof(CustomControl_digital)));
} public void SetBackColor(int index)
{
if (index == )
BackColor = defaultColor;
else
BackColor = Color.FromRgb(, , );
} public int Row { get; set; } = -;
public int Column { get; set; }
public string StrDigital_old { get; set; }
public string StrDigital
{
get { return (string)GetValue(StrDigitalProperty); }
set { SetValue(StrDigitalProperty, value); }
} static Dictionary<Brush, Pen> _colorToPenGroup = new Dictionary<Brush, Pen>();
public static Pen GetPen(Brush color)
{
if(_colorToPenGroup.TryGetValue(color,out Pen pen))
{
return pen;
} Pen pen2 = new Pen(color, );
pen2.Freeze();
_colorToPenGroup.Add(color, pen2);
return pen2;
} static Dictionary<Color, SolidColorBrush> _colorToBrushGroup = new Dictionary<Color, SolidColorBrush>();
public static SolidColorBrush GetBrush(Color color)
{
if (_colorToBrushGroup.TryGetValue(color, out SolidColorBrush brush))
{
return brush;
} SolidColorBrush newBrush = new SolidColorBrush(color);
newBrush.Freeze();
_colorToBrushGroup.Add(color, newBrush);
return newBrush;
} protected override void OnRender(DrawingContext dc)
{ base.OnRender(dc);
if (StrDigital == "--")
return; double len = Math.Min(ActualHeight, ActualWidth);
Point center = new Point(ActualWidth / , ActualHeight / ); Pen pen = GetPen(Brushes.Black);
Brush brush = GetBrush(BackColor); double totalRadius = len / ;
double radius = totalRadius * / ;
dc.DrawEllipse(brush, pen, center, radius, radius); if (!string.IsNullOrEmpty(StrDigital))
{
FormattedText text = new FormattedText(StrDigital, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), , Brushes.White);
Point txtPoint = new Point(center.X - , center.Y - );
dc.DrawText(text, txtPoint);
}
}
}

因为需要绑定数据,所以需要定义依赖属性。需要重载函数 protected override void OnRender(DrawingContext dc),在这个函数中处理显示。

 彩票中奖结果界面

这个也是ListView控件,只是数据模板定义不一样。数据模板定义如下:

 <DataTemplate x:Key="keyDigitalView" >
<StackPanel Margin="5,2,5,2" HorizontalAlignment="Center" Width="">
<local:UserControl_DigitalView x:Name="digitalView"
StrDigital="{Binding Path=StrDigital}"
StrHighlight="{Binding Path=StrHighlight}">
</local:UserControl_DigitalView>
</StackPanel>
</DataTemplate>

要显示这样的效果需要知道两个参数:1 需要显示哪些数字,2 哪些数字需要绿色背景。这两个参数是 StrDigital,StrHighlight。我们生成用户控件,同时定义这两个参数;为了可以绑定,参数必须定义为依赖属性。

UserControl_DigitalView控件定义如下:
public partial class UserControl_DigitalView : UserControl
{
public static readonly DependencyProperty StrDigitalProperty;
public static readonly DependencyProperty StrHighlightProperty; static UserControl_DigitalView()
{
StrDigitalProperty =
DependencyProperty.Register("StrDigital", //属性名称
typeof(string), //属性类型
typeof(UserControl_DigitalView), //该属性所有者,即将该属性注册到那个类上
new PropertyMetadata("")); //属性默认值 StrHighlightProperty =
DependencyProperty.Register("StrHighlight", //属性名称
typeof(string), //属性类型
typeof(UserControl_DigitalView), //该属性所有者,即将该属性注册到那个类上
new PropertyMetadata("")); //属性默认值 // DefaultStyleKeyProperty.OverrideMetadata(typeof(UserControl_DigitalView),
// new FrameworkPropertyMetadata(typeof(UserControl_DigitalView))); } public string StrDigital
{
get { return (string)GetValue(StrDigitalProperty); }
set { SetValue(StrDigitalProperty, value); }
} public string StrHighlight
{
get { return (string)GetValue(StrHighlightProperty); }
set { SetValue(StrHighlightProperty, value); }
} public UserControl_DigitalView()
{
InitializeComponent();
} private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
ShowView();
} Run GetRun(string digital, string[] digitalHighlight)
{
Run run = new Run();
int count = digitalHighlight.Where(o => o == digital).Count();
if (count > )
{
run.Background = Brushes.Green;
run.Foreground = Brushes.White;
}
run.Text = digital;
return run;
} private void ShowView()
{
string[] digitals = StrDigital.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] digitalHighlight = StrHighlight.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string digital in digitals)
{
txtDigital.Inlines.Add(GetRun(digital, digitalHighlight));
txtDigital.Inlines.Add(" ");
}
}
}

txtDigital是TextBlock控件,该控件的Inlines属性可以包含不同字体属性的文字片段。我们需要根据StrDigital,StrHighlight参数填充这些片段。

后记 WPF非常重要的概念就是绑定和模板,这是MVVM模式具体的体现。WPF的控件其实并不多,可能还没WinForm多,但是wpf控件都是可定制的。控件只是定义行为(相当于业务逻辑),控件外观都是可定制,wpf控件可以以一当十;所以一个框架的好坏以控件多少来评判是不公平。本人用业余时间两天开发完成这个程序,可见WPF开发的效率和灵活性之高。

彩票历史记录分析工具 -- 通过实例学习wpf开发的更多相关文章

  1. OFD电子证照模版制作工具 --(采用wpf开发)

    前言  ofd应用的范围非常广,电子证照是其中非常重要的一个应用.同一类电子证照具有相同的板式.元数据:所以电子证照非常适合用模版来制作.模版就是板式样式固定,每个具体的证照只是文字或图片内容不同.比 ...

  2. linux 内核分析工具 Dtrace、SystemTap、火焰图、crash等

    << System语言详解 >> 关于 SystemTap 的书. 我们在分析各种系统异常和故障的时候,通常会用到 pstack(jstack) /pldd/ lsof/ tc ...

  3. Linux 日志分析工具之awstats

    一.awstats 是什么 官方网站:AWStats is a free powerful and featureful tool that generates advanced web, strea ...

  4. WPF开发的彩票程序(练手好例子) 附源码

    前言 WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大. 应朋友之邀,编写了一个小程序.程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑. 对于新手学习有很好的借鉴意义, ...

  5. WPF开发的彩票程序(练手好例子)

    前言 WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大. 应朋友之邀,编写了一个小程序.程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑. 对于新手学习有很好的借鉴意义, ...

  6. 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧

    之前一篇<工欲善其事,必先利其器.VS2013全攻略(安装,技巧,快捷键,插件)!> 看到很多朋友回复和支持,非常感谢,尤其是一些拍砖的喷油,感谢你们的批评,受益良多. 我第一份工作便是W ...

  7. Cacti 是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具

    Cacti 是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具. mysqlreport是mysql性能监测时最常用的工具,对了解mysql运行状态和配置调整都有很大 ...

  8. 开发工具-网络封包分析工具Charles

    extends:http://blog.devtang.com/blog/2013/12/11/network-tool-charles-intr/ 简介 本文为InfoQ中文站特供稿件,首发地址为: ...

  9. [原创]Android Monkey 在线日志分析工具开发

    [原创]Android Monkey 在线日志分析工具开发 在移动App测试过程中,Monkey测试是我们发现潜在问题的一种非常有效手段,但是Android原生的Monkey有其天然的不足,数据不能有 ...

随机推荐

  1. 【NS2】各种TCP版本 之 TCP Tahoe 和 TCP Reno(转载)

    实验目的 学习TCP的拥塞控制机制,并了解TCP Tahoe 和 TCP Reno的运行方式. 基础知识回顾 TCP/IP (Transmission Control Protocol/Interne ...

  2. python 文本文件的操作

  3. oralce分析函数如何工作

    语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达式,…> <ORDER BY 表达式 <ASC DESC> &l ...

  4. Gym - 101480K_K - Kernel Knights (DFS)

    题意:有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同) 要求找以一个区间s: 集合S中的骑士不会互相挑战. 每个集合外的骑士必定会被集合S内的某个骑士挑战. 题解:讲真被题目绕懵 ...

  5. @codeforces - 1209H@ Moving Walkways

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 机场中常常见到滑行道:假如一个滑行道的运行速度为 s,你的行走速 ...

  6. auto uninstaller密钥激活码破解注册机ver 8.8.58

    auto uninstaller密钥破解注册机ver 8.8.58 楼主分享几个auto uninstaller密钥破解注册机,可以用于auto uninstaller 8.8.58 .因为每个版本的 ...

  7. 「BZOJ2510」弱题

    「BZOJ2510」弱题 这题的dp式子应该挺好写的,我是不会告诉你我开始写错了的,设f[i][j]为操作前i次,取到j小球的期望个数(第一维这么大显然不可做),那么 f[i][j]=f[i-1][j ...

  8. behavior planning——11 create a cost function speed penalty

    A  key part of getting transitions to happen when we want  them to is the design of reasonable cost ...

  9. Java安装完毕后的环境配置

    右键计算机=>属性=>高级系统设置=>环境变量=>系统变量=>新建系统变量 变量名:JAVA_HOME变量值:E:\Program Files\Java\jdk-9.0. ...

  10. hdu 3662 3D Convex Hull

    Problem - 3662 题意很简单,构造三维凸包,求凸包有多少个面. 代码如下: #include <cstdio> #include <iostream> #inclu ...