WPF 員工卡條形碼

作者: Gnie  來源: 博客園  發布時間: 2010-07-27 10:14  閱讀: 1204 次  推薦: 0   原文鏈接   [收藏]  

     大家都知道條形碼(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>

Image

     代碼內容比較簡單,其中需要提一下的是x:Name 為closeBtn 的<Grid>,可以看到它包含了一個<Ellipse>和<Textblock>,它們的顏色填充方式看上去做的很復雜。其實是為了實現一個動態效果:當鼠標移動到關閉圖標上時,其<Ellipse>和<Textblock>會改變顏色(如下圖對比)。

normal   change

     該效果代碼如下,通過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);

效果圖

最后運行下程序看看效果如何,當然條形碼內容可按各自需求添加任何字符或數字。

Image

源代碼下載

WPFBarcode.zip

0
0
 
標簽:WPF
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()