轉帖|其它|編輯:郝浩|2010-12-21 15:23:18.000|閱讀 1519 次
概述:大家都知道條形碼(Barcode)是一種可以由機器識別的特殊編碼,在生產(chǎn)、生活中也常常會見到并使用它。條形碼的類型和種類很多感興趣的朋友可以詳細了解一下。其中Code 39 可以說是一種最為常見并廣泛使用的字符與數(shù)字結合的編碼類型,本篇也將利用它制作一個帶有條形碼的員工卡應用程序。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
大家都知道條形碼(Barcode)是一種可以由機器識別的特殊編碼,在生產(chǎn)、生活中也常常會見到并使用它。條形碼的類型和種類很多感興趣的朋友可以詳細了解一下。其中Code 39 可以說是一種最為常見并廣泛使用的字符與數(shù)字結合的編碼類型,本篇也將利用它制作一個帶有條形碼的員工卡應用程序。
在公司內(nèi)部員工卡是員工身份唯一的識別工具,同時也是考勤及門禁系統(tǒng)的主要信息來源。首先在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>
代碼內(nèi)容比較簡單,其中需要提一下的是x:Name 為closeBtn 的<Grid>,可以看到它包含了一個<Ellipse>和<Textblock>,它們的顏色填充方式看上去做的很復雜。其實是為了實現(xiàn)一個動態(tài)效果:當鼠標移動到關閉圖標上時,其<Ellipse>和<Textblock>會改變顏色(如下圖對比)。
該效果代碼如下,通過Window.Resources 設置一個ColorAnimation Storyboard,再通過MouseEnter、MouseLeave 來觸發(fā)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 類創(chuàng)建一個新的條形碼,定義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;
繪制條形碼
根據(jù)編碼(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);
}
添加可讀碼
最后在條形碼下方添加一行可讀碼,方便員工認讀條形碼內(nèi)容,也就是將"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);
效果圖
最后運行下程序看看效果如何,當然條形碼內(nèi)容可按各自需求添加任何字符或數(shù)字。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客轉載