MainPage.xaml页面图片:
-----------------------------------
<Grid x:Name="LayoutRoot" Background="Transparent" Loaded="LayoutRoot_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="187"/>
</Grid.ColumnDefinitions>
<sdk:Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" Content="瑞高(浙江)建筑系统有限公司" FontSize="16" />
<sdk:Label Grid.Row="1" Grid.Column="1" Content="用户名:" Width="50" Height="24"/>
<sdk:Label Grid.Row="2" Grid.Column="1" Content="密 码:" Width="50" Height="24"/>
<TextBox x:Name="usernames" Grid.Row="1" Grid.Column="2" Width="160" Height="24" HorizontalAlignment="Left" />
<PasswordBox x:Name="passwords" Grid.Row="2" Grid.Column="2" Width="160" Height="24" HorizontalAlignment="Left" />
<sdk:Label Grid.Row="3" Grid.Column="1" Content="验证码:" Width="50" Height="24"/>
<TextBox x:Name="pagecode" Grid.Row="3" Grid.Column="2" Width="160" Height="24" HorizontalAlignment="Left" />
<Image x:Name="CheckCodeImage" Grid.Row="4" Grid.Column="2" Width="150" Height="30" HorizontalAlignment="Left"/>
<Button Grid.Row="5" Grid.Column="2" Width="80" Height="30" Content="确 认" HorizontalAlignment="Left" Click="Button_Click" />
</Grid>
----------------------------------------
MainPage.xaml.cs 代码:
------------------------
using System.Windows.Browser; // 页面跳转引用;
using NewVillacornellAdmin.ServiceReference1; // 用通过WCF的才能实现SL和LINQ TO SQL交互;
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
IndentifyCodeClass code = new IndentifyCodeClass();
generatedCode = code.CreateIndentifyCode(6);
code.CreatImage(generatedCode, CheckCodeImage, 150, 30);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrEmpty(pagecode.Text)){
if (generatedCode.ToLower() != pagecode.Text.ToLower())
{
// MessageBox.Show(generatedCode+","+pagecode.Text);
MessageBox.Show("验证码输入有错误");
}
else {
string Username = usernames.Text.ToLower();
string Password = passwords.Password.ToLower();
// MessageBox.Show(Username+","+Password);
if (!string.IsNullOrEmpty(Username))
{
if (!string.IsNullOrEmpty(Password))
{
Service1Client WebClient = new Service1Client();
WebClient.JudgUsernameCompleted += new EventHandler<JudgUsernameCompletedEventArgs>(WebClient_JudgUser);
WebClient.JudgUsernameAsync(Username, Password);
}
else {
MessageBox.Show("密码不能为空");
}
}
else
{
MessageBox.Show("用户名不能为空");
}
}
}
}
private void WebClient_JudgUser(object sender, JudgUsernameCompletedEventArgs e)
{
List<UsernameView> stu = new List<UsernameView>(e.Result);
var usernm = stu.FirstOrDefault();
if (usernm != null)
{
// MessageBox.Show("登陆成功");
HtmlWindow html = HtmlPage.Window; // 页面跳转;
html.Navigate(new Uri("/index.htm", UriKind.Relative)); //相对路径
}
else
{
MessageBox.Show("登陆失败");
}
}
---------------------------------
对了,这里用用到那个 验证码的代码,我这是个贴出来:
IndentifyCodeClass.cs
--------------------------------
using System.Windows.Media.Imaging;
namespace NewVillacornellAdmin
{
public class IndentifyCodeClass
{
public string CreateIndentifyCode(int count)
{
string allchar = "1,2,3,4,5,6,7,8,9,0,A,a,B,b,C,c,D,d,E,e,F,f," +
"G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s," +
"T,t,U,u,V,v,W,w,X,x,Y,y,Z,z";
string[] allchararray = allchar.Split(',');
string randomcode =string.Empty;
int temp = -1;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp == t)
{
return CreateIndentifyCode(count);
}
temp = t;
randomcode += allchararray[t];
}
return randomcode;
}
Random r = new Random(DateTime.Now.Millisecond);
public void CreatImage(string Text, Image imgsource, int iw, int ih)
{
Grid Gx = new Grid();
Canvas cv1 = new Canvas();
for (int i = 0; i < 6; i++)
{
Polyline p = new Polyline();
for (int ix = 0; ix < r.Next(3, 6); ix++)
{
p.Points.Add(new Point(r.NextDouble() * iw,
r.NextDouble() * ih));
}
byte[] Buffer = new byte[3];
r.NextBytes(Buffer);
SolidColorBrush SC = new SolidColorBrush(Color.FromArgb(255,
Buffer[0], Buffer[1], Buffer[2]));
p.Stroke = SC;
p.StrokeThickness = 0.5;
cv1.Children.Add(p);
}
Canvas cv2 = new Canvas();
int y = 0;
int lw = 6;
double w = (iw - lw) / Text.Length;
int h = (int)ih;
foreach (char x in Text)
{
byte[] Buffer = new byte[3];
r.NextBytes(Buffer);
SolidColorBrush SC = new SolidColorBrush(Color.FromArgb(255,
Buffer[0], Buffer[1], Buffer[2]));
TextBlock t = new TextBlock();
t.TextAlignment = TextAlignment.Center;
t.FontSize = r.Next(h - 3, h);
t.Foreground = SC;
t.Text = x.ToString();
t.Projection = new PlaneProjection()
{
RotationX = r.Next(-30, 30),
RotationY = r.Next(-30, 30),
RotationZ = r.Next(-10, 10)
};
cv2.Children.Add(t);
Canvas.SetLeft(t, lw / 2 + y * w);
Canvas.SetTop(t, 0);
y++;
}
Gx.Children.Add(cv1);
Gx.Children.Add(cv2);
WriteableBitmap W = new WriteableBitmap(Gx, new TransformGroup());
W.Render(Gx, new TransformGroup());
imgsource.Source = W;
}
}
}
------------------------------