大家都知道条形码(Barcode)是一种可以由机器识别的特殊编码,在生产、生活中也常常会见到并使用它。条形码的类型和种类很多感兴趣的朋友可以详细了解一下。其中Code 39 可以说是一种最为常见并广泛使用的字符与数字结合的编码类型,本篇也将利用它制作一个带有条形码的员工卡应用程序。

在公司内部员工卡是员工身份唯一的识别工具,同时也是考勤及门禁系统的主要信息来源。首先在WPF 中设计一个简单的员工卡样式,具备员工卡标识、员工相片、员工姓名等。

<Border CornerRadius="3" BorderBrush="Gray" BorderThickness="2" Background="White"
MouseLeftButtonDown="Border_MouseLeftButtonDown">
<Canvas x:Name="mainCanvas">
<Grid x:Name="closeBtn" Canvas.Left="330" Canvas.Top="0"
MouseLeftButtonDown="Close_MouseLeftButtonDown">
<Ellipse Height="15" Width="15" HorizontalAlignment="Center">
<Ellipse.Fill>
<SolidColorBrush x:Name="ellipseColor"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="x" Margin="2,-2,2,2" HorizontalAlignment="Center">
<TextBlock.Foreground>
<SolidColorBrush x:Name="textColor" Color="Gray"/>
</TextBlock.Foreground>
</TextBlock>
</Grid> <Border BorderBrush="#FF54545C" Canvas.Top="25" CornerRadius="5"
Height="49" Width="339" Background="#FF2192C4" Canvas.Left="5">
<TextBlock Text="EMPLOYEE CARD" Foreground="White" FontSize="20"
VerticalAlignment="Center" HorizontalAlignment="Center"
FontWeight="Black" FontFamily="Microsoft Sans Serif"/>
</Border>
<Grid Canvas.Left="96" Canvas.Top="78">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image Source="Images/cardpic.png" Grid.Row="0"/>
<TextBlock Text="Li Jing Ran" FontSize="30" FontWeight="Black"
Grid.Row="1" HorizontalAlignment="Center"/>
</Grid>
</Canvas>
</Border>

 代码内容比较简单,其中需要提一下的是x:Name 为closeBtn 的<Grid>,可以看到它包含了一个<Ellipse>和<Textblock>,它们的颜色填充方式看上去做的很复杂。其实是为了实现一个动态效果:当鼠标移动到关闭图标上时,其<Ellipse>和<Textblock>会改变颜色(如下图对比)。

该效果代码如下,通过Window.Resources 设置一个ColorAnimation Storyboard,再通过MouseEnter、MouseLeave 来触发Storyboard 动画效果。

<Window.Resources>
<Storyboard x:Key="flashClose">
<ColorAnimation Storyboard.TargetName="ellipseColor"
Storyboard.TargetProperty="Color"
From="White" To="Gray" Duration="0:0:0.1"/>
<ColorAnimation Storyboard.TargetName="textColor"
Storyboard.TargetProperty="Color"
From="Gray" To="White" Duration="0:0:0.1"/>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger SourceName="closeBtn" RoutedEvent="Grid.MouseEnter">
<BeginStoryboard x:Name="showClosBtn" Storyboard="{StaticResource flashClose}"/>
</EventTrigger>
<EventTrigger SourceName="closeBtn" RoutedEvent="Grid.MouseLeave">
<StopStoryboard BeginStoryboardName="showClosBtn"/>
</EventTrigger>
</Window.Triggers>
 完成上面的界面设计,最后只需在员工卡下放的空白处添加员工编号条形码即可。首先在项目中加入Barcode 和Code39 类,我们要通过这两个类完成条形码的绘制工作。打开C#程序,编写如下代码。
  通过Barcodes 类创建一个新的条形码,定义BarcodeType 为"Code39",编码Data 为“10001”,如果需要校验则将CheckDigit 设为"Yes"。其中thinWidth、thickWidth 用于定义黑白条码的宽窄度。
Barcodes bb = new Barcodes();
bb.BarcodeType = Barcodes.BarcodeEnum.Code39;
bb.Data = "10001";
bb.CheckDigit = Barcodes.YesNoEnum.Yes;
bb.encode(); int thinWidth;
int thickWidth; thinWidth = 2;
thickWidth = 3 * thinWidth; string outputString = bb.EncodedData;
string humanText = bb.HumanText;

绘制条形码

根据编码(EncodedData)的长度利用Rectangle 类逐一绘制黑、白条码,t 表示窄码,w 表示宽码。

int len = outputString.Length;
int currentPos = 50;
int currentTop = 340;
int currentColor = 0;
for (int i = 0; i < len; i++)
{
Rectangle rect = new Rectangle();
rect.Height = 80;
if (currentColor == 0)
{
currentColor = 1;
rect.Fill = new SolidColorBrush(Colors.Black);
}
else
{
currentColor = 0;
rect.Fill = new SolidColorBrush(Colors.White);
}
Canvas.SetLeft(rect, currentPos);
Canvas.SetTop(rect, currentTop); if (outputString[i] == 't')
{
rect.Width = thinWidth;
currentPos += thinWidth;
}
else if (outputString[i] == 'w')
{
rect.Width = thickWidth;
currentPos += thickWidth;
}
mainCanvas.Children.Add(rect);
}

添加可读码

最后在条形码下方添加一行可读码,方便员工认读条形码内容,也就是将“10001”员工编号显示出来。

TextBlock tb = new TextBlock();
tb.Text = humanText;
tb.FontSize = 25;
tb.FontFamily = new FontFamily("Consolas");
Rect rx = new Rect(0, 0, 0, 0);
tb.Arrange(rx);
Canvas.SetLeft(tb, 120);
Canvas.SetTop(tb, currentTop + 80);
mainCanvas.Children.Add(tb);

WPF 员工卡条形码的更多相关文章

  1. 未来工厂——电器行业ERP案例

    江苏科兴电器有限公司位于全国著名的“银杏之乡”泰兴市南首,主要生产35kV及以下电流.电压互感器等系列产品.产品多次经国家及省市技术监督部门抽检合格,广泛应用于国家重点工程.“COSINE”商标荣获泰 ...

  2. 微信公众平台开发教程(八)Session处理

    微信公众平台开发教程(八)Session处理 在微信窗口,输入的信息有限,我们需要将一些信息分多次请求. 比如:在进行用户绑定时,我们需要输入用户的相关信息,比如:用户名.密码,或者姓名.电话号码,服 ...

  3. php 文本框里面显示数据库调出来的资料

    php 文本框里面显示数据库调出来的资料,,,在里面我标注了,,那个地方为什么是!=才能显示正确的数据库资料啊?我理解的是对比正确输出数据库内容的.大师貌似不知道为什么就写错了 <html> ...

  4. ASP.NET Core 认证与授权[5]:初识授权

    经过前面几章的姗姗学步,我们了解了在 ASP.NET Core 中是如何认证的,终于来到了授权阶段.在认证阶段我们通过用户令牌获取到用户的Claims,而授权便是对这些的Claims的验证,如:是否拥 ...

  5. Xamarin 开发过的那些项目

    您可能已经看到类似的统计数据:智能手机用户在手机媒体上花费了89%的时间使用应用程序.或者听说Gartner预测到2017年移动应用程序下载将产生价值770亿美元的收入.很难不考虑这些数字.今天,每个 ...

  6. winform excel导入--NPOI方式

    项目中要用到excel导入数据,用NPOI方式做了一个demo,记录如下: Form1代码: public Form1() { InitializeComponent(); } private voi ...

  7. asp.net 导出excel的一种方法

    项目用到的一种导出excel 的方法予以记录:(具体的业务类可更具情况替换使用) protected void Export(string filename, List<ComponentCon ...

  8. (转)微信公众平台开发教程(七)Session处理

    原文地址:http://www.cnblogs.com/yank/p/3476874.html 微信公众平台开发教程(七)Session处理 在微信窗口,输入的信息有限,我们需要将一些信息分多次请求. ...

  9. 从乌云的错误漏洞分析看Mifare Classic安全

    前言 12年2月初国内著名安全问题反馈平台-乌云发布了有关某公司员工卡的金额效验算法破解的安全问题.从整个漏洞分析来看,漏洞的提交者把员工卡的数据分析得非常仔细,以至很多刚刚接触或者未曾接触的都纷纷赞 ...

随机推荐

  1. The beta-reports-active Entitlement

    Q:  How do I resolve the "beta-reports-active" code signing error? A: There are a number o ...

  2. vs2012 webservice创建

    第一步:打开VS2012,新建空项目,注意选择.NET Framework的版本.这里我选择的是.NET Framework 4 新建好项目后,在项目中添加一个WebService项 打开这个文件,我 ...

  3. 【JUnit】Junit命令行执行、参数化执行、Main方法执行

    参考资料: main方法执行:http://stackoverflow.com/questions/2543912/how-do-i-run-junit-tests-from-inside-my-ja ...

  4. EF使用延迟加载的本质原因

    EF(Entity Framework)是微软的一个ORM框架 使用过EF的同学都知道它有一个延迟加载的功能 那么这个延迟加载的功能到底是什么? 为什么需要延迟加载? 使用延迟加载的优点和缺点又各是什 ...

  5. 启动sping的时候可以使用system.in.read()暂停

    启动sping的时候可以使用system.in.read()暂停 只要不输入就可以不会停了:

  6. [React] Use react-rewards to add microinteractions to React app to reward users for some actions

    It's important that our users enjoy using our application or website. One way we can make it happen ...

  7. WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试) 接口测试与数据驱动

    转载:http://www.cnblogs.com/chengtch/p/6576117.html 1简介 数据驱动测试,即是分离测试逻辑与测试数据,通过如excel表格的形式来保存测试数据,用测试脚 ...

  8. react-native 初始化 各种报错 及 解决方案

    1.Unable to load script from assets 'index.android.bundle'. curl -k "http://localhost:8081/inde ...

  9. 【Excle数据透视表】如何新建数据透视表样式

    如果觉得Excle给出的数据透视表样式不符合自己的心意,可以自己定义一个数据透视表样式 步骤1 单击数据透视表区域任意单元格→数据透视表工具→设计→样式组中的下拉按钮,打开数据透视表样式库→新建数据透 ...

  10. EasyUI 鼠标经过 显示气泡一例

    $(function(){ $('#contacts').tooltip({ position: 'bottom', content: '<c:forEach items="${rec ...